Arithmetic in a loop

Every report in the world is a loop that adds. Twelve deposits of the same amount, a total that has to match the arithmetic, and a counter that ends one step past the last — the three facts of this lesson are small and each of them appears in a bug report somewhere.

A running total

A loop of twelve iterations adds twenty-five seventy-five each time, and then a single expression computes what the total should be:

IDENTIFICATION DIVISION. PROGRAM-ID. BALANCE. DATA DIVISION. WORKING-STORAGE SECTION. 01 DEPOSIT-F PIC 9(3)V99 VALUE 25.75. 01 TOTAL-F PIC 9(5)V99 VALUE 0. 01 MONTH-F PIC 9(2) VALUE 0. 01 CHECK-F PIC 9(5)V99 VALUE 0. PROCEDURE DIVISION. PERFORM VARYING MONTH-F FROM 1 BY 1 UNTIL MONTH-F > 12 ADD DEPOSIT-F TO TOTAL-F END-PERFORM. COMPUTE CHECK-F = DEPOSIT-F * 12. DISPLAY "deposits " MONTH-F " (one past the last)". DISPLAY "total [" TOTAL-F "]". DISPLAY "check [" CHECK-F "]". IF TOTAL-F = CHECK-F DISPLAY "total matches the check" ELSE DISPLAY "total does not match" END-IF. STOP RUN.

cobc -x -free -Wall -o balance balance.cob
./balance

deposits 13 (one past the last)
total [00309.00]
check [00309.00]
total matches the check

Twelve times twenty-five seventy-five is three hundred and nine, and the loop reached it one addition at a time. The two numbers agree because the addition is exact: every deposit is a whole number of cents and so is the total, and no step had a fraction to lose.

The counter after the loop

PERFORM VARYING adds one after the last iteration and then tests the condition, so the counter is 13 when the loop ends. The first line of the output says so on purpose: a program that prints MONTH-F as "the number of months" prints 13, and the fix is either a counter of its own or the arithmetic MONTH-F - 1.

That is not a quirk of GnuCOBOL. The VARYING clause is defined to increment before the test, which is what makes UNTIL MONTH-F > 12 the natural condition and UNTIL MONTH-F = 12 a loop of eleven.

The check that proves the total

The comparison at the end is worth more than it looks. A loop that adds is a program that can be wrong in one iteration and right in eleven, and the only way to see it is a value computed another way: COMPUTE CHECK-F = DEPOSIT-F * 12 does not use the loop at all.

The same idea scales. A batch that sums a file compares the total with a control total from the header; a report compares its subtotals with its grand total; a test asserts the value the specification gives. The check costs one statement and catches the class of bug that a reader of the code cannot see.

Rounding each time against rounding once

An item of half a cent, twelve times. The loop adds it into a field of two decimals, which is what a program does when the item itself is already in the currency, and keeps the full precision in a second field:

IDENTIFICATION DIVISION. PROGRAM-ID. ROUND. DATA DIVISION. WORKING-STORAGE SECTION. 01 ITEM-F PIC 9(1)V999 VALUE 0.005. 01 TOTAL-F PIC 9(3)V99 VALUE 0. 01 SUM-F PIC 9(3)V9999 VALUE 0. 01 WIDE-F PIC 9(1)V99 VALUE 0. PROCEDURE DIVISION. PERFORM 12 TIMES ADD ITEM-F TO TOTAL-F ADD ITEM-F TO SUM-F END-PERFORM. DISPLAY "rounded each time [" TOTAL-F "]". DISPLAY "kept in full [" SUM-F "]". COMPUTE WIDE-F ROUNDED = SUM-F. DISPLAY "rounded once [" WIDE-F "]". STOP RUN.

cobc -x -free -Wall -o round round.cob
./round

rounded each time [000.00]
kept in full [000.0600]
rounded once [0.06]

Twelve times half a cent is six cents. The first line says zero, because each addition of 0.005 into a two-decimal field was cut before the next one arrived; the second says six hundredths of a cent, which is the true sum; the third rounds it once and gets the answer a person expects.

This is the arithmetic of any fee or tax charged per item. If the business rule is "round the fee of each item to a cent and add the cents", the first line is correct and its zero is the rule, not a bug. If the rule is "compute the total and round it", the third line is correct. The program has to know which rule it implements, and the two totals differ by six cents on twelve lines.

The rule for a total

  1. Accumulate in a field with at least two more decimals than the answer.
  2. Round once, where the answer is stored or printed.
  3. Check the total against a value computed another way, and keep the check in the program.
  4. Write down which rounding rule the business wants, because both are defensible and they disagree.

The next two lessons take the output side: the pictures that print an amount with a currency sign and thousands separators (edited numeric output), and then a real bug of exactly this shape, found and fixed (an arithmetic bug).

Exercises

  1. Change the deposit to 25.755 and run the balance program. What does the check say now, and why?
  2. Replace UNTIL MONTH-F > 12 with UNTIL MONTH-F = 12 and run it. How many deposits were made, and what is the total?
  3. Add a second check that computes the total as DEPOSIT-F * (MONTH-F - 1). Does it agree with the loop?
  4. Round the half-cent item to a cent inside the loop and add the rounded values. What is the total, and which line of the run does it match?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is in the loop counter when PERFORM VARYING finishes?

    Answer

    One past the last value that ran the body

    The counter is incremented before the condition is tested, so the test that ends the loop sees one more than the last iteration.

  2. Why check a running total against a value computed another way?

    Answer

    A loop can be wrong in one iteration and right in the others

    An independent expression does not share the loop bug, so the comparison is evidence rather than a restatement.

  3. Why does adding 0.005 twelve times into a two-decimal field give zero?

    Answer

    Each addition is cut to two decimals before the next one arrives

    Truncation happens at every store, so half a cent becomes nothing at the first addition and the total never grows.

  4. Which total is right when the business rounds each item to a cent?

    Answer

    The sum of the rounded items, not the rounded sum

    The rule decides the arithmetic: rounding per item and rounding once give different totals, and the program must implement the stated one.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 DEPOSIT-F PIC 9(3)V99 VALUE 25.75. 01 TOTAL-F PIC 9(5)V99 VALUE 0. 01 MONTH-F PIC 9(2) VALUE 0. PROCEDURE DIVISION. PERFORM VARYING MONTH-F FROM 1 BY 1 UNTIL MONTH-F > 12 ADD DEPOSIT-F TO TOTAL-F END-PERFORM. DISPLAY "[" TOTAL-F "]". STOP RUN.

    Answer

    [00309.00]

    Twelve deposits of twenty-five seventy-five add up to three hundred and nine exactly, because every value is whole cents.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 MONTH-F PIC 9(2) VALUE 0. PROCEDURE DIVISION. PERFORM VARYING MONTH-F FROM 1 BY 1 UNTIL MONTH-F > 12 CONTINUE END-PERFORM. DISPLAY "[" MONTH-F "]". STOP RUN.

    Answer

    [13]

    The loop increments before testing, so the value that fails the test is thirteen and it stays in the field.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 ITEM-F PIC 9(1)V999 VALUE 0.005. 01 TOTAL-F PIC 9(3)V99 VALUE 0. PROCEDURE DIVISION. PERFORM 12 TIMES ADD ITEM-F TO TOTAL-F END-PERFORM. DISPLAY "[" TOTAL-F "]". STOP RUN.

    Answer

    [000.00]

    Half a cent disappears at the first addition into a two-decimal field, so twelve of them still add nothing.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 ITEM-F PIC 9(1)V999 VALUE 0.005. 01 SUM-F PIC 9(3)V9999 VALUE 0. 01 WIDE-F PIC 9(1)V99 VALUE 0. PROCEDURE DIVISION. PERFORM 12 TIMES ADD ITEM-F TO SUM-F END-PERFORM. COMPUTE WIDE-F ROUNDED = SUM-F. DISPLAY "[" SUM-F "][" WIDE-F "]". STOP RUN.

    Answer

    [000.0600][0.06]

    The wide field keeps the half cents, so the sum is six hundredths of a cent, and one rounding at the end gives six cents.

  9. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 DEPOSIT-F PIC 9(3)V99 VALUE 25.75. 01 CHECK-F PIC 9(5)V99 VALUE 0. PROCEDURE DIVISION. COMPUTE CHECK-F = DEPOSIT-F * 12. DISPLAY "[" CHECK-F "]". STOP RUN.

    Answer

    [00309.00]

    One multiplication by twelve is the same total the loop reaches, which is what makes it a check.

  10. A fee of 0.335 is charged four times and the total is stored rounded. What does a program that rounds each fee first add up to?

    Answer

    1.36, four fees of 0.34

    Rounding 0.335 to a cent gives 0.34, and four of those are 1.36, while the total rounded once is 1.34; the two rules differ by two cents.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev