Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
💻 COBOL

COBOL COMP Fields Explained

Intermediate 🕑 15 min read 👁 0 views

COBOL COMP Fields Explained

Computational fields optimize storage and processing for numeric data.

COMP Types

Type Also Known As Description
COMP COMP-4, BINARY Binary format
COMP-1 Single-precision floating point
COMP-2 Double-precision floating point
COMP-3 PACKED-DECIMAL Packed decimal
COMP-5 Native binary

COMP-1 (Float)

```cobol 01 WS-FLOAT COMP-1. ``` 4 bytes, approximate values, scientific use.

COMP-2 (Double)

```cobol 01 WS-DOUBLE COMP-2. ``` 8 bytes, higher precision float.

Storage Comparison

For PIC 9(7):

  • DISPLAY: 7 bytes
  • COMP-3: 4 bytes
  • COMP: 4 bytes

Performance Considerations

  • COMP-3: Best for decimal operations
  • COMP: Best for integer operations
  • DISPLAY: Best for I/O, worst for math

Alignment

Binary fields may require alignment; use SYNCHRONIZED clause.

Code Example

       IDENTIFICATION DIVISION.
       PROGRAM-ID. COMP-TYPES-DEMO.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-COMP PIC S9(9) COMP VALUE 123456789.
       01 WS-COMP3 PIC S9(9) COMP-3 VALUE 123456789.
       01 WS-COMP1 COMP-1 VALUE 3.14159.
       01 WS-COMP2 COMP-2 VALUE 3.14159265358979.
       01 WS-COMP5 PIC S9(9) COMP-5.
       PROCEDURE DIVISION.
           DISPLAY 'COMP (binary): ' WS-COMP
           DISPLAY 'COMP-3 (packed): ' WS-COMP3
           DISPLAY 'COMP-1 (float): ' WS-COMP1
           DISPLAY 'COMP-2 (double): ' WS-COMP2
           
           DISPLAY 'COMP size: ' LENGTH OF WS-COMP
           DISPLAY 'COMP-3 size: ' LENGTH OF WS-COMP3
           DISPLAY 'COMP-1 size: ' LENGTH OF WS-COMP1
           DISPLAY 'COMP-2 size: ' LENGTH OF WS-COMP2
           STOP RUN.