Pointer Walk

Floating Point

0.1 + 0.2 is not 0.3. 0.1 has no exact binary form, so the sum lands a hair past 0.3.

Languages and RuntimesBeginner8 min read

0.1 + 0.2 is not 0.3

Type this into almost any programming language:

python
>>> 0.1 + 0.2 == 0.3
False
>>> 0.1 + 0.2
0.30000000000000004

It is not a bug, and it is not specific to Python. JavaScript, Java, C, Go and Rust all give the same answer, because they all store these numbers the same way. The cause is simple once you see it: 0.1 cannot be written down exactly in binary.

Some fractions never end

You already know this problem in decimal. Write one third as a decimal:

0.3333333...

It never ends. Any amount of paper leaves you slightly short. The reason is that 3 does not divide evenly into a power of 10.

Computers store numbers in binary, base 2. In binary, a fraction only comes out exactly if it can be built from halves, quarters, eighths and so on. One half is 0.1. Three quarters is 0.11. But one tenth is not a sum of those, however many you use.

Writing 0.1 in binaryInteractive
0.
...

Start. To write a fraction in binary: double it. If the result reaches 1, write a 1 and keep what is left over. Otherwise write a 0. Repeat with the leftover.

Left over
0.1
Bits written
0

The pattern 0011 repeats for ever, just as the 3s do in one third.

So the computer rounds

A standard floating-point number (a "double") has 64 bits, of which 53 hold the significant digits. The endless binary fraction is cut off there and rounded to the nearest value that fits.

So when you write 0.1, what is stored is not one tenth. It is the closest number the format can represent, which is very slightly more:

0.1000000000000000055511151231257827...

The error is tiny, about one part in a hundred million billion. Usually it is invisible. It shows up when two rounded numbers are combined and the result is compared with a third rounded number.

0.1 + 0.2 == 0.3Interactive
1a = 0.1
2b = 0.2
3total = a + b
4print(total == 0.3)
5print(total)

What is really stored

nothing yet

Start. Press play, or step through one line at a time.

0.1 was stored a hair too high. 0.2 was stored a hair too high. Their sum landed a hair above 0.3. And the 0.3 you typed was stored a hair below 0.3. Two different numbers, so == says false.

Which numbers are exact?

NumberExact in binary?Why
0.5, 0.25, 0.125YesHalves, quarters, eighths
0.75YesA half plus a quarter
0.1, 0.2, 0.3, 0.7NoTenths are not sums of halves
Whole numbers up to about 9 quadrillionYesThey fit in 53 bits
Larger whole numbersNot allBeyond 2⁵³, not every integer is available

So 0.5 + 0.25 == 0.75 is true, and 0.1 + 0.2 == 0.3 is false.

Living with it

  1. Never compare floats with ==

    Ask whether they are close enough.

    python
    import math
    math.isclose(0.1 + 0.2, 0.3)     # True
  2. Never store money as a float

    Errors of a fraction of a cent add up over millions of transactions. Store whole numbers of the smallest unit (cents, paise), or use a decimal type.

    python
    from decimal import Decimal
    Decimal("0.1") + Decimal("0.2") == Decimal("0.3")     # True
  3. Round when showing, not when storing

    Keep full precision inside the program and format to two decimals at the last moment.

  4. Be careful adding many small numbers

    Each addition can add a little error. Adding 0.1 ten times does not give exactly 1.0.

Key takeaways

  • Computers store fractions in binary, and most decimal fractions have no exact binary form.
  • 0.1 in binary repeats for ever, like one third in decimal, so it is rounded.
  • 0.1 + 0.2 lands slightly above 0.3, while 0.3 itself is stored slightly below.
  • Compare floats with a tolerance, never with ==.
  • Use integers or a decimal type for money.

Your progress

Check your progress

0%

  1. Read the page50% of this page

    When you have gone through the page and its animations, tick it off.

  2. Take the quiz50% of this page

    7 questions, from easy to hard. Take it as often as you like: the questions change, and only your best score counts.

Saved on this device only. See all your progress