Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 38 ยท Data Manipulation

Arithmetic in COBOL: ADD, SUBTRACT, MULTIPLY, DIVIDE, COMPUTE

Do exact decimal maths in COBOL: ADD, SUBTRACT, MULTIPLY, DIVIDE with GIVING and REMAINDER, COMPUTE expressions, ROUNDED, truncation, ON SIZE ERROR and intermediate precision.

COBOL's arithmetic works on exact decimal fields, so money calculations behave the way an accountant expects. There are two styles: the four English-like verbs (ADD, SUBTRACT, MULTIPLY, DIVIDE) and the algebraic COMPUTE. Both support ROUNDED and ON SIZE ERROR, the two features that control what happens when the answer does not fit the receiving field.

ADD

COBOL
           ADD 1 TO COUNTER
           ADD PRICE TO TOTAL
           ADD A B C TO TOTAL             *> TOTAL = TOTAL + A + B + C
           ADD A B GIVING SUM-AB          *> SUM-AB = A + B
           ADD A TO B GIVING C            *> C = A + B; B is not changed
           ADD A TO B C                   *> B = B + A and C = C + A

TO adds into the receiver and changes it. GIVING stores the result in a separate field and leaves the operands alone.

SUBTRACT

COBOL
           SUBTRACT DISCOUNT FROM PRICE           *> PRICE = PRICE - DISCOUNT
           SUBTRACT A B FROM C                    *> C = C - A - B
           SUBTRACT A FROM B GIVING RESULT        *> RESULT = B - A

With GIVING, the FROM operand is the starting value, the subtracted values come first.

MULTIPLY

COBOL
           MULTIPLY RATE BY AMOUNT                *> AMOUNT = AMOUNT * RATE
           MULTIPLY HOURS BY RATE GIVING PAY      *> PAY = HOURS * RATE

DIVIDE

COBOL
           DIVIDE TOTAL BY COUNT-ITEMS GIVING AVERAGE
           DIVIDE 7 INTO TOTAL                    *> TOTAL = TOTAL / 7
           DIVIDE 17 BY 5 GIVING QUOTIENT REMAINDER LEFTOVER

INTO divides the receiving field by the number given; BY ... GIVING divides the first operand by the second. REMAINDER gets what is left after the quotient (which is truncated unless ROUNDED is used; with ROUNDED the remainder is computed from the unrounded quotient). For 17 / 5 into PIC 99 fields, QUOTIENT is 3 and LEFTOVER is 2.

COMPUTE

COMPUTE evaluates an algebraic expression with + - * / ** and parentheses:

COBOL
           COMPUTE GROSS = HOURS * RATE
           COMPUTE NET   = GROSS - (GROSS * TAX-RATE)
           COMPUTE AREA  = 3.14159 * RADIUS ** 2
           COMPUTE AVG ROUNDED = (A + B + C) / 3

Precedence: ** first, then * and /, then + and -, left to right. Unary minus is allowed. The expression can use data items and numeric literals, and functions such as FUNCTION SQRT(X); it cannot use edited items. Operators need spaces around them. COMPUTE is the shortest way to write anything beyond one operation.

ROUNDED

By default the result is truncated to the receiving field's decimals. ROUNDED rounds instead, normally to the nearest value with halves rounded away from zero:

COBOL
           01 R1   PIC 9V99.
           ...
           COMPUTE R1 = 2 / 3             *> .66
           COMPUTE R1 ROUNDED = 2 / 3     *> .67

COBOL 2002 added ROUNDED MODE IS ... with choices such as NEAREST-EVEN (banker's rounding), TRUNCATION, TOWARD-GREATER and TOWARD-LESSER.

Python Truncation versus ROUNDED (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

ON SIZE ERROR

If the result does not fit (too many integer digits, or division by zero), the stored result would be wrong. Without protection, COBOL silently drops high-order digits on most compilers (and the result is undefined by the standard). ON SIZE ERROR lets you catch it:

COBOL
           COMPUTE TOTAL = A * B
               ON SIZE ERROR
                   DISPLAY "Overflow: TOTAL not updated"
                   MOVE ZERO TO TOTAL
               NOT ON SIZE ERROR
                   DISPLAY "OK"
           END-COMPUTE

           DIVIDE TOTAL BY COUNT-ITEMS GIVING AVERAGE
               ON SIZE ERROR
                   MOVE ZERO TO AVERAGE
           END-DIVIDE

When the condition occurs, the receiving field is left unchanged (for most statements) and the ON SIZE ERROR branch runs. Any ADD, SUBTRACT, MULTIPLY, DIVIDE or COMPUTE supports it, with its own END- terminator. Always use it on a DIVIDE whose divisor could be zero, and wherever a field might be too small.

Python ON SIZE ERROR (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Intermediate results

In COMPUTE the compiler keeps extra digits for intermediate results, so COMPUTE X ROUNDED = A / B * C does not round after the division. Different compilers use slightly different internal precision, which is why the same program can give a last-digit difference on another platform. For financial code, put important rounding steps in separate COMPUTEs so you control exactly where rounding happens.

Common mistakes

  • Expecting ADD A TO B GIVING C to change B: with GIVING, only C is updated.
  • Dividing without ON SIZE ERROR.
  • Receiving fields with too few decimals, silently truncating cents.
  • Using COMPUTE with edited items as operands.
  • Forgetting that MULTIPLY A BY B changes B.
  • No spaces around - in A -B (it can be read as a negative number or a hyphenated name).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does ADD A B GIVING C do?

    GIVING stores the result in the named field and does not change the operands.
  2. 2. A field is PIC 9V99. What does COMPUTE F = 2 / 3 store without ROUNDED?

    The default is truncation to the receiving field's decimals, giving .66.
  3. 3. What happens to the receiving field when ON SIZE ERROR occurs?

    The result is not stored; your handler decides what to do.
  4. 4. What is the result of DIVIDE 17 BY 5 GIVING Q REMAINDER R (integer fields)?

    17 = 3 * 5 + 2. The quotient is truncated and the remainder is what is left.

Summary

  • ADD, SUBTRACT, MULTIPLY, DIVIDE use TO/FROM/BY/INTO; GIVING stores in a different field.
  • COMPUTE evaluates full expressions with **, *, /, +, - and parentheses.
  • Results are truncated unless you say ROUNDED (nearest, halves away from zero by default).
  • ON SIZE ERROR catches overflow and divide-by-zero and leaves the field unchanged; close with END-COMPUTE etc.
  • DIVIDE ... REMAINDER gives quotient and remainder; keep rounding steps explicit in financial code.

Next: String Handling: STRING, UNSTRING and INSPECT