COMP-3 Example


Scenario - Defining, initializing, its usage and display of COMP-3 variables.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. COMP3.
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-WIDTH        PIC S9(02) USAGE IS COMP-3.
          05 WS-AREA         PIC S9(06) COMP-3.

       PROCEDURE DIVISION.

           MOVE 10            TO WS-WIDTH.
           COMPUTE WS-AREA =  WS-WIDTH ** 2.

           DISPLAY "LENGTH OF WS-AREA VARIABLE: "
                    LENGTH OF WS-AREA.
           DISPLAY "AREA OF SQUARE: " WS-AREA.
           STOP RUN.

Output -

LENGTH OF WS-AREA VARIABLE: 000000004
AREA OF SQUARE: 000100

Explaining Example -

WS-AREA variable is declared as COMP-3, with a signed byte plus 6 digits. A total of 7 bytes and the (n+1)/2 formula will apply as n is an odd number. So, a total of 8/2 = 4 bytes allocated for WS-AREA.