ABAP Keyword Documentation → ABAP - Reference → Processing Internal Data → Numerical Calculations → Statements for Numerical Calculations → COMPUTE
Lossless Calculations
The example demonstrates lossless calculations and the exceptions raised when roundings occur.
Other versions: 7.31 | 7.40 | 7.54
Source Code
DATA: result TYPE decfloat34,
exc TYPE REF TO cx_sy_conversion_rounding.
WRITE: 15 number, `/ sy-index `,
56 number, `* ( 1 / sy-index )`.
ULINE.
DO 100 TIMES.
WRITE: / sy-index NO-GAP NO-SIGN, `:`.
TRY.
COMPUTE EXACT result = number / sy-index .
WRITE (40) result COLOR COL_POSITIVE INTENSIFIED OFF.
CATCH cx_sy_conversion_rounding INTO exc.
WRITE (40) exc->value COLOR COL_NEGATIVE INTENSIFIED OFF.
ENDTRY.
TRY.
COMPUTE EXACT result = number * ( 1 / sy-index ).
WRITE (40) result COLOR COL_POSITIVE INTENSIFIED OFF.
CATCH cx_sy_conversion_rounding INTO exc.
WRITE (40) exc->value COLOR COL_NEGATIVE INTENSIFIED OFF.
ENDTRY.
ENDDO.
Description
The addition EXACT
of the statement COMPUTE
makes
the following calculations and assigns the result to a data object with the type decfloat34
:
result = number / sy-index
result = number * ( 1 / sy-index )
The results of lossless calculations are displayed with a green background in the output list. If roundings occur, the associated exception CX_SY_CONVERSION_ROUNDING is caught and its attribute VALUE is displayed with a red background.
This example demonstrates how the structure of an arithmetic expression can influence the result of
a lossless calculation. For example, the division of the number number
by
itself is always a lossless calculation; multiplying number
by 1 / number
, however, raises an exception when a rounding occurs in the division.
The following two expressions are further examples where the structure of an arithmetic expression is an influence:
result = number * 1 / sy-index
result = 1 / sy-index * number
The first expression has an effect like number / sy-index
and the second like number * ( 1 / sy-index )
(again).