ABAP - Keyword Documentation →  ABAP - Programming Language →  Processing Internal Data →  Numeric Calculations →  Arithmetic Expressions (arith_exp) → 
Mail Feedback

arith_exp - Arithmetic Operators

The table shows the possible arithmetic operators for arithmetic expressions, their priority, and the order in which the calculation is performed. Within a parenthesis level, calculations with operators of higher priority are performed before calculations with operators of lower priority. For adjacent operators of the same priority, the calculation is performed in the order specified. In the third column of the table below, 3 indicates the highest priority and 1 the lowest.

The calculation is performed according to the calculation rule for the current calculation type. With the exception of **, the operators have no effect on the calculation type. If the operator ** does not occur in an arithmetic expression, the calculation type is determined only by the data types involved. If ** is used, the calculation type is either decfloat34 or f. It is decfloat34 if one of the operands involved is a decimal floating point number and f in all other cases.

Operator Calculation Priority Order
+ Addition of the operands 1 From left to right
- Subtraction of the right operand from the left 1 From left to right
* Multiplication of the operands 2 From left to right
/ Division of the left operand by the right 2 From left to right
DIV Integer part of the division of the left operand by the right, with positive remainder 2 From left to right
MOD Positive remainder of the division of the left operand by the right; a remainder other than zero is always between zero and the size of the right operand 2 From left to right
** Left operand raised to the power of the right 3 From right to left

Division by the value 0 is undefined and raises a catchable exception. The only situation where division by 0 does not raise an exception is if the dividend is also 0. Here, the result is set to 0.

Programming Guideline

Prevent division by zero

Hints

Example

This example demonstrates the functions of the three division operators, DIV, MOD, and /. The results are 4.7273, 4.0000, and 0.8000.

DATA: pack TYPE p DECIMALS 4,
      n    TYPE decfloat34 VALUE '+5.2',
      m    TYPE decfloat34 VALUE '+1.1'.

pack = n / m.
cl_demo_output=>write( |{ n } / { m } = { pack }| ).

pack = n DIV m.
cl_demo_output=>write( |{ n } DIV { m } = { pack }| ).

pack = n MOD m.
cl_demo_output=>display( |{ n } MOD { m } = { pack }| ).

Example

The following table shows the results of integer divisions and their remainders. See also the example for the SQL operators DIV and MOD.

operand1 operand2 DIV MOD
7 3 2 1
-7 3 -3 2
7 -3 -2 1
-7 -3 3 2

Executable Example

Arithmetic Calculations

Executable Example

Floating Point Numbers, Arithmetic Calculations



Continue
Example arith_exp - Calculations
Example arith_exp - Floating Point Numbers