Effect

The arithmetic expression arithexp is calculated, and the result
placed in field n.

You can use the four basic operators +, -, *, /, the integer
division operators DIV (Quotient) and MOD (remainder), the power
operator ** ("`X ** Y`" = `X` to the power of
`Y`), and the other functions listed below.

In mixed expressions, the functions have the highest precedence,
followed by the powers. Next are *, /, DIV, and MOD. + and - have
the lowest precedence. You may use any number of parentheses.

The fields that you use are normally types I, P or F . However, in
certain cases, this rule is not always observed (see below).

The keyword COMPUTE is optional.

**Built-in Functions**

- Functions for all number types

ABS Amount (absolute value) |x| von x

SIGN Sign of x;

1 x > 0

SIGN( x ) = 0 if x = 0

-1 x < 0

CEIL Smallest integer value that is not less than x

FLOOR Largest integer value that is not greater than x

TRUNC Interger part of x

FRAC Decimal part of x

- Floating Point Functions

ACOS Arccos(x) in range [-pi/2, pi/2], x from [-1, 1]

ASIN Arcsin(x) in range [0, pi], x from [-1, 1]

ATAN Arctan(x) in range [-pi/2, pi/2] (pi = 3.1415926535897932)

COS Cosine of an angle in radians

SIN Sine of an angle in radians

TAN Tangent of an angle in radians

COSH Hyperbolic cosine

SINH Hyperbolic sine

TANH Hyperbolic tangent

EXP Exponential function, based on e = 2.7182818284590452

LOG natural logarithm (based on e) of a positive number

LOG10 Logarithm of x, base 10, x > 0

SQRT Square root of a non-negative number

- String Functions

STRLEN Length of a string up to its last non-space character
(its **occupied length**).

Function expressions consist of three elements:

- Function name with opening parenthesis (no space between)

- Argument

- Closing parenthesis

All elements of an expression, especially all elements of a function expression, must be separated from each other by at least one space.

Example

The following statements and the calculations they contain are syntactically correct:

DATA: I1 TYPE I, I2 TYPE I, I3 TYPE I, F1 TYPE F, F2 TYPE F, WORD1(10), WORD2(20). ... F1 = ( I1 + EXP( F2 ) ) * I2 / SIN( 3 - I3 ). COMPUTE F1 = SQRT( SQRT( ( I1 + I2 ) * I3 ) + F2 ). I1 = STRLEN( WORD1 ) + STRLEN( WORD2 ).

Mathematical expressions are, by default, calculated using
packed numbers. However, if the expression contains a floating
point function or at least one operator with type F , or the result
field has type F, the entire expression is calculated using
floating point arithmetic. On the other hand, if only type I
fields, or date and time fields (see below) are used, the
expression is calculated using integer arithmetic.

If the expression contains mixed data types, the system converts
types as appropriate.

There is a difference in the performance of the various
calculation types. Greatly simplified, type I is the most
efficient, type F takes more time, and type P causes the greatest
system load.

If you use mixed expressions, the system must convert some of the
fields. This also adversely affects performance, especially when
converting from or to type P.

- Notes

You can also use fields in calculations, that do not have type
I, F or P, as long as their values can be interpreted as numbers.
The system automatically converts the types before calculating the
result ( ** MOVE**).
In particular, you can subtract one date field from another (type
D), to calculate the number of days between them. To aid
efficiency, you should, whenever possible, use operands with the
same data type (apart from operations with STRLEN ). The system
does not then have to convert any values, and it can optimize the
calculation internally.

- If you try to divide by zero, the calculation terminates,
except when the numerator is also 0 (
`0 / 0`) = 0).

- In a string processing command, STRLEN treats its operands as
character strings, regardless of their actual type, and without
converting them. Operands of floating point functions, on the other
hand, are always converted to type F if their original type is
different.

Example

Date and Time Arithmetic

DATA: DAYS TYPE I, DATE_FROM TYPE D VALUE '19911224', DATE_TO TYPE D VALUE '19920101'. DAYS = DATE_TO - DATE_FROM.

`DAYS` is now equal to 8.

DATA: SECONDS TYPE I, TIME_FROM TYPE T VALUE '200000', TIME_TO TYPE T VALUE '020000'. SECONDS = ( TIME_TO - TIME_FROM ) MOD 86400.

`SECONDS` has the value 21600 (6 hours). The "`MOD
86400`" operation in this example ensures that the result is
not negative, even if the time period spans midnight.

Note

Packed Number Arithmetic

All P fields are treated as integers without decimal places. You
have to program "decimal" arithmetic by multiplying or dividing by
10, 100, ... The DECIMALS addition to the DATA statement, only
affects the output using WRITE.

If the **fixed point arithmetic** is active, the
DECIMALS addition is also effective in the calculation. Any
intermediate results are then also calculated to the maximum
possible accuracy (up to 31 decimal places). This particularly
affects division.

SAP therefore recommends that you always set the "Fixed point
arithmetic" attribute in your programs.

Example

DATA P TYPE P. P = 1 / 3 * 3.

Without fixed point arithmentic, `P` now has the value 0,
since " `1 / 3`" is rounded to 0.

With fixed point arithmetic, `P` now has the value 1, since
" `1 / 3` " is stored in the system as
0.333333333333333333333333333333.

Note

Floating Point Arithmetic

When you use floating point arithmetic, you should always remember
that you will encounter inaccuracies due to rounding errors) ( ABAP
number types).

Note

Powers

The power expression "x**y" means that x is multiplied by itself y
times, where y is a natural numebr. x**y = exp(y*log(x)) for any
value of y.

Operators with the same precedence are calculated from left to
right. Only the power operator is calculated **from right to
left** (as is usual in mathematics). The expression "`4
** 3 ** 2`" is therefore "`4 ** ( 3 ** 2 )` ", not
"`( 4 ** 3 ) ** 2`", making the answer 262144, not 4096.

The following restrictions apply to the expression " `X **
Y`": If `X` = 0, `Y` must be positive. If
`X` is negative, `Y` must be a whole number.

Note

DIV and MOD

The integer division operators DIV and MOD are defined as
follows:

- ndiv = n1 DIV n2

- nmod = n1 MOD n2

so that:

- n1 = ndiv * n2 + nmod

- ndiv is an integer

- 0 <= nmod < |n2|

If n2 = 1 and n1 <> 0, a runtime error occurs.

Example

DATA: D1 TYPE I, D2 TYPE I, D3 TYPE I, D4 TYPE I, M1 TYPE P DECIMALS 1, M2 TYPE P DECIMALS 1, M3 TYPE P DECIMALS 1, M4 TYPE P DECIMALS 1, PF1 TYPE F VALUE '+7.3', PF2 TYPE F VALUE '+2.4', NF1 TYPE F VALUE '-7.3', NF2 TYPE F VALUE '-2.4'. D1 = PF1 DIV PF2. M1 = PF1 MOD PF2. D2 = NF1 DIV PF2. M2 = NF1 MOD PF2. D3 = PF1 DIV NF2. M3 = PF1 MOD NF2. D4 = NF1 DIV NF2. M4 = NF1 MOD NF2.

The variables now have the following values:

`D1 = 3, M1 = 0.1,`

`D2 = - 4, M2 = 2.3,`

`D3 = - 3, M3 = 0.1,`

`D4 = 4, M4 = 2.3.`

Example

The ABS, SIGN, CEIL, FLOOR, TRUNC, and FRAC Functions

DATA: I TYPE I, P TYPE P DECIMALS 2, M TYPE F VALUE '-3.5', D TYPE P DECIMALS 1. P = ABS( M ). " 3,5 I = P. " 4 - business rounding I = M. " -4 I = CEIL( P ). " 4 - next largest whole number I = CEIL( M ). " -3 I = FLOOR( P ). " 3 - next smallest whole number I = FLOOR( M ). " -4 I = TRUNC( P ). " 3 - integer part I = TRUNC( M ). " -3 D = FRAC( P ). " 0.5 - decimal part D = FRAC( M ). " -0.5

- The functions SIN, COS and TAN are defined for any number.
However, the results become imprecise if the argument is greater
than approximately
`1E8`(10**8).

- You can calculate logarithms in a base other than e or 10 as
follows:

Logarithm of`b`to base`a`=`LOG( b ) / LOG( a )`

Note

**Runtime errors:**

The functions and operators described above can cause runtime
errors if their operands are inappropriate (for example, trying to
calculate the logarithm of a negative argument).

Related