Friday, November 18, 2011

COMPUTE


Complex arithmetic operations can be carried out using COMPUTE statement. We can use arithmetic symbols than keywords and so it is simple and easy to code.
+ For ADD, - for SUBTRACT, * for MULTIPLY, / for DIVIDE and ** for exponentiation.
Rule: Left to right –   1.Parentheses
                             2.Exponentiation
                             3.Multiplication and Division
                             4.Addition and Subtraction

Caution: When ROUNDED is coded with COMPUTE, some compiler will do rounding for every arithmetic operation and so the final result would not be precise.

77 A PIC 999 VALUE 10
COMPUTE A ROUNDED = (A+2.95) *10.99 
Result: (ROUNDED(ROUNDED(12.95) * ROUNDED(10.99)) =120 or
  ROUNDED(142.3205) = 142        
So the result can be 120 or 142.Be cautious when using ROUNDED keyword with COMPUTE statement.

All arithmetic operators have their own explicit scope terminators. (END-ADD, END-SUBTRACT, END-MULTIPLY, END-DIVIDE, END-COMPUTE). It is suggested to use them.
CORRESPONDING is available for ADD and SUBTRACT only.  

No comments:

Post a Comment