Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Processing Internal Data →  Numeric Calculations →  arith_exp - Arithmetic Expressions →  arith_exp - Arithmetic Operators 

Floating Point Numbers, Arithmetic Calculations

The example demonstrates arithmetic calculations with floating point numbers.

Other versions: 7.31 | 7.40 | 7.54

Source Code

<span class="blue">*   Calculate decfloat34</span>
    TRY.
        CASE operator.
          WHEN ' '.
            df34_r = EXACT #( + df34_1 ).
          WHEN '+'.
            df34_r = EXACT #( df34_1 + df34_2 ).
          WHEN '-'.
            df34_r = EXACT #( df34_1 - df34_2 ).
          WHEN '*'.
            df34_r = EXACT #( df34_1 * df34_2 ).
          WHEN '/'.
            df34_r = EXACT #( df34_1 / df34_2 ).
          WHEN '**'.
            df34_r = df34_1 ** df34_2.
            CLEAR exct_34.
        ENDCASE.
      CATCH cx_sy_conversion_overflow.
        res_df34 = text-ove.
        CLEAR exct_34.
      CATCH cx_sy_arithmetic_overflow.
        res_df34 = text-ove.
        CLEAR exct_34.
      CATCH cx_sy_conversion_rounding INTO DATA(exrnd).
        df34_r = exrnd->value.
        exct_34 = text-noe.
    ENDTRY.
    IF res_df34 = ' '.
      res_df34 = |{ df34_r ALIGN = LEFT STYLE = SCALE_PRESERVING }|.
      continue_flag = 'X'.
    ENDIF.
<span class="blue">*   Calculate decfloat16</span>
    TRY.
        CASE operator.
          WHEN ' '.
            df16_r = EXACT #( df34_1 ).
          WHEN '+'.
            df16_r = EXACT #( df34_1 + df34_2 ).
          WHEN '-'.
            df16_r = EXACT #( df34_1 - df34_2 ).
          WHEN '*'.
            df16_r = EXACT #( df34_1 * df34_2 ).
          WHEN '/'.
            df16_r = EXACT #( df34_1 / df34_2 ).
          WHEN '**'.
            df16_r = df34_1 ** df34_2.
            CLEAR exct_16.
        ENDCASE.
      CATCH cx_sy_conversion_overflow.
        res_df16 = text-ove.
        CLEAR exct_16.
      CATCH cx_sy_arithmetic_overflow.
        res_df16 = text-ove.
        CLEAR exct_16.
      CATCH cx_sy_conversion_rounding INTO exrnd.
        df16_r = exrnd->value.
        exct_16 = text-noe.
    ENDTRY.
    IF res_df16 = ' '.
      res_df16 = |{ df16_r ALIGN = LEFT STYLE = SCALE_PRESERVING }|.
    ENDIF.
<span class="blue">*   Calculate type f</span>
    TRY.
        f1 = df34_1.
        f2 = df34_2.
        CASE operator.
          WHEN ' '.
            f_r = f1.
          WHEN '+'.
            f_r = f1 + f2.
          WHEN '-'.
            f_r = f1 - f2.
          WHEN '*'.
            f_r = f1 * f2.
          WHEN '/'.
            f_r = f1 / f2.
          WHEN '**'.
            f_r = f1 ** f2.
        ENDCASE.
      CATCH cx_sy_conversion_overflow.
        res_f = text-ove.
      CATCH cx_sy_arithmetic_overflow.
        res_f = text-ove.
    ENDTRY.
    IF res_f = ' '.
      res_f = |{ f_r ALIGN = LEFT }|.
    ENDIF.

Description

Two operands and various arithmetic operators can be entered. The calculation is performed for the data types decfloat34, decfloat16, and f. For decimal floating point numbers, the losslessness of the calculation is checked using the lossless operator EXACT.