An arithmetic bug, found and fixed
The arithmetic of the last ten lessons, used on one invoice. Three lines, a tax rate, a total, and a result that is a cent lower than the one the customer works out with a calculator. The bug is real, it is common, and the fix is one word.
The invoice that is a cent short
Three lines, an eight and a quarter percent tax, and the totals as the program computes them:
IDENTIFICATION DIVISION.
PROGRAM-ID. BUG1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 LINE-A PIC 9(3)V99 VALUE 12.50.
01 LINE-B PIC 9(3)V99 VALUE 10.40.
01 LINE-C PIC 9(3)V99 VALUE 10.40.
01 SUB-F PIC 9(4)V99 VALUE 0.
01 TAX-RATE PIC 9(1)V9(4) VALUE 0.0825.
01 TAX-F PIC 9(3)V99 VALUE 0.
01 TOTAL-F PIC 9(4)V99 VALUE 0.
PROCEDURE DIVISION.
COMPUTE SUB-F = LINE-A + LINE-B + LINE-C.
COMPUTE TAX-F = SUB-F * TAX-RATE.
COMPUTE TOTAL-F = SUB-F + TAX-F.
DISPLAY "subtotal [" SUB-F "]".
DISPLAY "tax [" TAX-F "]".
DISPLAY "total [" TOTAL-F "]".
STOP RUN.
cobc -x -free -Wall -o bug1 bug1.cob
./bug1
subtotal [0033.30]
tax [002.74]
total [0036.04]
By hand: twelve fifty and ten forty and ten forty are thirty-three thirty. Eight and a quarter percent of that is 2.74725, which is nearer two seventy-five than two seventy-four, and the invoice should be 36.05. The program says 36.04, the build is clean, and nothing in the run suggests a problem.
Where the cent went
The tax statement is a plain COMPUTE into a two-decimal field. The temporary held 2.74725, the receiver has two decimals, and the default is to cut, so the third decimal went away: 002.74. The total then adds what is in the field, not what the temporary held, and the cent is gone.
The rate field had four decimals and the product had five, so the precision was never the problem. The problem is that the program said where the cents end without saying what should happen to the digits beyond them, and the language answers that question with truncation unless it is told otherwise.
The fix
One clause on the statement that stores cents:
IDENTIFICATION DIVISION.
PROGRAM-ID. BUG2.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 LINE-A PIC 9(3)V99 VALUE 12.50.
01 LINE-B PIC 9(3)V99 VALUE 10.40.
01 LINE-C PIC 9(3)V99 VALUE 10.40.
01 SUB-F PIC 9(4)V99 VALUE 0.
01 TAX-RATE PIC 9(1)V9(4) VALUE 0.0825.
01 TAX-F PIC 9(3)V99 VALUE 0.
01 TOTAL-F PIC 9(4)V99 VALUE 0.
PROCEDURE DIVISION.
COMPUTE SUB-F = LINE-A + LINE-B + LINE-C.
COMPUTE TAX-F ROUNDED = SUB-F * TAX-RATE.
COMPUTE TOTAL-F = SUB-F + TAX-F.
DISPLAY "subtotal [" SUB-F "]".
DISPLAY "tax [" TAX-F "]".
DISPLAY "total [" TOTAL-F "]".
IF TOTAL-F = 36.05
DISPLAY "check passed"
ELSE
DISPLAY "check failed"
END-IF.
STOP RUN.
cobc -x -free -Wall -o bug2 bug2.cob
./bug2
subtotal [0033.30]
tax [002.75]
total [0036.05]
check passed
The tax is a cent higher, the total with it, and the amount now agrees with the calculator. Nothing else in the program changed: the inputs, the pictures and the order of the statements are the same.
The check that proves it
The IF at the end is not decoration. It compares the computed total with the amount the invoice is expected to have, and it is the statement that turns the fix into evidence. Without it, the next change to a rate or a line is tested by reading the output and hoping; with it, the program says whether the arithmetic still holds.
The literal 36.05 is the right thing to write in a check like this, because it comes from the specification and not from the program. A check that recomputes the total the same way the program does proves only that the program is consistent with itself.
What the bug teaches
- Truncation is the default of every arithmetic statement, and the statement that stores cents is where it bites.
- A build with -Wall has nothing to say about a result that is cut to its receiver: the loss is defined behaviour, not an error. The bug is found by arithmetic, not by the compiler.
- The test value has to be one that exposes the bug. A subtotal of 33.30 with a rate of 8.25 percent gives a third decimal of 7, so the cut is visible; most totals in a test suite would have hidden it.
- The fix belongs on the statement that decides the cents, and the check belongs in the program, not in the head of the person who ran it once.
This is the last lesson of the arithmetic part. The next part is text: fields of characters, the functions that search and change them, and the statements that build a line for a report. Alphanumeric fields and padding is the first of them.
Exercises
- Change LINE-A to 12.51 and compute the tax by hand. Does the truncated program still lose the same cent?
- Add a fourth line of 0.05 to the invoice and run both programs. Which total is now wrong by two cents?
- Replace the literal in the check with a field that holds 36.05 from a VALUE clause and run the program. Is the check still worth having?
- Write a second invoice in the same program, with a rate of 7.5 percent and a subtotal that gives an exact half cent, and check both totals. What does the half-up rule do to the second one?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why is the total a cent short before the fix?
Answer
The tax was stored with two decimals and the third was cut, not rounded
The product was 2.74725 and the plain COMPUTE cut it to 2.74, so the cent never reached the total.
-
Which clause fixes the tax statement?
Answer
ROUNDED on the statement that stores the tax
The digits are already there; ROUNDED is what tells the statement to use the one after the last stored place.
-
Why is a check worth keeping in the program?
Answer
It compares the total with the specification value, so a later change is caught
A comparison against a value that comes from outside the computation turns the fix into evidence and guards later edits.
-
What does the compiler say about a result that is truncated to its receiver?
Answer
Nothing: truncation is defined behaviour, not an error
The cut is what the language specifies, so the build stays silent and the bug is found by checking the arithmetic.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 SUB-F PIC 9(4)V99 VALUE 33.30. 01 RATE-F PIC 9(1)V9(4) VALUE 0.0825. 01 TAX-F PIC 9(3)V99 VALUE 0. PROCEDURE DIVISION. COMPUTE TAX-F = SUB-F * RATE-F. DISPLAY "[" TAX-F "]". STOP RUN.Answer
[002.74]
The product is 2.74725 and the third decimal is dropped, so the field holds 2.74.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 SUB-F PIC 9(4)V99 VALUE 33.30. 01 RATE-F PIC 9(1)V9(4) VALUE 0.0825. 01 TAX-F PIC 9(3)V99 VALUE 0. PROCEDURE DIVISION. COMPUTE TAX-F ROUNDED = SUB-F * RATE-F. DISPLAY "[" TAX-F "]". STOP RUN.Answer
[002.75]
The digit after the cents is seven, so the half-up rule takes the tax up to 2.75.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 SUB-F PIC 9(4)V99 VALUE 33.30. 01 TAX-F PIC 9(3)V99 VALUE 2.74. 01 TOTAL-F PIC 9(4)V99 VALUE 0. PROCEDURE DIVISION. COMPUTE TOTAL-F = SUB-F + TAX-F. DISPLAY "[" TOTAL-F "]". STOP RUN.Answer
[0036.04]
Thirty-three thirty plus two seventy-four is thirty-six and four cents, exactly the wrong total of the bug.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 SUB-F PIC 9(4)V99 VALUE 33.30. 01 TAX-F PIC 9(3)V99 VALUE 2.75. 01 TOTAL-F PIC 9(4)V99 VALUE 0. PROCEDURE DIVISION. COMPUTE TOTAL-F = SUB-F + TAX-F. IF TOTAL-F = 36.05 DISPLAY "check passed" ELSE DISPLAY "check failed" END-IF. STOP RUN.Answer
check passed
A subtotal of 33.30 and a tax of 2.75 give 36.05, which is the value the specification check expects.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 AMT-F PIC 9(3)V99 VALUE 0. PROCEDURE DIVISION. COMPUTE AMT-F = 8.25 * 0.33. DISPLAY "[" AMT-F "]". STOP RUN.Answer
[002.72]
The exact product is 2.7225, and the plain statement cuts it at two decimals to 2.72.
-
A test computes a total from a rate and a subtotal chosen so that the third decimal is a seven. The program has no ROUNDED. What does the test prove?
Answer
That the program cuts the result and the total is a cent low, so the bug is visible
The test value is chosen to make the cut visible; a value with a third decimal below five would have hidden the same bug.
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