Summary -

In this topic, we described about the Comp-3/Computation-3/Packed-decimal with detailed example.

Specifies for internal decimal items. COMP-3 is the equivalent of PACKED-DECIMAL. COMP-3 usage clause is applicable for numeric data type.

If an item declared as COMP-3, the item appears in storage in packed decimal format. The right most half byte contains the sign value.

COMP-3 data item contains any of the digits 0 through 9, a sign. COMP-3 can have a value not exceeding 18 decimal digits.

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 stores on least significant digit (rightmost lower nibble).

The sign representation uses the same bit configuration as the 4-bit sign representation in zoned decimal fields.

Below table represents the storage occupation based on the number of digits in PICTURE clause –

Digits in PICTURE clause Storage occupied
1 digit ½ Byte

The formula for memory calculation of the COMP-3 with n digits is -

No. of bytes = Round ((n + 1)/2) -- Where n is the size used in declaration. PICTURE clause required for COMP-3 declaration.

Practicle Example -

Code:


 IDENTIFICATION DIVISION.                                         
 PROGRAM-ID. COMP3.                                              
 ENVIRONMENT DIVISION.                                            
 DATA DIVISION.                                                   
 WORKING-STORAGE SECTION.                                         
 01 COMP3-NM1             PIC 9(04) USAGE COMP-3 VALUE '2561'.    
 01 COMP3-NM2             PIC S9(04) USAGE COMP-3 VALUE '2561'.   
 PROCEDURE DIVISION.                                              
     DISPLAY 'COMPUTATION-3..'.                                   
     DISPLAY 'COMPUTATION3 - NUMERIC [9(04)-> 2 BYTES]'.          
     DISPLAY COMP3-NM1.                                           
     DISPLAY 'COMPUTATION3 - NUMERIC [S9(04)-> 3 BYTES]'.         
     DISPLAY COMP3-NM2.                                           
     STOP RUN.