arith_exp - Calculation type
A calculation type is assigned to an arithmetic expression. The calculation type corresponds to one
of the three numeric data types i,
p, or f. With one exception, this is determined by the involved data type that has the largest value range (see the table of
value ranges of predefined ABAP types). The value range
of the data type f is greater than that of the data type
p, which is greater than the value range of
i. If the operator ** is used, the calculation type is always f, regardless of the data types of the operators.
Involved data types include the data types of all operands of the whole arithmetic expression, and the
data types of the result field result. For operands with numeric data types, this data type is used. Operands of other data types are handled as follows:
For operands that are specified as predefined functions or
functional methods, the data type of the return value applies.
Before the calculation, all operands are converted to the calculation type according to the
conversion rules for elementary data types. After the calculation,
the result of the arithmetic expression is converted to the data type of the result field, if these are of different calculation 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 one division) is rounded up to an integer. Each intermediate result must be in the value range of the data type
i (see the table of value ranges for predefined data types). If it is not, a treatable exception occurs.
- Calculation type p
The arithmetic expression is calculated to an internal accuracy of 31
decimal places and using a specific
decimal floating point arithmetic for intermediate results. During the calculation, the decimal point
is not fixed in numbers of type p. If there is an overflow
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
a further overflow occurs, this leads to a treatable exception. An overflow always occurs if the level of accuracy is not sufficient for all decimal places before the decimal separator. Surplus
fractional portions do not generate an exception, but are rounded up for every 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 as the digits in an integer, and each intermediate result is rounded up to an integer.
- Calculation type f
The arithmetic
expression is calculated using the standard 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 a treatable exception. As the fractional portions of a floating
point number are displayed internally using a dual fraction, there is not an exact equivalent for every number that can be displayed in the decimal system. This can lead to rounding errors in the intermediate results (see also
class for mathematical operators).
Note
Determination of a calculation type before the calculation is executed, taking all operands including
the result field into account, is a feature of ABAP that differs considerably from the way in which
other programming languages perform calculations. To avoid unnecessary conversions, if possible, all
operands and the result field should have the same numeric data type. Another difference is the decimal rounding of intermediate results in the calculation type
i. In other programming languages, the decimal places are simply cut off.