Codementor Events

Why 0.1 + 0.7 does not equal 0.8?

Published Sep 30, 2022
Why 0.1 + 0.7 does not equal 0.8?

It always comes as a surprise to the coders that computer is not always accurate at maths. It's unexpected and unfathomable when realized for the first time!

Try it in any editor, console or language of your choice. Go on, open the google chrome developer tools console and try it there. Or if you want, goto python editor and try it there, or any other language of your choice. It will all come back to you as a surprise, that computers are not accurate at floating points calculations.

No, it's not just 0.1 and 0.7 that are the issue. There are numerous combinations that you will find that fail the computers miserably at calculations.
E.g.:

  • 0.1 * 0.9
  • 0.1 * 0.4
  • 0.4 + 0.2
    and so on!

Why does it matter?

One may ask, why does it matter? It's such a small number / difference, that it shouldn't make any difference in real life?
Wrong, it does make a huge difference. E.g. during banking transactions (both real money, or crypto currency), space ships and aircrafts, and many other applications! Billions of transactions having such a small error can end up breaking down the whole system.

Discover and read more posts from nabtron
get started