ERP Database - The Unofficial ERP Knowledge Base

Facebook Twitter del.icio.us Digg it
ERP Database contains a huge collection of articles related to ERP System and Software. Many of the articles are specifically related to SAP. All these ERP/SAP articles are freely available to everyone.

If you would like to submit an article or share any document related to ERP or any specific ERP software. Please mail it to support@erpdb.info. Please make sure that the documents are not copyrighted.
Social Bookmarks:

COMPUTE: ABAP Keyword a day

Print This Post Email This Post Written by admin on Nov 16th, 2007 | Filed under: ABAP Keywords

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

* Functions for all number types

ABS Amount (absolute value) |x| of x SIGN Sign (preceding sign) of x;

1 x > 0

SIGN( x ) = 0 if x = 0

-1 x < pi =" 3.1415926535897932)" e =" 2.7182818284590452"> 0 SQRT Square root of a non-negative number

* String functions

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:

* ndiv = n1 DIV n2

* nmod = n1 MOD n2

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 --------------------- ABAPer, mail: abap.community@gmail.com http://www.erpdb.info

Share

If you like this post, you may as well like these too:

  1. AT : ABAP Keyword a day AT Events in lists- AT LINE-SELECTION.- AT USER-COMMAND.- AT PFn.Events on selection screens- AT SELECTION-SCREEN.Control break with extracts- AT NEW f.- AT END OF f.- AT FIRST.- AT LAST.- AT...
  2. GET: ABAP Keyword a day GET: Event keyword for defining event blocks for reporting events. GET <node> [FIELDS <fi> <f 2>...]. GET node LATE [FIELDS f1 f2 ...]. Only occurs in executable programs. When the...
  3. DO : ABAP Keyword a day DO Variants 1. DO. 2. DO n TIMES. Variant 1 DO. Addition … VARYING f FROM f1 NEXT f2 Effect Repeats the processing enclosed by the DO and ENDDO statements...
  4. ASSIGN: ABAP Keyword a day ASSIGN Variants:1. ASSIGN f TO .2. ASSIGN (f) TO .3. ASSIGN TABLE FIELD (f) TO .4. ASSIGN LOCAL COPY OF MAIN TABLE FIELD (f) TO .5. ASSIGN COMPONENT idx OF...
  5. FORM: ABAP Keyword a day FORM: Defines a subroutine. Syntax FORM [USING ... [VALUE(][)] [TYPE |LIKE ]„. ] [CHANGING... [VALUE(][)] [TYPE |LIKE ]„. ]. Introduces a subroutine. The USING and CHANGING additions define the subroutine’s...
  6. FETCH : ABAP Keyword a day FETCH Basic form FETCH NEXT CURSOR c target. Effect Uses the cursor c to read the next line or lines from the dataset of a database table determined by OPEN...
  7. EXPORT : ABAP Keyword a day EXPORT 1 2 3 4 *Export data - EXPORT obj1 ... objn TO MEMORY. - EXPORT obj1 ... objn TO DATABASE dbtab(ar) ID key. - EXPORT obj1 ... objn TO...
  8. DIVIDE : ABAP Keyword a day DIVIDE Basic form DIVIDE n1 BY n2. Effect Divides the contents of n1 by n2 and places the result in n1 . This is equivalent to: n1 = n1 /...
  9. DESCRIBE : ABAP Keyword a day DESCRIBE Return attributes of a field - DESCRIBE FIELD f. Return attributes of an internal table - DESCRIBE TABLE itab. Determine distance between two fields - DESCRIBE DISTANCE BETWEEN f1...
  10. DATA : ABAP Keyword a day DATA Variants1. DATA f.2. DATA f(len).3. DATA: BEGIN OF rec,…END OF rec.4. DATA: BEGIN OF itab OCCURS n,…END OF itab.5. DATA: BEGIN OF COMMON PART c,…END OF COMMON PART.EffectDefines global...
  11. CONTINUE: ABAP Keyword a day CONTINUE Basic formCONTINUE.EffectWithin loop structures like * DO … ENDDO* WHILE … ENDWHILE* LOOP … ENDLOOP* SELECT … ENDSELECTCONTINUE terminates the current loop pass, returns the processing to the beginning...
  12. CONCATENATE : ABAP Keyword a day CONCATENATE Basic form CONCATENATE f1 … fn INTO g. Addition … SEPARATED BY h Effect Places the fields f1 to fn after g . With the fields fi (1 <=...
  13. CNT : ABAP Keyword a day CNT Basic form… CNT(h) …EffectCNT(h) is not a statement, but a field which is automatically created and filled by the system if f is a sub-field of an extract dataset...
  14. CASE: ABAP Keyword a day CASE Basic formCASE f. Effect Case distinction.Depending on the current contents of a field, this statement executes one of several alternative processing branches. The field whose contents determine how the...
  15. GET PARAMETER: ABAP Keyword a day GET PARAMETER Gets an SPA/GPA parameters. Syntax GET PARAMETER ID <pid> FIELD <field_name>. Writes the value of the SPA/GPA parameter <pid> from the user-specific SAP memory into the variable <f>....



One Response to “COMPUTE: ABAP Keyword a day”

  1. Try http://www.5ithere.com to get your flash cart. You will receive it in 2-3 business days if you are in the US. Also free 5000 games at download center.

Leave a Reply