Why money is decimal
COBOL has a type for money and a type for measurements, and they are not the same. Packed decimal stores the digits a person would write; a binary float stores a fraction in base two. The difference is invisible in most runs and fatal in a few, and ten dimes is the smallest program that shows it.
Ten dimes are not a dollar
One loop adds ten cents ten times. Once into a COMP-2 field, once into a COMP-3 field, and the program then asks the float whether it is a dollar:
IDENTIFICATION DIVISION.
PROGRAM-ID. TENTH.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 FLOAT-F COMP-2 VALUE 0.
01 I-F PIC 9(3) VALUE 0.
01 PACKED-F PIC S9(3)V99 COMP-3 VALUE 0.
PROCEDURE DIVISION.
PERFORM 10 TIMES
ADD 0.1 TO FLOAT-F
ADD 0.1 TO PACKED-F
END-PERFORM.
DISPLAY "float [" FLOAT-F "]".
DISPLAY "packed [" PACKED-F "]".
IF FLOAT-F = 1
DISPLAY "float equals 1"
ELSE
DISPLAY "float is not 1"
END-IF.
STOP RUN.
cobc -x -free -Wall -o tenth tenth.cob
./tenth
float [0.9999999999999998]
packed [+001.00]
float is not 1
The packed field holds a dollar and the float does not, by two parts in a hundred million million. A report that prints two decimals shows 1.00 for both, which is why the bug survives review; the comparison on the last line is where it shows, and it is also the line a program usually writes to test a total.
What a binary float is
A COMP-2 field holds a sign, an exponent and a mantissa of binary digits, which is the hardware representation almost every other language calls a double. A tenth is one over ten, and ten is two times five: the factor five never disappears, so the binary representation of 0.1 does not end. The machine stores the nearest number it can, the error is about 5.5e-17, and every addition carries it.
A decimal field has no such problem with a tenth. The digits 0 and 1 are stored as digits, the point is in the picture, and ten of them make one exactly. This is not a property of COBOL as a language; it is a property of the representation, and a mainframe has had it for money since the 1960s because it is the only one that reconciles.
The same additions in packed decimal
The second output line of the run above is the whole answer: +001.00. The picture is S9(3)V99, the field is packed decimal, and ten additions of a tenth are exact because each addition is exact.
The price of the exactness is a fixed number of decimal places. The field has two, so a fraction of a cent cannot be inside it; a program that works with tax rates keeps the rate in a wider decimal field, computes in a temporary, and rounds once at the point of storing cents, as ROUNDED and the lesson on the temporary show.
A third that comes back wrong
The float has a second failure that the packed field does not share: a value that is not a decimal fraction at all. Ten thirds, computed as a division and then multiplied back, in both kinds of field:
IDENTIFICATION DIVISION.
PROGRAM-ID. THIRD.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 FLOAT-F COMP-2 VALUE 0.
01 PACKED-F PIC S9(3)V99 COMP-3 VALUE 0.
PROCEDURE DIVISION.
COMPUTE FLOAT-F = 10 / 3.
COMPUTE FLOAT-F = FLOAT-F * 3.
DISPLAY "float 10/3*3 [" FLOAT-F "]".
COMPUTE PACKED-F = 10 / 3.
COMPUTE PACKED-F = PACKED-F * 3.
DISPLAY "packed 10/3*3 [" PACKED-F "]".
IF FLOAT-F = 10
DISPLAY "float equals 10"
ELSE
DISPLAY "float is not 10"
END-IF.
STOP RUN.
cobc -x -free -Wall -o third third.cob
./third
float 10/3*3 [9.999999999999998]
packed 10/3*3 [+009.99]
float is not 10
Both values are wrong and they are wrong for different reasons. The float is off by two parts in a thousand million million because three does not divide ten in base two. The packed field is off by a cent because its picture has two decimals and the division was stored before the multiplication: 3.33 times three is 9.99. The decimal answer is the one a business can live with, because the size of the error is visible in the picture and a wider intermediate field or a reordered expression fixes it; the float error is invisible until a comparison finds it.
Which field for money
- COMP-3 for an amount: exact decimal digits, two per byte, the representation a mainframe file and a DB2 column use.
- DISPLAY for an amount that has to be read by another system as text, and for any field that is printed.
- COMP-2 for a physical measurement: a temperature, a distance, a rate that will never be added to a cent. It is the type of a scientific program, not of a ledger.
- Never for money, and never compared with = when the value came out of a computation: the comparison above returned false for a sum that prints as a dollar.
One more trap connects the two kinds of field: moving a float into a decimal field truncates. The ten dimes, moved into a PIC 9(3)V99, are not the dollar that the display suggested:
IDENTIFICATION DIVISION.
PROGRAM-ID. MOVE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 FLOAT-F COMP-2 VALUE 0.
01 DEC-F PIC 9(3)V99 VALUE 0.
PROCEDURE DIVISION.
PERFORM 10 TIMES
ADD 0.1 TO FLOAT-F
END-PERFORM.
DISPLAY "float [" FLOAT-F "]".
MOVE FLOAT-F TO DEC-F.
DISPLAY "moved [" DEC-F "]".
STOP RUN.
cobc -x -free -Wall -o move move.cob
./move
float [0.9999999999999998]
moved [000.99]
The display of the float rounds to 1.00 for the reader, the move takes the digits it finds below one, and the ledger loses a cent. A program that has a float anywhere near an amount rounds it explicitly before the move, and a program that is written to keep money does not have a float in it at all.
Exercises
- Add twenty-five cents a hundred times in a float and in a packed field and print both. Which one is 25.00, and by how much is the other off?
- Store 0.1 in a COMP-2 and print it with sixteen decimals by moving it to a PIC 9V9(15). What does the field hold?
- Compute 1 / 3 in a packed field of six decimals and multiply by three. How many nines does the answer have, and what would round it to one?
- Take a float total and compare it with the same total in packed decimal after each of ten additions. On which addition does the difference appear first?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why is the sum of ten dimes not exactly one in a COMP-2 field?
Answer
One tenth has no finite representation in base two, so each addition carries an error
The binary mantissa cannot write one tenth exactly, so the stored value is the nearest one and the errors accumulate.
-
What does packed decimal store?
Answer
The decimal digits themselves, exactly as written
COMP-3 keeps two digits per byte and a sign nibble, so a tenth is one digit and ten of them are a one with no error.
-
Which usage belongs on an amount of money?
Answer
COMP-3, or DISPLAY when the field is read as text
Money needs exact decimal digits, which is what packed decimal and zoned decimal provide; a binary float does not.
-
A float sum that prints as 1.00 fails the test
IF F = 1. Why?Answer
The stored value is slightly below one, and the comparison uses all its digits
The display rounds for the reader while the comparison compares the stored value, which is 0.9999999999999998.
-
What does this program print? (The loop adds 0.1 ten times.)
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 FLOAT-F COMP-2 VALUE 0. PROCEDURE DIVISION. PERFORM 10 TIMES ADD 0.1 TO FLOAT-F END-PERFORM. DISPLAY "[" FLOAT-F "]". STOP RUN.Answer
[0.9999999999999998]
Each addition of one tenth stores the nearest binary value, and the accumulated error lands just under one.
-
What does this program print? (The loop adds 0.1 ten times.)
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 PACKED-F PIC S9(3)V99 COMP-3 VALUE 0. PROCEDURE DIVISION. PERFORM 10 TIMES ADD 0.1 TO PACKED-F END-PERFORM. DISPLAY "[" PACKED-F "]". STOP RUN.Answer
[+001.00]
Ten exact tenths are exactly one, and the packed picture prints the sign and the three integer digits.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 FLOAT-F COMP-2 VALUE 0. PROCEDURE DIVISION. COMPUTE FLOAT-F = 10 / 3. COMPUTE FLOAT-F = FLOAT-F * 3. DISPLAY "[" FLOAT-F "]". STOP RUN.Answer
[9.999999999999998]
Three does not divide ten exactly in base two, and multiplying the stored quotient by three does not restore the ten.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 PACKED-F PIC S9(3)V99 COMP-3 VALUE 0. PROCEDURE DIVISION. COMPUTE PACKED-F = 10 / 3. COMPUTE PACKED-F = PACKED-F * 3. DISPLAY "[" PACKED-F "]". STOP RUN.Answer
[+009.99]
The division stores 3.33 in two decimals, and three times that is 9.99: a decimal error, visible in the picture.
-
What does this program print? (The loop adds 0.1 ten times.)
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 FLOAT-F COMP-2 VALUE 0. 01 DEC-F PIC 9(3)V99 VALUE 0. PROCEDURE DIVISION. PERFORM 10 TIMES ADD 0.1 TO FLOAT-F END-PERFORM. MOVE FLOAT-F TO DEC-F. DISPLAY "[" DEC-F "]". STOP RUN.Answer
[000.99]
The move truncates the stored value, which is just below one, so the field holds ninety-nine cents.
-
A program adds 0.01 a hundred times in a COMP-2 field and compares the total with 1. What should it do instead?
Answer
Keep the total in a decimal field and round once at the end
Money belongs in a decimal field, where every addition of a cent is exact and the comparison is meaningful.
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