Chapter 6: Key Takeaways -- Arithmetic Operations and Numeric Processing
Chapter Summary
Chapter 6 covers COBOL's five arithmetic verbs -- ADD, SUBTRACT, MULTIPLY, DIVIDE, and COMPUTE -- and the supporting features that make COBOL uniquely suited for financial computation. COBOL was designed from the ground up for business arithmetic: its decimal data types store numbers exactly as humans write them (0.10 is precisely 0.10, not a binary approximation), and its arithmetic operations include built-in mechanisms for rounding control, overflow detection, and remainder handling. This is not an academic distinction -- it is the reason that more than 70% of the world's financial transaction processing still runs on COBOL.
The first four verbs -- ADD, SUBTRACT, MULTIPLY, and DIVIDE -- use an English-like syntax (ADD WS-TAX TO WS-TOTAL, SUBTRACT WS-DISCOUNT FROM WS-PRICE GIVING WS-NET) that reads almost like a business instruction. Each supports the GIVING phrase to preserve original operands, the ROUNDED phrase to control rounding behavior, and the ON SIZE ERROR phrase to detect overflow and division by zero. The CORRESPONDING option on ADD and SUBTRACT enables matching fields by name across group items. DIVIDE uniquely supports the REMAINDER phrase for integer division with leftover calculation, and its INTO versus BY syntax is a common source of confusion that must be mastered. COMPUTE brings conventional algebraic notation to COBOL, supporting standard operator precedence and exponentiation (**), and is preferred for complex formulas like compound interest and loan payment calculations.
The chapter emphasizes five non-negotiable practices for production financial code: always use ROUNDED on currency calculations, always use ON SIZE ERROR on every arithmetic operation, always use COMP-3 for financial data fields, always use GIVING to preserve operand values, and always use signed fields (PIC S9) for any result that might be negative. These practices, combined with proper result field sizing and the discipline of checking divisors before every division, form the gold standard for COBOL financial programming.
Key Concepts
- ADD has three forms:
ADD a TO b(modifies b),ADD a b GIVING c(preserves a and b), andADD CORRESPONDING(matches fields by name across group items). - SUBTRACT mirrors ADD:
SUBTRACT a FROM b,SUBTRACT a FROM b GIVING c, andSUBTRACT CORRESPONDING. - MULTIPLY has two forms:
MULTIPLY a BY b(result replaces b, not a -- a common confusion) andMULTIPLY a BY b GIVING c. - DIVIDE has four forms:
DIVIDE a INTO b,DIVIDE a INTO b GIVING c,DIVIDE a BY b GIVING c, andDIVIDE a BY b GIVING c REMAINDER d. - The INTO versus BY distinction:
DIVIDE A INTO Bmeans B = B / A.DIVIDE A BY B GIVING Cmeans C = A / B. The mnemonic: INTO divides the second operand; BY follows mathematical order. - COMPUTE uses algebraic notation with operators +, -, *, /, and ** (exponentiation). Precedence follows standard mathematics: parentheses, then exponentiation, then multiplication/division, then addition/subtraction.
- COMPUTE is preferred for complex formulas (compound interest, loan payments) because it benefits from the compiler's intermediate precision management. Individual verbs are preferred for simple, self-documenting operations.
- The ROUNDED phrase changes default truncation to rounding. Without it, excess decimal digits are silently dropped. With it, the standard "round half-up" rule applies.
- ROUNDED MODE (COBOL-2002+) provides explicit rounding algorithms: NEAREST-AWAY-FROM-ZERO (default), NEAREST-EVEN (banker's rounding), TRUNCATION, and others.
- Banker's rounding (NEAREST-EVEN) eliminates the systematic upward bias of standard half-up rounding. Over millions of transactions, this prevents significant accumulated error.
- ON SIZE ERROR detects overflow (result exceeds field capacity) and division by zero. When triggered, the receiving field is not modified and the error imperative executes.
- Without ON SIZE ERROR, overflow produces undefined results and the program continues silently with corrupted data -- one of the most dangerous behaviors in COBOL.
- The GIVING phrase preserves all source operands, which is essential for audit trails, report generation, and subsequent calculations in financial systems.
- Automatic decimal alignment: COBOL aligns decimal points before performing arithmetic on fields with different decimal positions, ensuring correct results without manual adjustment.
- COMP-3 (packed decimal) stores two digits per byte with exact decimal representation, making it the standard for financial data. Never use COMP-1/COMP-2 (floating point) for money.
- Result field sizing: for multiplication, the result needs PIC 9(m+n)V9(p+q) where m,n are integer digits and p,q are decimal places of the operands. Always size generously.
Common Pitfalls
- Omitting ON SIZE ERROR. Without it, arithmetic overflow produces undefined results silently. The program continues with corrupt data, potentially causing incorrect financial calculations that are extremely difficult to trace. Use ON SIZE ERROR on every arithmetic operation in production code.
- Dividing without checking for zero. Division by zero triggers ON SIZE ERROR if present, but produces undefined results without it. Always check the divisor before dividing, even when ON SIZE ERROR is specified (defense in depth).
- Undersized result fields. Multiplying PIC 9(5)V99 by PIC 9(3) can produce up to 8 integer digits. If the result field is PIC 9(5)V99, the high-order digits are silently truncated. Size result fields for the maximum possible product.
- Confusing DIVIDE INTO and DIVIDE BY.
DIVIDE 5 INTO WS-VALUEmeans WS-VALUE = WS-VALUE / 5 (not 5 / WS-VALUE). The BY form follows natural mathematical reading:DIVIDE A BY B GIVING Cmeans C = A / B. - Using unsigned fields for potentially negative results. Subtracting a larger number from a smaller one and storing in PIC 9(7)V99 (no S) silently loses the negative sign. Always use PIC S9 for fields that might hold negative values.
- Not using ROUNDED for currency calculations. Default truncation can systematically lose or gain fractions of a cent. Over millions of transactions, this creates real financial discrepancies.
- Dividing before multiplying in multi-step calculations.
WS-A / 3 * WS-Bproduces a repeating decimal intermediate result that loses precision.WS-A * WS-B / 3avoids this by keeping the intermediate result as a larger integer. - Mixing USAGE types without considering performance. Arithmetic between COMP-3, DISPLAY, and COMP fields requires implicit conversion. Use consistent USAGE types across operands in the same calculation for optimal performance.
Quick Reference
ADD:
ADD a TO b b = b + a
ADD a b GIVING c c = a + b
ADD CORR group-1 TO group-2 Matches by name
SUBTRACT:
SUBTRACT a FROM b b = b - a
SUBTRACT a FROM b GIVING c c = b - a
SUBTRACT CORR group-1 FROM group-2
MULTIPLY:
MULTIPLY a BY b b = a * b (result in b!)
MULTIPLY a BY b GIVING c c = a * b
DIVIDE:
DIVIDE a INTO b b = b / a
DIVIDE a INTO b GIVING c c = b / a
DIVIDE a BY b GIVING c c = a / b
DIVIDE a BY b GIVING c c = a / b (integer quotient)
REMAINDER d d = a - (c * b)
COMPUTE:
COMPUTE c = a + b * d / e ** 2
Operators: + - * / ** (exponentiation)
Precedence: () then ** then * / then + -
ROUNDED Phrase:
ADD a TO b ROUNDED
COMPUTE c ROUNDED = a / b
COMPUTE c ROUNDED MODE IS NEAREST-EVEN = a * b
ON SIZE ERROR:
ADD a TO b
ON SIZE ERROR
PERFORM error-handler
NOT ON SIZE ERROR
CONTINUE
END-ADD
Financial Calculation Pattern:
01 WS-AMOUNT PIC S9(9)V99 COMP-3 VALUE ZERO.
01 WS-RATE PIC SV9(6) COMP-3 VALUE ZERO.
01 WS-RESULT PIC S9(11)V99 COMP-3 VALUE ZERO.
MULTIPLY WS-AMOUNT BY WS-RATE
GIVING WS-RESULT ROUNDED
ON SIZE ERROR
PERFORM 9999-OVERFLOW-HANDLER
END-MULTIPLY
Five Rules for Financial Arithmetic:
1. Always use ROUNDED on currency calculations
2. Always use ON SIZE ERROR on every operation
3. Always use COMP-3 for financial data
4. Always use GIVING to preserve operands
5. Always use PIC S9 for potentially negative results
What's Next
Chapter 7 (the next chapter in the textbook) covers conditional logic and program flow control -- IF/ELSE, EVALUATE (COBOL's case/switch statement), condition names, complex conditions with AND/OR/NOT, and the PERFORM statement in all its forms. Combined with the arithmetic skills from this chapter, conditional logic will enable you to build complete business processing programs that make decisions based on calculated results, validate data, and implement the branching logic at the heart of every financial application.