ABAP Keyword Documentation → ABAP - Reference → Processing Internal Data → Numeric Calculations → arith_exp - Arithmetic Expressions → arith_exp - Lossless Calculations
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: number TYPE i VALUE 3,
result TYPE decfloat34,
exc TYPE REF TO cx_sy_conversion_rounding.
cl_demo_input=>request( CHANGING field = number ).
cl_demo_output=>begin_section(
|{ number } / div vs. { number } * ( 1 / div )| ).
DO 100 TIMES.
APPEND INITIAL LINE TO output.
output[ sy-index ]-div = sy-index.
TRY.
result = EXACT #( number / sy-index ).
output[ sy-index ]-result1 = result.
output[ sy-index ]-flag1 = `X`.
CATCH cx_sy_conversion_rounding INTO exc.
output[ sy-index ]-result1 = exc->value.
output[ sy-index ]-flag1 = ` `.
ENDTRY.
TRY.
result = EXACT #( number * ( 1 / sy-index ) ).
output[ sy-index ]-result2 = result.
output[ sy-index ]-flag2 = `X`.
CATCH cx_sy_conversion_rounding INTO exc.
output[ sy-index ]-result2 = exc->value.
output[ sy-index ]-flag2 = ` `.
ENDTRY.
ENDDO.
cl_demo_output=>display( output ).
Description
The lossless operator EXACT
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 )
If rounding is not needed, the result result
is produced. If roundings are
needed, the associated exception CX_SY_CONVERSION_ROUNDING is caught and its attribute VALUE is displayed. The results of lossless calculations are flagged in the display.
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).