Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Numerical Calculations →  Statements for Numerical Calculations →  COMPUTE 

Floating Point Numbers, Arithmetic Calculations

The example demonstrates arithmetic calculations with floating point numbers.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA exrnd TYPE REF TO cx_sy_conversion_rounding .
<span class="blue">*   Compute decfloat34</span>
    TRY.
        CASE operator.
          WHEN ' '.
            COMPUTE EXACT df34_r = df34_1 .
          WHEN '+'.
            COMPUTE EXACT df34_r = df34_1 + df34_2.
          WHEN '-'.
            COMPUTE EXACT df34_r = df34_1 - df34_2.
          WHEN '*'.
            COMPUTE EXACT df34_r = df34_1 * df34_2.
          WHEN '/'.
            COMPUTE EXACT df34_r = 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 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">*   Compute decfloat16</span>
    TRY.
        CASE operator.
          WHEN ' '.
            COMPUTE EXACT df16_r = df34_1.
          WHEN '+'.
            COMPUTE EXACT df16_r = df34_1 + df34_2.
          WHEN '-'.
            COMPUTE EXACT df16_r = df34_1 - df34_2.
          WHEN '*'.
            COMPUTE EXACT df16_r = df34_1 * df34_2.
          WHEN '/'.
            COMPUTE EXACT df16_r = 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">*   Compute 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 EXACT addition of the COMPUTE statement.