COMP-2 | COMPUTATION-2
COMP-2 is a USAGE type that stores numbers as internal double-precision (64-bit) floating-point. It allows a VALUE clause with a signed floating-point.
Storage Size and Format -
COMP-2 has no PICTURE clause and is 8 bytes long (DOUBLE WORD). COMP-2 data is stored in the format of mantissa and exponent.
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 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.
The 9.999 value is stored in a variable declared COMP-2. The data is stored in memory like .9999 * 10E 1. 9.999 is equal to .9999 * 10E 1. In the above, 1 is the exponent value, and .9999 is the mantissa.
Arithmetic Operations -
Like other numeric variables, we can perform all arithmetic operations (add, subtract, multiply, divide, etc.) on COMP-2 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, initializing, its usage and display of COMP-2 variables.
Code -
----+----1----+----2----+----3----+----4----+----5----+
...
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.
...
Output -
THE AREA OF THE CIRCLE: .31415926999999994E 03