Key Takeaways — Chapter 5: Numeric Precision and Arithmetic

  1. Use COMPUTE for complex formulas, explicit verbs for simple operations. ADD/SUBTRACT for accumulations, DIVIDE with REMAINDER for penny distribution, COMPUTE for multi-operand expressions and exponentiation. Good programs use both.

  2. Always use ROUNDED on the final result. Without ROUNDED, excess decimal digits are silently truncated. Carry extra precision (six or more decimal places) in intermediate fields and round only the last step.

  3. Every DIVIDE needs ON SIZE ERROR. Division by zero is not always obvious — divisors can come from files, databases, or user input. Protect every division. Extend this discipline to COMPUTE statements involving multiplication that might overflow.

  4. Never use floating point (COMP-1/COMP-2) for money. Floating point cannot represent most decimal fractions exactly. Adding $0.01 ten thousand times in COMP-1 does not produce $100.00. Use COMP-3 (packed decimal) for all financial calculations.

  5. Always include S (sign) on fields that might be negative. Missing sign indicators silently convert negative results to positive — a bug that has caused millions of dollars in errors across the industry. If a field participates in subtraction, it needs S.

  6. Understand intermediate result precision. The compiler allocates temporary fields for partial results. Know your compiler's limits (18 digits for ARITH(COMPAT), 31 for ARITH(EXTEND)) and restructure expressions if needed.

  7. The penny distribution pattern is essential. DIVIDE with REMAINDER, distribute the base amount to all recipients, then add one penny each to the first N recipients (where N = remainder in pennies). Always verify that the sum equals the original total.

  8. Verify your arithmetic. After any proration or distribution, add the parts and compare to the original total. This single check catches rounding errors, missing-sign bugs, and overflow truncation.

  9. ON SIZE ERROR protects the target field. When a size error is detected, the target retains its previous value (not zero, not the truncated result). The imperative statement after ON SIZE ERROR executes, giving you a chance to handle the condition.

  10. Banker's Rounding (NEAREST-EVEN) reduces statistical bias. Standard "round half up" creates a slight upward bias over millions of transactions. NEAREST-EVEN eliminates this bias by rounding .5 to the nearest even digit.

  11. Track rounding adjustments for audit. In production financial systems, record the difference between the precise calculated value and the rounded posted value. These adjustments must be posted to a suspense account for reconciliation.

  12. Defensive programming is not optional in financial systems. The cost of a correct but slightly slower program is negligible. The cost of an incorrect program — reprocessing claims, issuing refund checks, regulatory penalties — can be millions of dollars.