SAP ABAP SYNTAX FOR COMPUTE PART TWO

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 . 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. 





No comments :

Post a Comment