Basic Arithmetic Operations

ABAP supports the four basic arithmetic operations, as well as power calculation. You can specify the following arithmetic operators in a mathematical expression:

+

Addition

-

Subtraction

*

Multiplication

/

Division

DIV

Integer division

MOD

Remainder of integer division

**

Exponentiation

Instead of using operators in mathematical expressions, you can perform basic arithmetic operations with the keywords ADD, SUBTRACT, MULTIPLY, and DIVIDE.

The following table shows how you can express the basic arithmetic operations in ABAP:

Operation

Statement using
mathematical expression

Statement using
Keyword

Addition

<p> = <n> + <m>.

ADD <n> TO <m>.

Subtraction

<p> = <m> - <n>.

SUBTRACT <n> FROM <m>.

Multiplication

<p> = <m> * <n>.

MULTIPLY <m> BY <n>.

Division

<p> = <m> / <n>.

DIVIDE <m> BY <n>.

Integer division

<p> = <m> DIV <n>.

---

Remainder of division

<p> = <m> MOD <n>.

---

Exponentiation

<p> = <m> ** <n>.

---

In the statements using keywords, the results of the operations are assigned to field <m>.

The operands <m>, <n>, <p> can be any numeric fields. If the fields are not of the same data type, the system converts all fields into the hierarchically highest data type that occurs in the statement.

When using mathematical expressions, please note that the operators +, -, *, **, and /, as well as opening and closing parentheses, are ABAP words and must therefore be preceded and followed by blanks.

In division operations, the divisor cannot be zero if the dividend is not zero. With integer division, you use the operators DIV or MOD instead of /. Use DIV to obtain the integer quotient and MOD to obtain the remainder.

If you combine several mathematical expressions together, calculations are performed from left to right for operators of equal priority, except in the case of exponentiation which is performed from right to left. Therefore, <n> ** <m> ** <p> is the same as <n> ** ( <m> ** <p>) and not the same as ( <n> ** <m>) ** <p>

DATA: COUNTER TYPE I.

COMPUTE COUNTER = COUNTER + 1.

COUNTER = COUNTER + 1.

ADD 1 TO COUNTER.

Here, the three operational statements perform the same arithmetic operation, i.e. adding 1 to the contents of the field COUNTER and assigning the result to COUNTER.

DATA: PACK TYPE P DECIMALS 4,
N TYPE F VALUE '+5.2',
M TYPE F VALUE '+1.1'.

PACK = N / M.
WRITE PACK.

PACK = N DIV M.
WRITE / PACK.

PACK = N MOD M.
WRITE /PACK.

The output appears as follows:

           4.7273

           4.0000

           0.8000

This example shows the different types of division.