28 October 2026
Floating point “errors” explained in python
If you’ve ever typed 0.1 + 0.2 into Python and expected a neat 0.3, only to be greeted with 0.30000000000000004, you’re not alone. It looks like Python has forgotten how numbers work – but in reality, it’s doing exactly what it’s designed to do.
This is one of the most common surprises in programming, and it all comes down to how computers represent numbers.
Binary vs decimal: Why the confusion happens
We use base-10 (decimal) every day, but computers use base-2 (binary). Because of this, many simple decimal numbers – like 0.1 – can’t be represented exactly in binary.
Instead, computers store these values using the IEEE 754 Floating-Point Standard, which breaks numbers into a sign, mantissa, and exponent. This allows for incredibly efficient calculations across science, graphics, and simulations, but it comes with a small trade-off: rounding.
Just like 1/3 becomes 0.3333… in decimal, 0.1 becomes an endlessly repeating binary fraction. Computers can’t store infinity, so they round to the closest possible approximation.
So why does 0.1 + 0.2 look wrong?
When you add 0.1 and 0.2 in Python, you’re not adding the exact values you think you are. You’re adding two tiny binary approximations. The result is extremely close to 0.3 – just not perfectly identical.
Python isn’t being wrong here; it’s being honest. It shows you the true stored value rather than hiding the small rounding differences.
Is this just a Python problem?
Not at all. This behaviour is standard across almost all programming languages that use floating-point arithmetic, including Java, JavaScript, and C++. It’s simply how modern computing works.
When precision really matters – such as in finance – developers use decimal libraries or arbitrary-precision arithmetic to avoid these tiny inaccuracies.
Computers are honest, sometimes too honest
The key takeaway? Python can absolutely count. The real lesson is that computers are incredibly powerful, but they don’t always think in the same neat decimals we do.
Sometimes they just insist on showing their working — even when we’d rather they didn’t.
Want to know more? Kat, our Craig’n’Dave Lesson Hacker, explains all.
For more Lesson Hacker videos, check out the Craig’n’Dave YouTube playlist HERE.
Visit our website to explore more cutting-edge tech news in the computer science world!
