ABAP contains a range of built-in functions that you can use as mathematical expressions, or as part of a mathematical expression:
[COMPUTE] <n> = <func>( <m> ).
The blanks between the parentheses and the argument <m> are obligatory. The result of calling the function <func> with the argument <m> is assigned to <n>.
Functions for all Numeric Data Types
The following built-in functions work with all three numeric data types (F, I, and P) as arguments.
Functions for all numeric data types
Function |
Result |
ABS |
Absolute value of argument. |
SIGN |
Sign of argument: 1 X > 0 SIGN( X) = 0 if X = 0 -1 X < 0 |
CEIL |
Smallest integer value not smaller than the argument. |
FLOOR |
Largest integer value not larger than the argument. |
TRUNC |
Integer part of argument. |
FRAC |
Fraction part of argument. |
The argument of these functions does not have to be a numeric data type. If you choose another type, it is converted to a numeric type. For performance reasons, however, you should use the correct type whenever possible. The functions itself do not have a data type of their own. They do not change the numerical precision of a numerical operation.
DATA N TYPE P DECIMALS 2.
DATA M TYPE P DECIMALS 2 VALUE '-5.55'.
N = ABS( M ). WRITE: 'ABS: ', N.
N = SIGN( M ). WRITE: / 'SIGN: ', N.
N = CEIL( M ). WRITE: / 'CEIL: ', N.
N = FLOOR( M ). WRITE: / 'FLOOR:', N.
N = TRUNC( M ). WRITE: / 'TRUNC:', N.
N = FRAC( M ). WRITE: / 'FRAC: ', N.
The output appears as follows:
ABS: 5.55
SIGN: 1.00-
CEIL: 5.00-
FLOOR: 6.00-
TRUNC: 5.00-
FRAC: 0.55-
DATA: T1(10),
T2(10) VALUE '-100'.
T1 = ABS( T2 ).
WRITE T1.
This produces the following output:
100
Two conversions are performed. First, the contents of field T2 (type C) are converted to type P. Then the system processes the ABS function using the results of the conversion. Then, during the assignment to the type C field T1, the result of the function is converted back to type C.
Floating-Point Functions
The following built-in functions work with floating point numbers (data type F) as an argument.
Functions for floating point data types
Function |
Meaning |
ACOS, ASIN, ATAN; COS, SIN, TAN |
Trigonometric functions. |
COSH, SINH, TANH |
Hyperbolic functions. |
EXP |
Exponential function with base e (e=2.7182818285). |
LOG |
Natural logarithm with base e. |
LOG10 |
Logarithm with base 10. |
SQRT |
Square root. |
For all functions, the normal mathematical constraints apply (for example, square root is only possible for positive numbers). If you fail to observe them, a runtime error occurs.
The argument of these functions does not have to be a floating point field. If you choose another type, it is
converted to type F. The functions themselves have the data type F. This can change the numerical precision of a numerical operation.
DATA: RESULT TYPE F,
PI(10) VALUE '3.141592654'.
RESULT = COS( PI ).
WRITE RESULT.
The output is