Back

Why can’t Python count properly?

The truth behind 0.1 + 0.2 in Python

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!

 

Related posts

Beyond the IDE: programming lessons in the age of AI

One of the most persistent challenges in a programming classroom is knowing what every student is doing at any given […]

23 October 2026

If a NOT gate turns zero into one, where does that one come from?

How does a NOT gate turn a 0 into a 1 without creating something from nothing? Discover how voltage, transistors and digital logic work together to power the technology inside every modern computer.

15 October 2026

Ofsted after the Big Listen: have the rules changed, or just the packaging?

Ofsted has changed its inspection framework, but many school leaders question whether it has changed the culture surrounding inspection. We explore whether report cards can rebuild trust, reduce pressure and create an accountability system that genuinely helps schools improve.

9 October 2026

Should I make my own resources?

Should teachers really be making all their own resources? We explore the balance between creating, buying and adapting resources — and why thoughtful planning matters more than who made them.

5 October 2026

How does age verification technology work?

Can technology really tell how old you are? Explore the computer science behind age verification and the privacy and accuracy questions it raises.

28 September 2026

UTF-8 is witchcraft, right?

Ever wondered how your computer can happily store a simple English letter, an emoji, a pound sign and characters from […]

22 September 2026

Why teaching models keep returning

Teaching frameworks can provide valuable guidance, but when they become rigid formulas, they can undermine professional judgement. The goal isn’t identical lessons – it’s consistently effective teaching.

18 September 2026

Do GCSEs doom students to fail?

GCSE results may look stubbornly flat, but the reality behind the numbers tells a very different story. Why has further improvement become so difficult, and are we measuring success in the right way?

11 September 2026

Will Quantum Computers Break Encryption?

Could quantum computers really break encryption and put our data at risk? We explore the reality behind the hype, from qubits and Shor’s Algorithm to the future of post-quantum security.

7 September 2026