Intermediate precision
An arithmetic statement does not compute in the receiving field. It computes in a temporary of its own, with more digits than the receiver, and writes the result once at the end. Where that temporary ends is the difference between a total that reconciles and one that is a cent off.
A temporary wider than the receiver
One eighth in a field of two decimals is 0.12, cut. One eighth times eight, in the same field, is one: the division was not cut before the multiplication, because the product was computed first, in the temporary.
IDENTIFICATION DIVISION.
PROGRAM-ID. PRECISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ONE-F PIC 9(2) VALUE 1.
01 EIGHT-F PIC 9(2) VALUE 8.
01 X-VAL PIC 9(1)V99 VALUE 0.
01 Y-VAL PIC 9(1)V999 VALUE 0.
PROCEDURE DIVISION.
COMPUTE X-VAL = ONE-F / EIGHT-F * EIGHT-F.
DISPLAY "1/8*8 in V99 [" X-VAL "]".
COMPUTE Y-VAL = ONE-F / EIGHT-F.
DISPLAY "1/8 in V999 [" Y-VAL "]".
COMPUTE X-VAL = ONE-F / EIGHT-F.
DISPLAY "1/8 in V99 [" X-VAL "]".
STOP RUN.
cobc -x -free -Wall -o precision precision.cob
./precision
1/8*8 in V99 [1.00]
1/8 in V999 [0.125]
1/8 in V99 [0.12]
The three lines are one value seen through three pictures. The temporary held 0.125, which the second line prints because its receiver has three decimals; the third line cuts the same value to two, and the first line sees the whole thing before any cut happens at all.
The temporary has a limit
The temporary is wide but finite, and a repeating fraction exhausts it. A third, times three, is the classic case:
IDENTIFICATION DIVISION.
PROGRAM-ID. CHAIN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A-VAL PIC 9(3) VALUE 1.
01 B-VAL PIC 9(3) VALUE 3.
01 C-VAL PIC 9(3) VALUE 3.
01 T-VAL PIC 9(1)V99 VALUE 0.
01 X-VAL PIC 9(1)V99 VALUE 0.
PROCEDURE DIVISION.
DIVIDE B-VAL INTO A-VAL GIVING T-VAL.
DISPLAY "1 / 3 [" T-VAL "]".
MULTIPLY C-VAL BY T-VAL.
DISPLAY "then * 3 [" T-VAL "]".
COMPUTE X-VAL = A-VAL / B-VAL * C-VAL.
DISPLAY "one compute [" X-VAL "]".
COMPUTE X-VAL = A-VAL * C-VAL / B-VAL.
DISPLAY "reordered [" X-VAL "]".
STOP RUN.
cobc -x -free -Wall -o chain chain.cob
./chain
1 / 3 [0.33]
then * 3 [0.99]
one compute [0.99]
reordered [1.00]
The first two lines are the chain of statements: the division stores 0.33 in the receiver, and the multiplication reads that 0.33, so the answer is 0.99. The third line is more interesting: one COMPUTE with the same order gives 0.99 too, because the temporary holds only so many decimals of one third. The fourth line is the same arithmetic with the multiplication first, and it is exact: nothing is lost until the final division, and that division is exact for these values.
How wide the temporary is
A receiver with nine or twelve decimals shows how far the division goes before it stops:
IDENTIFICATION DIVISION.
PROGRAM-ID. WIDE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 A-FLD PIC 9 VALUE 1.
01 B-FLD PIC 9 VALUE 3.
01 V9-F PIC 9(1)V9(9) VALUE 0.
01 V12-F PIC 9(1)V9(12) VALUE 0.
PROCEDURE DIVISION.
COMPUTE V9-F = A-FLD / B-FLD * B-FLD.
DISPLAY "9 decimals [" V9-F "]".
COMPUTE V12-F = A-FLD / B-FLD.
DISPLAY "12 decimals [" V12-F "]".
STOP RUN.
cobc -x -free -Wall -o wide wide.cob
./wide
9 decimals [0.999999999]
12 decimals [0.333333333333]
The second line says the division produced at least twelve threes, so the temporary is not the two decimals of a price. The first line says it is not infinite: the threes stop somewhere, three times what is left is 0.999999999, and no picture can print the one that was never there.
That is the same reason a binary float fails at the next lesson, in a place where COBOL is usually safe: the fault is not decimal arithmetic, it is a fraction that cannot be written with a finite number of digits.
A chain of statements loses more
A single COMPUTE has one temporary and one cut. A chain of verbs has a cut per statement, because each one stores into its receiver, and the next statement reads the stored value. Three divisions of a price in a row lose three digits, where one expression would have lost one.
The rule that follows is old and still good: keep the intermediate values in a field with more decimals than the final answer, and round once, at the end. A field of four decimals for cents keeps two guard digits, and the running total is the case where that matters most.
Order is a decision
A / B * C and A * C / B are the same formula on paper and different programs in a machine with a finite temporary. When both orders are legal, the one that divides last is the one that keeps the digits, and it is usually also the one that explains the intent: multiply by the count first, then split the total.
The other decision is where to round. Rounding each intermediate value and rounding once at the end give different answers when there are more than two steps, and the difference is not a bug in either statement; it is a property of the algorithm, and the lesson that hunts such a bug starts from exactly this paragraph.
Exercises
- Compute 1 / 7 * 7 into a field with two decimals and into one with six. Explain both results from the width of the temporary.
- Write the mean of three values as one COMPUTE and as three statements. Which one is closer to the exact value, and by how much?
- Compute 100.00 / 3 * 3 and 100.00 * 3 / 3. Which one gives a hundred back, and what does the other one give?
- Take the chain program and store the intermediate in a field of six decimals instead of two. What does the final answer become, and why does it still not reach one?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Where does an arithmetic statement compute its result?
Answer
In a temporary wider than the receiving field, then written once
The receiver only sets the final picture; the expression is evaluated in a temporary that keeps more digits.
-
Why does one COMPUTE of 1 / 3 * 3 give 0.99 in two decimals?
Answer
The temporary holds a finite number of decimals of one third, so the product is just under one
One third needs infinitely many digits, the temporary stops somewhere, and three times the truncated value is 0.999...
-
Which rewrite of A / B * C is exact when C equals B?
Answer
A * C / B: multiply first and divide once
Doing the division last keeps the exact product in the temporary, so the final division by B undoes the multiplication exactly.
-
Why does a chain of DIVIDE and MULTIPLY lose more than one COMPUTE?
Answer
Each statement stores into its receiver, so every step starts from the cut value
A COMPUTE cuts once at the end; a chain cuts at every statement, and the next statement reads the cut value.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 ONE-F PIC 9(2) VALUE 1. 01 EIGHT-F PIC 9(2) VALUE 8. 01 X-VAL PIC 9(1)V99 VALUE 0. PROCEDURE DIVISION. COMPUTE X-VAL = ONE-F / EIGHT-F * EIGHT-F. DISPLAY "[" X-VAL "]". STOP RUN.Answer
[1.00]
One eighth is exact in decimal, the temporary keeps 0.125, and multiplying by eight gives one exactly.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 ONE-F PIC 9(2) VALUE 1. 01 EIGHT-F PIC 9(2) VALUE 8. 01 X-VAL PIC 9(1)V99 VALUE 0. PROCEDURE DIVISION. COMPUTE X-VAL = ONE-F / EIGHT-F. DISPLAY "[" X-VAL "]". STOP RUN.Answer
[0.12]
The temporary holds 0.125, and the two-decimal receiver cuts the third digit rather than rounding it.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-VAL PIC 9(3) VALUE 1. 01 B-VAL PIC 9(3) VALUE 3. 01 T-VAL PIC 9(1)V99 VALUE 0. PROCEDURE DIVISION. DIVIDE B-VAL INTO A-VAL GIVING T-VAL. MULTIPLY B-VAL BY T-VAL. DISPLAY "[" T-VAL "]". STOP RUN.Answer
[0.99]
The division stores 0.33 and the multiplication reads that stored value, so three times it is 0.99.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-VAL PIC 9(3) VALUE 1. 01 B-VAL PIC 9(3) VALUE 3. 01 C-VAL PIC 9(3) VALUE 3. 01 X-VAL PIC 9(1)V99 VALUE 0. PROCEDURE DIVISION. COMPUTE X-VAL = A-VAL * C-VAL / B-VAL. DISPLAY "[" X-VAL "]". STOP RUN.Answer
[1.00]
The product is three, and three divided by three is exact, so the receiver holds one with its two decimals.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-FLD PIC 9 VALUE 1. 01 B-FLD PIC 9 VALUE 3. 01 V9-F PIC 9(1)V9(9) VALUE 0. PROCEDURE DIVISION. COMPUTE V9-F = A-FLD / B-FLD * B-FLD. DISPLAY "[" V9-F "]". STOP RUN.Answer
[0.999999999]
Nine decimals show what the two-decimal run hides: the truncated third, multiplied by three, is a string of nines.
-
A program divides 100.00 by 3 and then multiplies by 3, with two decimals at each step. What is the total?
Answer
99.99, because the division was cut before the multiplication
The intermediate is stored as 33.33, and three times that is 99.99; multiplying first would have kept the hundred.
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