COMP-1 | COMPUTATION-1
COMP-1 stores the numbers in a single-precision (32-bit) floating-point format. It 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.
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 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, and .9999 is the mantissa.
Arithmetic Operations -
Like other numeric variables, we can perform all arithmetic operations (add, subtract, multiply, divide, etc.) on COMP-1 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 - Declaring, initializing, their usage, and display of COMP-1 variables.
Code -
----+----1----+----2----+----3----+----4----+----5----+
...
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.
...
Output -
THE AREA OF THE CIRCLE: .31415918E 03