Sunday, September 8, 2019

COMP, COMP1, COMP2 and COMP3 IN COBOL

COMP:
COMP usage clause will be called as BINARY or COMPUTATION. COMP usage clause applicable to Numeric data type only. COMP usage is a binary representation of data. The data in COMP variables stored memory in pure binary format. The memory allocation for COMP USAGE is like below.
PictureNumber of Bytes
S9 to S9(4)2
S9(5) to S9(9)4
S9(9) to S9(18)8

Let’s take an example for displaying all data types which are passing through program and see how it is after writing to the program.

      ID DIVISION.
       PROGRAM-ID. COM1.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 A PIC S9(8) USAGE COMP.
       PROCEDURE DIVISION.
           INITIALIZE A
           MOVE '1234567' TO A.
           DISPLAY A.
           DISPLAY 'LENGTH ' LENGTH OF A.
           STOP RUN.
COMP-1:
COMP-1 usage clause will be called as Floating point. COMP-1 usage clause is applicable to single floating point data type. COMP-1 is specified for internal floating point items which is single precision. COMP-1 items are 4 bytes long. PICTURE clause should not be specified. COMP-1 allows Signed floating point item where the sign will store on leftmost byte first bit which is allocated to exponent. In the 4 bytes, leftmost 8 bits to store exponent and remaining 24 bits stores mantissa. The COMP-1 memory calculations like below.

PictureNumber of Bytes
9(16)- 1 word4
COMP-1 will store the data in the format of mantissa and exponent. Here Mantissa is numeric value and exponent is the precision number.

       ID DIVISION.
       PROGRAM-ID. COM1.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 A USAGE COMP-1.
       PROCEDURE DIVISION.
           INITIALIZE A
           MOVE '1234567' TO A.
           DISPLAY A.
           DISPLAY 'LENGTH ' LENGTH OF A.
           STOP RUN.
COMP-2:
COMP-2 usage clause will be called as Hexadecimal form. COMP-2 usage clause applicable to double floating point data type. COMP-2 is specified for internal floating point items which is double precision. COMP-2 items are 8 bytes long. PICTURE clause should not be specified. COMP-2 allows Signed floating point item where the sign will store on leftmost byte first bit which is allocated to exponent. In the 8 bytes, leftmost 12 bits to store exponent and remaining 52 bits stores mantissa. The COMP-2 memory calculations like below. 

PictureNumber of Bytes
9(32)- 2 word8
COMP-2 will store the data in the format of mantissa and exponent. Here Mantissa is numeric value and exponent is the precision number.
       ID DIVISION.
       PROGRAM-ID. COM2.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 A USAGE COMP-2.
       PROCEDURE DIVISION.
           INITIALIZE A
           MOVE '1234567' TO A.
           DISPLAY A.
           DISPLAY 'LENGTH ' LENGTH OF A.
           STOP RUN.
COMP-3:
COMP-3 usage clause will be called as packed decimal form. COMP-3 usage clause is applicable for numeric data type. COMP-3 will store the data as packed. i.e. two digits in each byte. COMP-3 data will be stored in memory as BCD (Binary Coded Decimal) Format. COMP-3 data stored in memory higher to lower in the size of nibble (4 bits). i.e. The upper nibble stores the most significant digit and lower nibble stores the next digit and the upper nibble stores the next digit etc,. COMP-3 allows Signed packed decimal item where the sign will store on least significant digit (rightmost lower nibble). In COMP-3 Sign always table one nibble so always even number of nibbles allocated.

PictureNumber of Bytes
9½ Byte
The formula for memory calculation of the COMP-3 data item which declared with N bytes. PICTURE clause required for COMP-3 declaration.

       ID DIVISION.
       PROGRAM-ID. COM3.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 A PIC 9(4)V9(2) USAGE COMP-3.
       01 B PIC 9(4).9(2).
       PROCEDURE DIVISION.
           INITIALIZE A
           MOVE 1234.67 TO A.
           MOVE A TO B.
           DISPLAY A.
           DISPLAY 'LENGTH ' LENGTH OF A.
           DISPLAY B.
           DISPLAY 'LENGTH ' LENGTH OF B.
           STOP RUN.

No comments:

Post a Comment