## Wednesday, February 17, 2010

### 5. Arithmetic verbs

All the basic arithmetic operations of ADD, SUBTRACT, MULTIPLY, and DIVIDE require that the fields operated on (1) have numeric PIC clauses and (2) actually have numeric data when the program is executed.
In all cases, the resultant field must be an identifier or data-name, not a literal.

The result, or sum, of an ADD operation is always placed in the last field mentioned. The only field that is altered as a result of the ADD operation is this last field, which is the one directly following the word TO.
When using the TO format in an ADD statement, all the data-names and literals are added together, and the result placed in the last field specified.
When using the GIVING format, all fields and literals preceding the word GIVING are added together and the sum is placed in the field following the word GIVING.

Eg 5.1:
05 EMP-BASIC PIC 9(5) VALUE 4000.
05 EMP-HRA PIC 9(3) VALUE 650.
05 EMP-TOTAL PIC 9(6) VALUE 100.

Result :
EMP-TOTAL = 4100

b. ADD EMP-BASIC EMP-HRA GIVING EMP-TOTAL.

Result :
EMP-TOTAL = 4650

SUBTRACT statement
All fields and literals preceding the word FROM will be added together and the sum subtracted from the field following the word FROM. The result, or difference, will be placed in this same field if no GIVING option is used. All other fields will remain unchanged.

Eg 5.2:
05 EMP-GROSS PIC 9(5) VALUE 4250.
05 EMP-PF PIC 9(3) VALUE 250.
05 EMP-ITAX PIC 9(3) VALUE 100.
05 EMP-NET PIC 9(6) VALUE 100.

a. SUBTRACT EMP-PF FROM EMP-BASIC.

Result :
EMP-BASIC = 4000

b. SUBTRACT EMP-PF EMP-ITAX FROM EMP-GROSS.

Result :
EMP-GROSS = 3900

c. SUBTRACT EMP-PF EMP-ITAX FROM EMP-GROSS GIVING EMP-NET.

Result :
EMP-GROSS = 4250
EMP-NET = 3900

MULTIPLY and DIVIDE statements
With each MULTIPLY or DIVIDE statement specified, only two operands can be multiplied or divided. Always make sure the receiving fields is large enough to store the result. The preposition used with the MULTIPLY verb is always BY. In the DIVIDE operation, the preposition is either BY or INTO.

Eg 5.3:
05 EMP-GROSS PIC 9(5) VALUE 4000.
05 EMP-ANN-SAL PIC 9(5) VALUE ZERO.
05 EMP-NEW-SAL PIC 9(5) .
05 EMP-REM PIC 9(3).

a. MULTIPLY 12 BY EMP-GROSS.
Result :
EMP-GROSS = 48000

b. MULTIPLY EMP-GROSS BY 12 GIVING EMP-ANN-SAL.
Result :
EMP-GROSS = 4000
EMP-ANN-SAL=48000

c. DIVIDE 4 INTO EMP-GROSS.
Result :
EMP-GROSS = 1000

d. DIVIDE 4 INTO EMP-GROSS GIVING EMP-NEW-SAL.
Result :
EMP-GROSS = 4000
EMP-NEW-SAL = 1000

e. DIVIDE EMP-GROSS BY 4 GIVING EMP-NEW-SAL.
Result :
EMP-NEW-SAL = 1000

Use of the REMAINDER clause in the DIVIDE operation
When performing a division operation, the result will be placed in the receiving field according to the PIC specifications of that field.

Eg 5.4:
DIVIDE 130 BY 40 GIVING WS-TOTAL.

After the operation is performed, 03 is placed in WS-TOTAL.
It is sometimes useful to store the remainder of a division operation for additional processing. The DIVIDE can be used for this purpose by including a REMAINDER clause.

ROUNDED Option
A frequent need exists for rounding numeric values.

Eg 5.5:
05 AMT1 PIC 99V999 VALUE 12.857.
05 AMT2 PIC 99V999 VALUE 25.142.
05 AMT3 PIC 99V99 VALUE 37.99.

In the code given above, the result 37.999 is placed in an accumulator. When this value is move to the field AMT3 , the low-order decimal position is truncated and 37.99 is stored in the field. A more desirable result would be 38.00 since 38 is closer to the sum of 37.999. We consider results more accurate if they are rounded to the nearest decimal position.
To obtain rounded results, the ROUNDED option may be specified with any arithmetic statement. In all case, it directly follows the resultant data-name.

Eg 5.6:
ADD AMT1 AMT2 GIVING AMT3 ROUNDED.

If the ROUNDED option is not specified, truncation of decimal positions will occur if the resultant field cannot accommodate all the decimal positions in the result. With the ROUNDED option, the computer will always round the result to the PICTURE specification of the receiving field.

If ROUNDED and REMAINDER are to be used in the same DIVIDE statement, ROUNDED must appear first.

ON SIZE ERROR
Consider the following :

Eg 5.7:
05 AMT1 PIC 999 VALUE 800.
05 AMT2 PIC 999 VALUE 150.
05 AMT3 PIC 999 VALUE 050.

The effect of the above statement would be the same as coding MOVE 1000 TO AMT3. In this case, the resultant field is not large enough to store the accumulated sum. In other words , an overflow or size error condition has occurred. This will produce erroneous results.

Eg 5.8:
ON SIZE ERROR MOVE ZERO TO TOTAL-OUT.

In a divide, the size error condition has additional significance. If an attempt is made to divide by zero, a size error condition will occur. This is because division by zero yields a result of infinity which makes it impossible to define a sufficiently large receiving field.
If the ON SIZE ERROR option is employed along with the ROUNDED option, the word ROUNDED always precedes ON SIZE ERROR.

COMPUTE statement
If complex or extensive arithmetic operations are required in a program, the use of the four arithmetic verbs may prove cumbersome. The COMPUTE verb provides another method of performing arithmetic. The COMPUTE statement uses the following arithmetic symbols :
- Subtract
* Multiply
/ Divide
** exponentiation

Eg 5.9:
COMPUTE TOTAL = AMT1 + AMT2 – AMT3.

To round the results in a COMPUTE statement to the specifications of the receiving field, use the ROUNDED option directly following the receiving field. If we need to test for a size error condition we may use the ON SIZE ERROR clause as the last one in the statement.

The sequence in which operations are performed in a COMPUTE statement
**
* or / (whichever appears first from left to right)
+ or - (whichever appears first from left to right)
The use of parentheses overrides rules 1-3. That is, operations with parentheses are performed first.

Back to COBOL Index