Floating point problem

Chris Angelico rosuav at gmail.com
Sat Apr 18 10:06:03 EDT 2020


On Sun, Apr 19, 2020 at 12:03 AM Souvik Dutta <souvik.viksou at gmail.com> wrote:
>
> I literally tried it!!! And it did not stop because I did not get any 1.0
> rather I got 0.99999999999 But why does this happen. This is a simple math
> which according to normal human logic should give perfect numbers which are
> not endless. Then why does a computer behave so differently?
>

If you add 0.333 and 0.333 and 0.333, do you get 1.0? No, you get
0.999. But if you add 1/3 and 1/3 and 1/3, you get 1. The computer has
to round, same as you do - it doesn't have infinite precision. The
truth is that the number 0.1, to a computer, is not actually one tenth
- it's an approximation for one tenth, just as 0.333 is an
approximation for one third.

ChrisA


More information about the Python-list mailing list