Entering content frameArithmetic Calculations Locate the document in its SAP Library structure

Arithmetic calculations use arithmetic operations and special ABAP statements.

Basic Arithmetic Operations

ABAP supports the four basic arithmetic operations and power calculations. You can use the following arithmetic operators in mathematical expressions:

Operator

Meaning

+

Addition

-

Subtraction

*

Multiplication

/

Division

DIV

Integer division

MOD

Remainder of integer division

**

Powers

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 the different ways of expressing 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>.

---

Powers

<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 interpreted as words in ABAP 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 /. DIV calculates the integer quotient, MOD calculates the remainder.

When you combine arithmetic expressions, ABAP performs the calculation from left to right, with one exception: Powers are calculates from right to left. So <n> ** <m> ** <p> is the same as <n> ** ( <m> ** <p> ), not ( <n> ** <m> ) ** <p>.

Example

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.

Example

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.

Arithmetic Calculations Using Structures

In the same way that you can transfer values component by component between structures using the MOVE-CORRESPONDING statement, you can also perform arithmetic operations between the components of structures using the following statements:

ABAP performs the corresponding calculation for all components that have the same name in both structures. However, it only makes sense to use the operations if all of the components involved have a numeric data type.

Example

DATA: BEGIN OF RATE,
USA TYPE F VALUE '0.6667',
FRG TYPE F VALUE '1.0',
AUT TYPE F VALUE '7.0',
END OF RATE.

DATA: BEGIN OF MONEY,
USA TYPE I VALUE 100,
FRG TYPE I VALUE 200,
AUT TYPE I VALUE 300,
END OF MONEY.

MULTIPLY-CORRESPONDING MONEY BY RATE.

WRITE / MONEY-USA.
WRITE / MONEY-FRG.
WRITE / MONEY-AUT.

The output appears as follows:

        67

       200

     2,100

Here, MONEY-USA is multiplied by RATE-USA and so on.

Adding Sequences of Fields

There are variants of the ADD statement that allow you to add sequences of fields in memory. For example:

Adding sequences of fields and assigning the result to another field

ADD <n1> THEN <n 2> UNTIL <n z> GIVING <m>.

If <n 1 >, <n 2 >, ... , <n z > is a sequence of equidistant fields of the same type and length in memory, they are summed and the result is assigned to <m>.

Adding sequences of fields and adding the result to the contents of another field

ADD <n1> THEN <n 2> UNTIL <n z> TO <m>.

This statement is identical to the preceding one, with one difference: The sum of the fields is added to the existing contents of <m>.

For further information about other similar variants, see the keyword documentation for the ADD statement.

Example

DATA: BEGIN OF SERIES,
N1 TYPE I VALUE 10,
N2 TYPE I VALUE 20,
N3 TYPE I VALUE 30,
N4 TYPE I VALUE 40,
N5 TYPE I VALUE 50,
N6 TYPE I VALUE 60,
END OF SERIES.

DATA SUM TYPE I.

ADD SERIES-N1 THEN SERIES-N2 UNTIL SERIES-N5 GIVING SUM.
WRITE SUM.

ADD SERIES-N2 THEN SERIES-N3 UNTIL SERIES-N6 TO SUM.
WRITE / SUM.

Output

       150

       350

Here, the contents of components N1 to N5 are summed and assigned to the field SUM. Then, the contents of components N2 to N6 are summed and added to the value of SUM.

 

 

 

Leaving content frame