Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Blog/Foundations

Why is 0.1 + 0.2 not 0.3 in code?

Every language says 0.30000000000000004. Here is why binary can't store tenths, what it costs, and how to dodge it.

Derpx0621 Nov · 3 min read
Contents
Why can't a computer store 0.1?What does a double actually hold?So why does 0.1 + 0.2 go wrong?Does it matter in real life?How do I store money safely?How do I compare decimals?Wait, so is the computer wrong?Sources
0% read · 3 min leftBack to top ↑
On this page0%
Contents
Why can't a computer store 0.1?What does a double actually hold?So why does 0.1 + 0.2 go wrong?Does it matter in real life?How do I store money safely?How do I compare decimals?Wait, so is the computer wrong?Sources

Open any Python shell, type 0.1 + 0.2, and it prints 0.30000000000000004. Not 0.3.

Every language does this, and it isn't a bug. It's a side effect of how computers store numbers, and once you see it, you'll never trust == on decimals again.

Why can't a computer store 0.1?

One third can't be written exactly in decimals. Try it: you get 0.333... and it never ends. You can only write a few threes and round it.

Computers have the same kind of problem with 0.1. They count in binary (only 0s and 1s), so they build numbers from halves, quarters, eighths and so on, and one tenth can't be made exactly from those.

So 0.1 is the "1/3" of binary. Goldberg's classic paper says it plainly: 0.1 "has an infinite repeating representation" in binary.

The computer has to cut the pattern somewhere, so it stores the closest value it can.

The chop is the step where the tiny error sneaks in. But what exactly gets stored?

What does a double actually hold?

Most languages use the IEEE 754 double. It's a standard that squeezes each number into 64 bits. The number part (the "precision") gets 53 bits.

1 bit: sign (plus or minus)
11 bits: exponent (how big)
52 bits: fraction (the digits, plus one hidden bit)
64 bits per double. MDN lists the same split.

Python's docs show that 0.1 is stored as the fraction 3602879701896397 / 2 ** 55. That is close to 1/10 but not equal. The exact stored value is:

0.1000000000000000055511151231257827021181583404541015625

So the computer never sees 0.1. It sees a number a tiny bit bigger. What happens when we add two of those?

So why does 0.1 + 0.2 go wrong?

Here are the exact stored values, as Python shows them. 0.2 is also a tiny bit too big, and the sum lands just above 0.3 after rounding.

NumberExact value stored
0.10.1000000000000000055511151231257827021181583404541015625
0.20.200000000000000011102230246251565404236316680908203125
0.1 + 0.20.3000000000000000444089209850062616169452667236328125

Click Next to follow the numbers one at a time.

0.1
0.2
Add
Result
0.3

0.1 is stored a tiny bit too big

Step 1 of 5
Why 0.1 + 0.2 == 0.3 is False.

In the last step, the answer isn't random. It's a neighbour of 0.3, not 0.3 itself. That's why 0.1 + 0.2 == 0.3 is False.

Maths on paperOn paper: 0.1 + 0.2 = 0.3
→
A computerIn a double: 0.30000000000000004
Tiny errors, but they don't cancel.

It's a tiny error. But does it ever matter?

Does it matter in real life?

Mostly it's a 17th-digit problem, so a game score won't care. But small errors can add up. Goldberg's paper has a money example: working out compound interest with sloppy rounding gave 37615.45whentheexactanswerwas37615.45 when the exact answer was 37615.45whentheexactanswerwas37614.05. That's $1.40 off.

And equality checks go wrong. A loop that waits for a value to be exactly 0.3 can wait forever, because the value never lands exactly there.

So what do we do about it? Two fixes.

How do I store money safely?

Integers are exact (up to 2^53 - 1 in JavaScript, per MDN). So don't store 10.50. Store 1050 cents, and only add the decimal point when you show it.

Python
price_cents = 1050
total = price_cents * 3   # 3150, exact
print(total / 100)        # 31.5, only for display

If you need real decimal maths, Python's docs point to the decimal module, "suitable for accounting applications".

How do I compare decimals?

For things that really are fractions (like sensor readings), never ask "are these equal?". Ask "are these close enough?".

Python
import math
math.isclose(0.1 + 0.1 + 0.1, 0.3)   # True

Goldberg warns this isn't a cure-all, because you still have to choose how close is "close". Pick it based on what you're measuring.

  1. Money or counts? Use integers (such as cents).
  2. Need exact decimals? Use a decimal type.
  3. Measurements? Compare with a tolerance, never ==.
  4. Showing a number? Round only at the end, for display.

Wait, so is the computer wrong?

No. It's doing the best it can with 53 bits. The mismatch comes from expecting binary to store decimal fractions exactly.

Key takeaways
  • 0.1 can't be stored exactly in binary, just like 1/3 can't be in decimal.
  • A double keeps the nearest 53-bit value, so 0.1 + 0.2 comes out as 0.30000000000000004.
  • Store money as whole numbers (cents), not as decimals.
  • Compare decimals with a tolerance, not ==.

Sources

  • Python docs: Floating-Point Arithmetic: Issues and Limitations
  • Goldberg, What Every Computer Scientist Should Know About Floating-Point Arithmetic, ACM Computing Surveys, 1991
  • MDN: Number
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Foundations · 4 minHow does Git work inside? (Snapshots, not changes)Foundations · 5 minHow do maps apps find the fastest route?Foundations · 4 minWhy does one emoji have a length of 5, 7 or 17?
Start here
01How does Git work inside? (Snapshots, not changes)02How does your computer know the time? (And why it can be wrong)03What is a Bloom filter? A tiny "definitely not" checker