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 floating
Now, it’s easy to see why 0.1 + 0.2 != 0.3. Floating
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) 0.30000000000000004 >>> print(0.1 + 0.3) 0.4 >>> print(0.1 + 0.4) 0.5 >>> print(0.1 + 0.5) 0.6 >>> print(0.1 + 0.6) 0.7
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): print(x) x += 0.1 """ OUTPUT (Python 3.7.2): 0.1 0.2 0.30000000000000004 0.4 0.5 0.6 0.7 0.7999999999999999 0.8999999999999999 0.9999999999999999 """
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
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”
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.