Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 41 Arithmetic Clauses The ROUNDED clause –Place ROUNDED after the variable that holds the result ADD A TO B ROUNDED. ADD A TO B GIVING C ROUNDED.

Similar presentations


Presentation on theme: "Lecture 41 Arithmetic Clauses The ROUNDED clause –Place ROUNDED after the variable that holds the result ADD A TO B ROUNDED. ADD A TO B GIVING C ROUNDED."— Presentation transcript:

1 lecture 41 Arithmetic Clauses The ROUNDED clause –Place ROUNDED after the variable that holds the result ADD A TO B ROUNDED. ADD A TO B GIVING C ROUNDED. SUBTRACT A FROM B ROUNDED. MULTIPLY A BY B ROUNDED. DIVIDE A INTO B ROUNDED. –Rounds the computed value to the PIC specification of the result field –If ROUNDED is not used then truncation occurs

2 lecture 42 ROUNDED Clause Examples 9.99 ROUNDED into 99V9  10.0 (9.99 +.05 = 10.4) 9.94 ROUNDED into 9V9  9.9 (9.94 +.05 = 9.99) Adds.05 then truncates then moves to PIC clause 89.5 ROUNDED into 99  90 (89.5 +.5 = 90.0) 89.4 ROUNDED into 99  89 (89.4 +.5 = 89.9) Adds.5 then truncates then moves to PIC clause

3 lecture 43 ON SIZE ERROR Clause Generates a warning when a calculation is too large for the resultant field Used to detect overflow Appears after ROUNDED option Can execute a statement on this condition Example A = 150 PIC 999, B = 60 PIC 99 ADD A TO B ON SIZE ERROR MOVE ‘OVERFLOW’ TO WS-ERROR-MSG.

4 lecture 44 COMPUTE Statement COMPUTE data-name = arithmetic expression. Use for multiple arithmetic operations Operations –+, -, *, /, ** –5 ** 3  5 * 5 * 5

5 lecture 45 COMPUTE Statement All variables in the arithmetic expression must be numeric items You can code the Rounded clause whenever you will lose decimal places in a calculation If the Rounded clause is not used, the extra decimal places are truncated! If the variable that receives the arithmetic expression is numeric edited then that variable cannot be used in the arithmetic expression. Otherwise, it must be numeric

6 lecture 46 MULTIPLY B BY B GIVING B-SQUARED. MULTIPLY 4 BY A GIVING FOUR-A. MULTIPLY FOUR-A BY C GIVING FOUR-A-C. SUBTRACT FOUR-A-C FROM B-SQUARED GIVING RESULT-1. COMPUTE RESULT-2 = RESULT-1 **.5. SUBTRACT B FROM RESULT-2 GIVING NUMERATOR. MULTIPLY 2 BY A GIVING DENOMINATOR. DIVIDE NUMERATOR BY DENOMINATOR GIVING X. COMPUTE X = (-B + (B ** 2 - ( 4 * A * C)) **.5) / ( 2 * A). Both sets of code apply to the quadratic formula X = -B +  B 2 -4AC 2A Poor Code Improved Code

7 lecture 47 Operator Precedence Parentheses (inside out and left to right) Exponentiation Multiplication, division (left to right) Addition, subtraction (left to right) You can always clarify the sequence in which the operations are done by placing parenthesis

8 lecture 48 DATA NAMEA BC COMMENTS Value before execution 2 3 10 Initial Values Value after execution of: COMPUTE C = A + B. 2 3 5 Simple addition COMPUTE C = A + B * 2. 2 3 8 Multiplication before addition COMPUTE C = (A + B) * 2. 2 3 10 Parenthesis evaluated first COMPUTE C = A ** B. 2 3 8 algebraically, c = a b COMPUTE C = B ** A. 2 3 9 algebraically, c = b a

9 lecture 49 ALGEBRAIC EXPRESSION COBOL COMPUTE x = a + bCOMPUTE X = A + B. x = a + b COMPUTE X = (A + B) / 2. 2 x = (a + b)cCOMPUTE X = (A + B) * C / 2. 2 x = a + bCOMPUTE X = (A + B) / (2 * C). 2c x =  aCOMPUTE X = A **.5. x = a 2 + b 2 COMPUTE X = (A ** 2 + B ** 2) / C ** 2 c 2

10 lecture 410 Intermediate Results An intermediate result is computed when an expression consists of two or more operations. How is precision to the right of the decimal place preserved in intermediate results? –i.e., when you have PICs with different decimal precision –The final PIC used for the intermediate result can be compiler dependent Addition & Subtraction –Maximum no. of decimal places of the PICs Multiplication –Sum of the decimal places of the PICs Division –No. of decimal places in the PIC of the numerator Two solutions : –Redefine the fields used in the operations –Re-sequence the operations in the expression


Download ppt "Lecture 41 Arithmetic Clauses The ROUNDED clause –Place ROUNDED after the variable that holds the result ADD A TO B ROUNDED. ADD A TO B GIVING C ROUNDED."

Similar presentations


Ads by Google