COMP and COMP-3 Fields
Problem Description
Explain and use computational data types COMP, COMP-1, COMP-2, COMP-3.
Expected Output
Understanding binary and packed decimal
Hints
COMP=binary, COMP-3=packed decimal, more efficient storage.
Solution
IDENTIFICATION DIVISION.
PROGRAM-ID. COMPTEST.
DATA DIVISION.
WORKING-STORAGE SECTION.
* DISPLAY numeric (default) - 1 byte per digit
01 WS-DISPLAY PIC 9(5) VALUE 12345.
* COMP (BINARY) - stored in binary format
* PIC 9(4) COMP uses 2 bytes (halfword)
* PIC 9(9) COMP uses 4 bytes (fullword)
01 WS-COMP PIC 9(5) COMP VALUE 12345.
* COMP-3 (PACKED-DECIMAL) - 2 digits per byte + sign
* PIC 9(5) COMP-3 uses 3 bytes
01 WS-COMP3 PIC 9(5) COMP-3 VALUE 12345.
* COMP-1 - single precision float (4 bytes)
01 WS-COMP1 COMP-1 VALUE 123.45.
* COMP-2 - double precision float (8 bytes)
01 WS-COMP2 COMP-2 VALUE 123.456789.
01 WS-RESULT PIC 9(10) VALUE 0.
PROCEDURE DIVISION.
ADD WS-DISPLAY TO WS-RESULT.
DISPLAY "DISPLAY + RESULT: " WS-RESULT.
ADD WS-COMP TO WS-RESULT.
DISPLAY "COMP + RESULT: " WS-RESULT.
ADD WS-COMP3 TO WS-RESULT.
DISPLAY "COMP-3 + RESULT: " WS-RESULT.
DISPLAY "COMP-1 VALUE: " WS-COMP1.
DISPLAY "COMP-2 VALUE: " WS-COMP2.
STOP RUN.
Explanation:
COMP=binary storage, COMP-3=packed decimal (efficient), COMP-1/2=floating point.