Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Numerical Calculations →  arith_exp - Arithmetic Expressions 

arith_exp - Calculation Type

An arithmetic expression is assigned a calculation type that specifies how the arithmetic expression is to be handled.

Other versions: 7.31 | 7.40 | 7.54

Determining the Calculation Type

The calculation type corresponds to one of the numeric data types i, p, f, or decfloat34. It is determined according to the following hierarchy, and in this order of priority:

  • If one of the data types involved is decfloat16 or decfloat34, the calculation type is decfloat34.
  • If one of the data types involved is f or if the operator ** is used, the calculation type is f.
  • If one of the data types involved is p, the calculation type is p.
  • If one of the data types involved is i, (b or s), the calculation type is i.
  • The data type that has the largest value range therefore determines the calculation type. An exception to this is that each decimal floating point number results in the calculation type decfloat34, and that using the operator ** is handled like a data type of type f.

    Involved data types include:

    • When used in the COMPUTE statement, the data types of all operands of the whole arithmetic expression, and the data type of the result field result.
    • When used in logical expressions, the data types of all operands of the whole logical expression. The approach taken does not involve first determining the calculation type of each arithmetic expression involved and then deriving the comparison type. All operands of the logical expression are used to determine the calculation type (which is also the comparison type) regardless of the arithmetic expressions they belong to.
    • When used as actual parameters for input parameters of methods, the data types of all operands of the whole arithmetic expression and the typing of the formal parameter, provided that it is fully typed. If the formal parameter is typed generically, then only the operands of the expression are evaluated.

    For operands with numeric data types, this data type is used. Operands with other data types are handled as follows:

    • d and t as i
    • c, n, and string as p
    • x and xstring as i
    • For operands that are specified as non-overloaded integrated functions, the data type of the return value applies.
    • For operands that are specified as functional methods, the data type of the return value applies.


    Notes

    • Determining a calculation type before performing the calculation and by taking into consideration all operands including the result field (when used in COMPUTE or as an actual parameter) is a special ABAP feature that differs considerably from the way in which other programming languages perform calculations. To avoid unnecessary conversions, all operands and one result field should have the same numeric data type. Another difference is that intermediate results are rounded to whole numbers for calculation type i; in other programming languages, the decimal places are simply removed.

    • With calcualtion type f, you need to make sure than the accuracy is approximately 15 decimal places and that whole numbers are only represented accurately up to an absolute value of 2**53 (that is, 9.007.199.254.740.992). Other intermediate results are rounded. This is especially important when using the operator **, which can lead to calculation type f if no decimal floating point number is involved.

    Meaning of the Calculation Type

    Before the calculation is made, all operands (where this is required) are converted to the calculation type, in accordance with the conversion rules for elementary data types.

    The calculation type determines the calculation method and the calculation accuracy:

    • Calculation type i
      The arithmetic expression is calculated using integer arithmetic, in which each intermediate result that is not an integer (after a division) is rounded to the nearest integer. Each intermediate result must fall within the value range of the data type i, otherwise this raises the handleable exception CX_SY_ARITHMETIC_OVERFLOW.
    • Calculation type p
      The arithmetic expression is calculated to an internal accuracy of 31 decimal places and using a special decimal floating point arithmetic for intermediate results. During the calculation, the decimal point for numbers of type p is not fixed. If an overflow occurs because an intermediate result is greater than 10^31 - 1, the whole expression is recalculated to an internal accuracy of 63 decimal places, or a maximum value of 10^63 -1 for intermediate results. If another overflow occurs, this triggers the treatable exception CX_SY_ARITHMETIC_OVERFLOW. An overflow always occurs if the level of accuracy is not sufficient for all decimal places before the decimal separator. Surplus decimal places do not generate an exception, but are rounded to the nearest whole number for each intermediate result. If the program attribute fixed point arithmetic is not set, the decimal separator is not taken into account in the calculation. All decimal places are treated like the digits of an integer, and each intermediate result is rounded to the nearest integer.
    • Calculation type f
      The arithmetic expression is calculated using the binary floating point arithmetic of the current platform. Each intermediate result must fall within the value range of the data type f, otherwise this leads to the treatable exception CX_SY_ARITHMETIC_OVERFLOW. Since the decimal places of a floating point number are represented internally using a dual fraction, there is not an exact equivalent for every number that can be represented in the decimal system. This can result in rounding errors in the intermediate results.
    • Calculation type decfloat34
      The arithmetic expression is calculated with decimal floating point arithmetic according to norm IEEE-754-2008, where (as for all calculation types) division by 0 does not result in an exception if the dividend is 0. Each intermediate result must fall within the value range of the data type decfloat34, otherwise this leads to the treatable exception CX_SY_ARITHMETIC_OVERFLOW. Intermediate results are rounded to the nearest whole number. The scaling of each intermediate result is defined such that the smallest possible exponent is selected if the result is not precise, and the following procedure is used to determine the scaling if the result is precise:
    • For addition and subtraction, the exponent of the result is the smaller of the exponents of the two operands.
    • For multiplication, the exponent of the result is the sum of the exponents of the two operands.
    • For division, the exponent of the result is the difference between the exponents of the dividend and of the divisor.
    • If you use the sqrt function, the exponent is the integer part of half of the exponent of the operand.


    Notes

    The conversion of non-numeric data types to a numeric calculation type allows you to perform calculations with date and time fields. This is because their content is converted into days or seconds in accordance with the conversion rules. Adding values and determining differences also play a part here.

    Handling the Result

    After the calculation is performed, the result of the arithmetic expression is handled as follows:

    • When used in the COMPUTE statement, the result is converted to the data type of the result field if the calculation type is different.
    • When used in logical expressions, no conversion is performed because calculation and comparison type are identical.
    • When the result is used as an actual parameter for input values of methods, and if the formal parameter is fully typed, the result is converted into the formal parameter type in the static method call and passed. If the formal parameter is generically typed, it takes on the calculation type; in the case of calculation type p, the number of decimal places is determined based on the accuracy required in the calculation.