Why 0.1 + 0.2 is not 0.3 on some calculators.
Why computers write 0.1 + 0.2 as 0.30000000000000004, why it matters for money and percentages, and how a calculator can keep decimals exact.
By Suraj Kumar4 min read
Type 0.1 + 0.2 into a programming language and you often get 0.30000000000000004. Many web calculators are built on the same arithmetic, so they can be off in the last place too, and it shows up in percentages and money.
Computers count in halves, quarters and eighths.
Most software stores numbers in binary floating point, which holds about 15 to 17 significant digits as sums of powers of two. A tenth is not a sum of powers of two, just as a third is not a finite decimal, so 0.1 is stored as a number very slightly off, and adding two of those errors shows in the last digit.
Keeping numbers exact instead.
KeepSimp's calculators keep every number you type as an exact fraction: 0.1 is 1/10 and 0.2 is 2/10, so their sum is exactly 3/10, written 0.3. A third stays a third, and 100! is written in all 158 digits.
Only when an answer cannot be a fraction, such as √2 or sin 45°, is it worked out as a decimal, to 60 significant digits, and then shown to 12. That way the last digit shown is right, the same promise Android's calculator makes.
When it matters.
For a quick sum the difference is invisible once rounded. It matters when answers are compared for equality, added up many times as on a shop's tape, or rounded at a half, as with money. A calculator that keeps exact values rounds 2.675 to 2.68, not 2.67.
Further reading
Topics: 0.1 + 0.2 · 0.30000000000000004 · floating point error · exact calculator · decimal precision · rounding error
A little less theory. A little more doing.
Try it on your own terms, with a free browser tool.
Try the tool