ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Numeric 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. The calculation type is determined at program runtime from those data types involved that can be identified. The calculation type only needs to be determined statically if the target field is an inline declaration, where generically typed operands are included using a standard type.

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 produces the calculation type `decfloat34`, and that using the operator `**` is handled like a data type of type `f`.

Involved data types include:

• as the right side of an assignment in an arithmetic expression, the data types of all operands of the whole arithmetic expression and the data type of the result field `result`, if not an inline declaration `DATA(var)`. If `result` is an inline declaration, the calculation type derived from the data types of the arithmetic expression determines the data type of the declared variable.
• as the operand of a relational expression in an arithmetic expression, the data types of all operands of the entire relational 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 relational expression are used to determine the calculation type (which is also the comparison type) regardless of the arithmetic expressions they belong to.
• in an arithmetic expression after the statement `CASE`, the operands of the arithmetic expression, ignoring the operands specified after `WHEN`.
• as actual parameters for input parameters of methods in an arithmetic expression, 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.
• as an argument of a predefined function in an arithmetic expression, the data types of all operands of the entire arithmetic expression and the type of the input parameter, if determined in full. An overloaded numeric function containing an arithmetic expression as an argument, on the other hand, operates like an arithmetic operator and is handled in its position like an arithmetic expression.
• as an argument in a constructor expression in an arithmetic expression, the calculation type is determined as in an assignment. The left side of the assignment is either the result of the constructor expression with the type `type` specified in front of the parentheses or a structure component.

For operands with complete 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`

In the case of operands not specified as data objects, the type is determined as follows:

• In the case of operands specified as non-overloaded integrated functions, the data type of the return value applies.
• When a floating point function is used, the calculation type is either `decfloat34` or `f`.
• In the case of operands specified as constructor expressions, the data type specified by `type` here applies.
• In the case of operands specified as table expressions, the data type of the result applies.
• If operands are specified as generically typed field symbols or formal parameters and an existing variable is used as the target field of an assignment, then these operands (together with the data type) contribute to the calculation type assigned to them at runtime.
• If operands are specified as generically typed field symbols or formal parameters and an inline declaration `DATA(var)` is used as the target field of an assignment, the generic types contribute to the statically detectable calculation type (used to determine the data type of the declaration) as follows.
• `any`, `data`, `simple`, `numeric`, and `decfloat` like `decfloat34`
• `csequence`, `clike`, `c`, `n`, and `p` like `p`. If no type with a higher priority is involved, the type `p` with length 8 (no decimal places) is used for the declaration.
• `xsequence` and `x` like `i`
The calculation type determined at runtime from all operands (including the target field) always matches the data type of the variable declared inline, because this is always the data type with the greatest value range.

Notes

• Determining a calculation type before performing the calculation and by taking into consideration all operands including the result field (in the case of an arithmetic expression, as the right side of an assignment 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.

• In the case of arithmetic expressions used as operands of relational expressions in logical expressions joined using Boolean operators, the rule above only ever applies to the relational expression in question. The calculation type is not determined while skipping the Boolean operators `AND`, `OR`, or `EQUIV`.

• If the calculation type is `f`, the accuracy is only approximately 15 decimal places and 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.

• To stop the operator `**` producing the calculation type `f`, the predefined function `ipow` can be used for integer exponents. Here, the calculation type is determined by the argument.

• The conversion operator `CONV` can be used to convert parts of arithmetic expressions to other types and hence modify the calculation type.

## 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`
Fixed point arithmetic 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, the handleable exception CX_SY_ARITHMETIC_OVERFLOW is raised. 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 raise an exception, but are rounded to the nearest whole number for each intermediate result.
• 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 the handleable exception CX_SY_ARITHMETIC_OVERFLOW is raised. 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 raise an exception if the dividend is 0. Each intermediate result must fall within the value range of the data type `decfloat34`, otherwise the handleable exception CX_SY_ARITHMETIC_OVERFLOW is raised. 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 the `sqrt` function is used, the exponent is the integer part of half of the exponent of the operand.

## Handling the Result

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

• in the case of an arithmetic expression as the right side of an assignment statement, the result is converted to the data type of the result field if the calculation type is different.
• in the case of an arithmetic expression as an operand of a relational expression, no conversion takes place, since the calculation type and the comparison type are identical.
• in the case of an arithmetic expression 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.