COMPUTE

Basic form

COMPUTE n = arithexp.

Effect

Evaluates the arithmetic expression arithexp and places the result in the field n .

Allows use of the four basic calculation types + , - , * and / , the whole number division operators DIV (quotient) and MOD (remainder), the exponentiation operator ** (exponentiation " X ** Y means X to the power of Y ) as well as the functions listed below.

When evaluating mixed expressions, functions have priority. Then comes exponentiation, followoed by the "point operations" * , / , DIV and MOD , and finally + and - . Any combination of parentheses is also allowed.

The fields involved are usually of type I , F or P , but there are exceptions to this rule (see below).

You can omit the key word COMPUTE .
Built-in functions
ABS Amount (absolute value) |x| of x SIGN Sign (preceding sign) of x;
1 x > 0
SIGN( x ) = 0 if x = 0
-1 x < 0
CEIL Smallest whole number value not less than x FLOOR Greatest whole number value not greater than x TRUNC Whole number part of x FRAC Decimal part of x
ACOS Arc cosine(x) in the range [-pi/2, pi/2], x from [-1, 1] ASIN Arc cosine(x) in the range [0, pi], x aus [-1, 1] ATAN Arc tangent(x) in the range [-pi/2, pi/2] (pi = 3.1415926535897932) COS Cosine of an angle specified in the arc SIN Sine of an angle specified in the arc TAN Tangent of an angle specified in the arc COSH Hyperbola cosine SINH Hyperbola sine TANH Hyperbola tangent EXP Exponential function for base e = 2.7182818284590452 LOG Natural logarithm (i.e. base e) of a positive number LOG10 Logarithm of x for base 10, x > 0 SQRT Square root of a non-negative number
STRLEN Length of a string up to the last non-blank character (i.e. the occupied length )
Function expressions consist of three parts:
Function identifier directly followed by an opening parenthesis Argument Closing parenthesis
All parts of an expression, particularly any parts of a function expression, must be separated from each other by at least one blank.

Example

The following statements, especially the arithmetic expressions, 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 ).

Notes

When used in calculations, the amount of CPU time needed depends on the data type. In very simple terms, type I is the cheapest, type F needs longer and type P is the most expensive.
Normally, packed numbers arithmetic is used to evaluate arithmetic expressions. If, however, the expression contains a floating point function, or there is at least one type F operand, or the result field is type F , floating point arithmetic is used instead for the entire expression. On the other hand, if only type I fields or date and time fields occur (see below), the calculation involves integer operations.
You can also perform calculations on numeric fields other than type I , F or P . Before executing calculations, the necessary type conversions are performed (see MOVE ). You can, for instance, subtract one date field (type D ) from another, in order to calculate the number of days difference. However, for reasons of efficiency, only operands of the same number type should be used in one arithmetic expression (apart from the operands of STRLEN ). This means that no conversion is necessary and special optimization procedures can be performed.
Division by 0 results in termination unless the dividend is also 0 ( 0 / 0 ). In this case, the result is 0.
As a string processing command, the STRLEN operator treats operands (regardless of type) as character strings, without triggering internal conversions. On the other hand, the operands of floating point functions are converted to type F if they have another type.

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 now contains the value 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 now contains the value 21600 (i.e. 6 hours). The operation " MOD 86400 " ensures that the result is always a positive number, even if the period extends beyond midnight.

Note

Packed numbers arithmetic:

All P fields are treated as whole numbers. Calculations involving decimal places require additional programming to include multiplication or division by 10, 100, ... . The DECIMALS specification with the DATA declaration is effective only for output with WRITE .
If, however, fixed point arithmetic is active, the DECIMALS specification is also taken into account. In this case, intermediate results are calculated with maximum accuracy (31 decimal places). This applies particularly to division.
For this reason, you should always set the program attribute "Fixed point arithmetic".

Example

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

Without "fixed point arithmetic", P has the value 0, since " 1 / 3 " is rounded down to 0.
With fixed point arithmetic, P has the value 1, since the intermediate result of " 1 / 3 " is 0.333333333333333333333333333333.

Note

Floating point arithmetic

With floating point arithmetic, you must always expect some loss of accuracy through rounding errors (ABAP/4 number types ).

Note

Exponentiation

The exponential expression "x**y" means x*x*...*x y times, provided that y is a natural number. For any value of y, x**y is explained by exp(y*log(x)).
Operators of the same ranke are evaluated from left to right. Only the exponential operator, as is usual in mathematics, is evaluated from right to left . The expression " 4 ** 3 ** 2 " thus corresponds to " 4 ** ( 3 ** 2 ) " and not " ( 4 ** 3 ) ** 2 ", so the result is 262144 and not 4096.
The following resrtictions apply for the expression " X ** Y ": If X is equal to 0, Y must be positive. If X is negative, Y must be a whole number.

Note

DIV and MOD

The whole number division operators DIV and MOD are defined as follows:

so that:
n1 = ndiv * n2 + nmod ndiv is a whole number 0 <= nmod < |n2|
A runtime error occurs if n2 is equal to 0 and n1 is not equal to 0.

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

Functions ABS , SIGN , CEIL , FLOOR , TRUNC , FRAC
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 - commercially rounded 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 - whole number part I = TRUNC( M ). " -3 D = FRAC( P ). " 0,5 - decimal part D = FRAC( M ). " -0,5

Notes

Floating point functions
Although the functions SIN , COS and TAN are defined for any numbers, the results are imprecise if the argument is greater than about 1E8 , i.e. 10**8.
The logarithm for a base other than e or 10 is calculated as follows:

Logarithm of b for base a = LOG( b ) / LOG( a )

Note

Runtime errors

Depending on the operands, the above operators and functions can cause runtime errors (e.g. when evaluating the logarithm with a negative argument).
Related ADD , SUBTRACT , MULTIPLY , DIVIDE , MOVE

Index
© SAP AG 1996