Decimal money: double, cents and a decimal type
One invoice, three lines of 100.05, 200.10 and 300.15, thirteen per cent tax on each line with half a cent rounded up, and a total. Four programs compute it: one in COBOL, and three in C++ with the three number types a C++ programmer reaches for. The invoice is small on purpose, because the difference is not in the size of the sum but in what the machine does with a hundredth of a unit.
The invoice in COBOL
The amounts are COMP-3: packed decimal, two digits to a byte, exact at every step. ROUNDED makes the tax round half up, which is what an invoice needs, and the ON SIZE ERROR at the end shows what happens when a three-digit field is asked to hold a thousand:
IDENTIFICATION DIVISION.
PROGRAM-ID. INVOICE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-LINE-AMOUNT PIC 9(5)V99 COMP-3.
01 WS-TAX PIC 9(5)V99 COMP-3.
01 WS-TOTAL PIC 9(7)V99 COMP-3 VALUE 0.
01 WS-TAX-TOTAL PIC 9(7)V99 COMP-3 VALUE 0.
01 WS-GRAND PIC 9(7)V99 COMP-3.
01 WS-AMT-OUT PIC Z(5)9.99.
01 WS-TAX-OUT PIC Z(5)9.99.
01 WS-SMALL PIC 9(3)V99 COMP-3 VALUE 0.
01 WS-BIG PIC 9(3) VALUE 999.
PROCEDURE DIVISION.
MOVE 100.05 TO WS-LINE-AMOUNT
PERFORM SHOW-LINE
MOVE 200.10 TO WS-LINE-AMOUNT
PERFORM SHOW-LINE
MOVE 300.15 TO WS-LINE-AMOUNT
PERFORM SHOW-LINE
COMPUTE WS-GRAND = WS-TOTAL + WS-TAX-TOTAL
MOVE WS-TAX-TOTAL TO WS-TAX-OUT
MOVE WS-GRAND TO WS-AMT-OUT
DISPLAY "tax total " FUNCTION TRIM(WS-TAX-OUT)
DISPLAY "invoice " FUNCTION TRIM(WS-AMT-OUT)
ADD 1 TO WS-BIG
ON SIZE ERROR DISPLAY "size error: 999 + 1 does not fit PIC 9(3)"
END-ADD
DISPLAY "small field " WS-SMALL
STOP RUN.
SHOW-LINE.
COMPUTE WS-TAX ROUNDED = WS-LINE-AMOUNT * 13 / 100
ADD WS-LINE-AMOUNT TO WS-TOTAL
ADD WS-TAX TO WS-TAX-TOTAL
MOVE WS-LINE-AMOUNT TO WS-AMT-OUT
MOVE WS-TAX TO WS-TAX-OUT
DISPLAY "line " FUNCTION TRIM(WS-AMT-OUT)
" tax " FUNCTION TRIM(WS-TAX-OUT).
cobc -x -free -Wall -O2 -o invoice invoice.cob
./invoice
echo $?
line 100.05 tax 13.01
line 200.10 tax 26.01
line 300.15 tax 39.02
tax total 78.04
invoice 678.34
size error: 999 + 1 does not fit PIC 9(3)
small field 000.00
0
Two things in that output are the language and not the program. The tax on a hundred and five hundredths is 13.0065, and the field keeps two decimals, so ROUNDED gives 13.01 and not 13.00. And the ADD that does not fit leaves the target field untouched at 000.00 and runs the error branch, because ON SIZE ERROR is part of the statement: a COBOL programmer does not have to check a return code to find out that the number did not fit.
The invoice in double
The same numbers in double, with std::round to two decimals, and two extra lines: an equality test against the expected total, and ten million additions of one hundredth:
#include <cmath>
#include <cstdio>
int main() {
const double lines[] = {100.05, 200.10, 300.15};
double total = 0.0;
double tax_total = 0.0;
for (double amount : lines) {
double tax = std::round(amount * 0.13 * 100.0) / 100.0;
std::printf("line %.2f tax %.2f\n", amount, tax);
total += amount;
tax_total += tax;
}
double grand = total + tax_total;
std::printf("tax total %.2f\n", tax_total);
std::printf("invoice %.2f\n", grand);
std::printf("equals 678.34: %s\n", grand == 678.34 ? "yes" : "no");
std::printf("raw total %.17g\n", grand);
double drift = 0.0;
for (long i = 0; i < 10000000; ++i) {
drift += 0.01;
}
std::printf("10M x 0.01 %.17g\n", drift);
return 0;
}
g++ -std=c++23 -Wall -Wextra -O2 -o invoice_double invoice_double.cpp
./invoice_double
echo $?
line 100.05 tax 13.01
line 200.10 tax 26.01
line 300.15 tax 39.02
tax total 78.04
invoice 678.34
equals 678.34: no
raw total 678.33999999999992
10M x 0.01 99999.999986309689
0
The invoice prints correctly and is wrong. The total is 678.33999999999992, so grand == 678.34 is false, and a program that compares the total with the expected one, or uses it as a map key, or writes it to a ledger that compares digits, has a bug that no print of two decimals will show. The ten million additions make the same point from the other side: adding a hundredth ten million times should give exactly 100000.00, and double gives 99999.999986309689, a tenth of a cent short and drifting with every run that adds a different number of terms.
The invoice in integer cents
The C++ answer that every payments team arrives at: store cents in an int64_t and never let a float near the money. The rounding is an integer expression, and the printing splits the value into units and hundredths:
#include <cstdint>
#include <cstdio>
std::int64_t round_half_up(std::int64_t numerator, std::int64_t denominator) {
return (numerator + denominator / 2) / denominator;
}
int main() {
const std::int64_t lines[] = {10005, 20010, 30015};
std::int64_t total = 0;
std::int64_t tax_total = 0;
for (std::int64_t amount : lines) {
std::int64_t tax = round_half_up(amount * 13, 100);
std::printf("line %lld.%02lld tax %lld.%02lld\n",
static_cast<long long>(amount / 100), static_cast<long long>(amount % 100),
static_cast<long long>(tax / 100), static_cast<long long>(tax % 100));
total += amount;
tax_total += tax;
}
std::int64_t grand = total + tax_total;
std::printf("tax total %lld.%02lld\n", static_cast<long long>(tax_total / 100),
static_cast<long long>(tax_total % 100));
std::printf("invoice %lld.%02lld\n", static_cast<long long>(grand / 100),
static_cast<long long>(grand % 100));
std::printf("equals 678.34: %s\n", grand == 67834 ? "yes" : "no");
return 0;
}
g++ -std=c++23 -Wall -Wextra -O2 -o invoice_cents invoice_cents.cpp
./invoice_cents
echo $?
line 100.05 tax 13.01
line 200.10 tax 26.01
line 300.15 tax 39.02
tax total 78.04
invoice 678.34
equals 678.34: yes
0
Byte for byte the same invoice as COBOL, including the rounding, because both are doing integer arithmetic on a fixed number of decimal places. That is the whole of the comparison: COBOL gives every program that arithmetic by default, and C++ gives it to a program that asks for it by choosing int64_t and writing the rounding itself.
The invoice in a decimal type
The third C++ answer keeps four decimal places in the intermediate tax so that the rounding happens once, at the end, which is what ROUNDED on a COBOL field with more decimals does. It is a struct of forty lines, and this is a small version of what a decimal library gives you:
#include <cstdint>
#include <cstdio>
struct Dec4 {
std::int64_t units;
static Dec4 from_cents(std::int64_t cents) { return Dec4{cents * 100}; }
Dec4 times_percent(int percent) const {
std::int64_t scaled = units * percent;
std::int64_t rounded = (scaled + 50) / 100;
return Dec4{rounded};
}
std::int64_t to_cents() const { return (units + 50) / 100; }
};
void print_cents(const char* label, std::int64_t cents) {
std::printf("%s%lld.%02lld\n", label, static_cast<long long>(cents / 100),
static_cast<long long>(cents % 100));
}
int main() {
const std::int64_t lines[] = {10005, 20010, 30015};
std::int64_t total = 0;
std::int64_t tax_total = 0;
for (std::int64_t amount : lines) {
Dec4 tax = Dec4::from_cents(amount).times_percent(13);
std::int64_t cents = tax.to_cents();
print_cents("line tax ", cents);
total += amount;
tax_total += cents;
}
print_cents("tax total ", tax_total);
print_cents("invoice ", total + tax_total);
return 0;
}
g++ -std=c++23 -Wall -Wextra -O2 -o invoice_fixed invoice_fixed.cpp
./invoice_fixed
echo $?
line tax 13.01
line tax 26.01
line tax 39.02
tax total 78.04
invoice 678.34
0
Four runs, four answers
The four programs, and the answer each gave on this machine:
| Program | Number type | Total | Equals 678.34 |
|---|---|---|---|
| COBOL | COMP-3, 2 decimals | 678.34 | not tested |
| C++ double | binary floating point | 678.34 printed, 678.33999999999992 stored | no |
| C++ cents | int64_t hundredths | 678.34 | yes |
| C++ decimal | int64_t ten-thousandths | 678.34 | not tested |
COBOL did not have to be told. The picture 9(7)V99 is a decimal number of a fixed shape, the arithmetic is decimal, and the compiler emits calls to the decimal routines of the runtime, which is why a COBOL money field is slower than an int and exact. A C++ program reaches the same place with int64_t cents or a decimal library, and it has to choose to. The one option that is never right for money is the one the language gives by default.
The COBOL lessons behind this page are COMP-3, ROUNDED, ON SIZE ERROR and why money is decimal. The next page leaves the number alone and compares the record: the same thirty-seven bytes as a C++ struct and as a COBOL record description.
The COBOL was built with cobc (GnuCOBOL) 3.2.0 and the C++ with g++ (Alpine 15.2.0) 15.2.0, both with -O2, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container with nproc 4.
Article author: Arthur Isaev