Handling Numeric Data
2.7 Using the Arithmetic Statements
Equivalent coding:
ADD A, B, GIVING TEMP.
ADD TEMP, C, GIVING TEMP.
ADD TEMP, D, GIVING TEMP.
ADD TEMP, E, GIVING E.
ADD TEMP, F, GIVING F.
ADD TEMP, G, GIVING G.
ADD TEMP, H, GIVING H.
2.
Statement:
SUBTRACT A, B, C, FROM D.
Equivalent coding:
ADD A, B, GIVING TEMP.
ADD TEMP, C, GIVING TEMP.
SUBTRACT TEMP FROM D, GIVING D.
3.
Statement:
ADD A,B,C,D, GIVING E.
Equivalent coding:
ADD A,B, GIVING TEMP.
ADD TEMP, C, GIVING TEMP.
ADD TEMP, D, GIVING E.
As in all Compaq COBOL statements, the commas in these statements are
optional.
2.7.8 Common Errors in Arithmetic Statements
Programmers most commonly make the following errors when using arithmetic
statements:
•
Using an alphanumeric item in an arithmetic statement. The MOVE
statement allows data movement between alphanumeric items and certain
numeric items, but arithmetic statements require that all items be numeric.
•
Writing the ADD or SUBTRACT statements without the GIVING phrase, and
attempting to put the result into a numeric-edited item.
•
Subtracting a 1 from a numeric counter that was described as an unsigned
quantity and then testing for a value less than zero.
•
Forgetting that the MULTIPLY statement, without the GIVING phrase,
stores the result back into the second operand (multiplier).
•
Performing a series of calculations that generates an intermediate result
larger than 36 digits when the final result will have 18 or fewer digits. You
can prevent this problem by interspersing divisions with multiplications or
by dropping nonsignificant digits after multiplying large numbers or numbers
with many decimal places.
•
Forgetting that when an arithmetic statement has multiple receiving items
you must specify the ROUNDED phrase for each receiving item you want
rounded.
•
Forgetting that the ON SIZE ERROR phrase applies to all receiving items
in an arithmetic statement containing multiple receiving items. Only those
receiving items for which a size error condition is raised are left unaltered.
The ON SIZE ERROR imperative statement is executed after all the receiving
items are processed.
•
Controlling a loop by adding to a numeric counter that was described as PIC
9, and then testing for a value of 10 or greater to exit the loop.
•
Forgetting that ROUNDING is done before the ON SIZE ERROR test.
Handling Numeric Data 2–15