Really basic problem

A.T.Hofkamp hat at se-162.se.wtb.tue.nl
Mon Oct 8 12:23:27 CEST 2007

```On 2007-10-08, Andreas Tawn <andreas.tawn at ubisoft.com> wrote:
>> i know this example is stupid and useless, but that's not the answer
>> to my question.
>> here it goes:
>>
> You've just discovered the joys of floating point number comparisons.
>
> Consider this snippet:
>
> status = 0.0
> print (repr(status))
>
> for i in range(10):
>     status += 0.1
>     print (repr(status))
>
> Output:
>
> 0.0
> 0.10000000000000001
> 0.20000000000000001
> 0.30000000000000004
> 0.40000000000000002
> 0.5
> 0.59999999999999998
> 0.69999999999999996
> 0.79999999999999993
> 0.89999999999999991
> 0.99999999999999989
>
> The issue is that 0.1 etc don't have an exact representation as floating
> point.
>
> Interestingly:
>
>>>> 0.10000000000000001 == 0.1
> True
>>>> 0.30000000000000004 == 0.3
> False
>
> I guess this means that Python has some concept of "close enough", but
> I'll have to defer to someone more knowledgeable to explain that.

It's not Python, it is the computer, ie you have this problem with any program
that uses floating point calculations. However, some programs hide it from you
so you think there is no problem...

Plz read the FAQ for an answer to this and many other questions:
http://www.python.org/doc/faq/general/#why-are-floating-point-calculations-so-inaccurate

Sincerely,
Albert

```