Python: “0.1 + 0.2 β‰  0.3”. Me: “?”

Rate this post

Python Representation Error: This article explains why floating-point arithmetic can lead you astray in Python. Floating-point arithmetic cannot represent some numbers precisely. While many people think it’s a bug, it’s actually not.

Have a look at the following code snippet:

print((0.1 + 0.2) == 0.3)
# False

In the code snippet, you have assumed that the number 0.1 actually represents the decimal value 0.1 or 1/10. (Let me know, if you didn’t… ;)) This is natural but a wrong assumption. The value 0.1 doesn’t exist on your computer. Instead, your computer stores every number as a binary consisting only of zeros and ones.

Use an online converter to convert the decimal value 0.1 to a binary value and you will realize that you get the following number: 0.0001100110011001100110011001100110011001100110011001101…

The floating-point representation of 0.1 in binary has an infinite number of digits. So your computer does the only thing he can do at this moment: limiting the number of digits.

This has the following effect. The decimal number of 0.1 is represented by the closest floatingpoint number 0.1000000000000000055511151231257827021181583404541015625 that can be represented in a limited space.

Now, it’s easy to see why 0.1 + 0.2 != 0.3. Floatingpoint arithmetic is not powerful enough to represent ALL numbers correctly.

Note that this is a common problem for all major programming languages. It happens because of hardware design choices, not because of the programming language designers.


Gughan, one of the readers of my “Coffee Break Python” email series (subscribe!), pointed out the following question:

But why does it happen only with 0.1Β + 0.2? If you add any other decimal number like 0.3 or 0.4 you still get what you expect to:

>>> print(0.1 + 0.2)
>>> print(0.1 + 0.3)
>>> print(0.1 + 0.4)
>>> print(0.1 + 0.5)
>>> print(0.1 + 0.6)

The reason is the following: Python cannot represent some numbers precisely using only floating-point arithmetic (because of the reasons above). However, this does not mean that Python cannot represent all numbers precisely.

x = 0.1
for i in range(10):
    x += 0.1

OUTPUT (Python 3.7.2):

The code snippet shows that some numbers are represented correctly, others not. Note that value 0.4 is based on the wrongly represented value 0.3 by adding 0.1 to it. The reason is that the mathematical result of 0.3000000000004+0.1 is rounded to the float 0.4 which is the closest floating-point number to the result.

In summary, whether floats has rounding numbers or not depends on their inherent ability to be represented as binary numbers using floating-point arithmetic.

  • Some of them can be represented correctly by floats,
  • others can’t.

This is neither a bug, nor a big problem. It’s just life in the discretized world of computers.

Where to go from here?

If you want to dive deep into this topic, you can read more about floating point representation here.

Also check out my brand-new programming workbook: “Coffee Break Python Workbook”. It’s easy, fun to read, and full of 127 Python puzzles for highly effective training.

Learn more at the book page on my blog: “Coffee Break Python Workbook: 127 Python Puzzles to Push You From Zero to Hero in Your Coffee Breaks”

Leave a Comment