Sunday, January 16, 2011

SAP Math Functions

Mathematical functions
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 m.
Functions for all Numeric Data Types
The following built-in functions work with all three numeric data types (f, i, p) as arguments.
Functions for all Numeric Data Types
Function Result
abs Absolute value of argument.
sign Prefix of the 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) TYPE c,
t2(10) TYPE c VALUE '-100'.
t1 = ABS( t2 ).
WRITE t1.
Result:
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.
Floating point functions
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) TYPE c VALUE '3.14159265'.
result = cos( pi ).
WRITE result.
The output is -1.00000000000000E+00. The character field pi is automatically converted to a type f field before the calculation is performed.

No comments: