COBOL Manual for TNS and TNS/R Programs

Procedure Division Verbs
HP COBOL Manual for TNS and TNS/R Programs522555-006
9-43
COMPUTE
This statement specifies that the result be stored in COMPUTE-RESULT without being
rounded:
COMPUTE COMPUTE-RESULT =
(((24.0 + 1) * (60 - 10)) / 125) ** 2
END-COMPUTE
(The result is 100.)
This COMPUTE statement specifies that the result be rounded and then stored in WS-
RESULT:
MOVE 2 TO EXPONENT
COMPUTE WS-RESULT ROUNDED =
WS-99 / 10 * WS-99 ** EXPONENT + WS-FIVE-ONES
END-COMPUTE
(The result is 108,141.)
Example 9-10. Combination of IF and COMPUTE Statements
IF A > 0
COMPUTE X = B ** 2 - ( 4 * A * C ) / ( 2 * A)
ON SIZE ERROR
MOVE "DIVISION ERROR" TO DIAGNOSTIC-FIELD
END-COMPUTE
ELSE
MOVE "DIVISION BY ZERO" TO DIAGNOSTIC-FIELD
END-IF.
Example 9-9. COMPUTE Statement (page 2 of 2)