Floating Point Logic
Why computers are bad at decimal math, and how we fix it.
The Binary Gap
Computers store numbers in binary (base-2). While integers translate perfectly, fractions often don't.
In base-10 (decimal), 1/3 is 0.333... (repeating). You cannot write it down perfectly. In base-2 (binary), 1/10 (0.1) is 0.0001100110011... (repeating).
Because computers have limited memory (64 bits for "double precision"), they must chop off the repeating tail. precise number becomes an approximation. When you do math with these approximations, the tiny errors accumulate.
DecimalBinary Approximation (IEEE 754)
0.50.1 (Exact)
0.250.01 (Exact)
0.10.00011001100110011001100110011...
The Implication
If you calculate interest on $10,000,000 daily for a year using standard floating point logic, you could lose or gain significant cents purely due to memory truncation.
Commonrule Solution:
We use BigDecimal libraries that store "0.1" as two integers (1 and 10) rather than a binary fraction.
We use BigDecimal libraries that store "0.1" as two integers (1 and 10) rather than a binary fraction.