COMP-2 | COMPUTATION-2
COMP-2 is a USAGE type used to store internal double-precision (64 bit) floating-point numbers. COMP-2 allows a VALUE clause with a signed floating-point.
Storage Size and Format -
COMP-2 has no PICTURE clause, and it is 8 bytes long (DOUBLE WORD). COMP-2 data value stored in the format of mantissa and exponent. The mantissa is the numeric value, and the exponent is the precision number. In the 8 bytes, the sign stores in the leftmost first byte, the exponent stores in the next 11 bits, and the mantissa stores in the remaining 52 bits.
Digits in PICTURE clause | Storage occupied |
---|---|
1 through 32 | 8 bytes (DOUBLE WORD) |
Using -
Floating-point numbers are real numbers - For example -, 9.99, 99, etc.
Definition in a COBOL Program -
To define a COMP-2 variable in your COBOL program, we use the USAGE IS COMP-2 clause in the DATA DIVISION. For example -
01 WS-DFPN USAGE IS COMP-2.
In this example, WS-DFPN is a variable holding a double-precision floating-point number using 8 bytes of storage.
Assigning Values to a COMP-2 Variable -
We can assign values to a COMP-2 variable like any other numeric variable in COBOL. For example -
MOVE 9.999 TO WS-DFPN.
9.999 value stores in a variable that is declared as COMP-2. 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 leftmost 8 bits), and .9999 is mantissa (stored in the remaining 55 bits).
Arithmetic Operations -
We can perform arithmetic operations (add, subtract, multiply, divide, etc.) on COMP-2 variables just like any other numeric variables. For example -
ADD 10.5 TO WS-DFPN.
Range of Values -
COMP-2 can represent values in the approximate range of ±2.23 x 10^-308 to ±1.80 x 10^308.
Practical Example -
Scenario - Defining and initializing COMP-2 variables and using them for calculations and display.
Code -
----+----1----+----2----+----3----+----4----+----5----+
IDENTIFICATION DIVISION.
PROGRAM-ID. COMP2.
AUTHOR. MTH.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-PI USAGE IS COMP-2.
05 WS-RADIUS USAGE IS COMP-2.
05 WS-AREA USAGE IS COMP-2.
PROCEDURE DIVISION.
MOVE 3.1415927 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: .31415926999999994E 03