COMP-1 | COMPUTATION-1


Tip! COMP-1 is applicable to the Numeric Data type.

COMP-1 is a USAGE type used to store single-precision (32 bit) floating-point numbers. COMP-1 allows a VALUE clause with a signed floating-point.

Storage Size and Format -

COMP-1 has no PICTURE clause, which is 4 bytes long (FULL WORD). COMP-1 data is stored in the format of mantissa and exponent. The mantissa is the numeric value, and the exponent is the precision number. In the 4 bytes, sign stores the leftmost first bit, exponent stores in the next 8 bits, and mantissa the remaining 23 bits stores.

Digits in PICTURE clause Storage occupied
1 through 16 4 bytes (FULL WORD)

Using -

Floating-point numbers are real numbers - For example -, 9.99, 99, etc.

Definition in a COBOL Program -

To define a COMP-1 variable in COBOL program, we use the USAGE IS COMP-1 clause in the data division. For example -

01 WS-FPN     USAGE IS COMP-1.

In this example, WS-FPN is a variable holding a single-precision floating-point number using 4 bytes of storage.

Assigning Values to a COMP-1 Variable -

We can assign values to a COMP-1 variable like any other numeric variable in COBOL. For example -

MOVE 9.999 TO WS-FPN.

9.999 value stores in a variable that is declared as COMP-1. The data is stored in the memory like .9999 * 10E 1. 9.999 equal to .9999 * 10E 1. In the above, 1 is the exponent value (stored in the most 8 bits), and .9999 is mantissa (stored in the remaining 23 bits).

Arithmetic Operations -

We can perform arithmetic operations (add, subtract, multiply, divide, etc.) on COMP-1 variables just like any other numeric variables. For example -

ADD 10.5 TO WS-FPN.

Range of Values -

COMP-1 can represent values in the approximate range of -1.18 x 10^-38 (-2,147,483,648) to +3.4 x 10^38 (+2,147,483,648).

Practical Example -


Scenario - Defining and initializing COMP-1 variables and using them for calculations and display.

Code -

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

       DATA DIVISION. 
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-PI           USAGE IS COMP-1.
          05 WS-RADIUS       USAGE IS COMP-1. 
          05 WS-AREA         USAGE IS COMP-1. 

       PROCEDURE DIVISION. 

           MOVE 3.1415918          TO WS-PI. 
           MOVE 10                 TO WS-RADIUS.  
           COMPUTE WS-AREA = WS-PI * (WS-RADIUS ** 2). 

           DISPLAY "THE AREA OF THE CIRCLE: " WS-AREA.
           STOP RUN.

Output -

THE AREA OF THE CIRCLE:  .31415918E 03