123.3 + 0.1 is 123.3999999999 ?
A Puzzled User
kendear_nospam at nospam.com
Fri May 16 01:40:14 EDT 2003
Isaac To wrote:
>
> Somehow people keep yelling that they can't stand the inaccuracy of floating
> points, without actually looking at how inaccurate (or actually, accurate)
> they are.
>
> Regards,
> Isaac.
I think people won't like it if 12.95 becomes 12.94999999999
and some comparison fails such as checking if an item is more than
or equal to $12.95 and the 12.94999999999 doesn't satisfy
the >= 12.95 check. But if fact, it does -- because essentially,
it is checking against itself. 12.949999999 >= 12.95 is true!
because 12.95 is stored as 12.949999999 anyways.
Here is a test:
>>> a = 12.95
>>> a
12.949999999999999
>>> if (a >= 12.95): print "more than my budget"
more than my budget
>>>
I suppose there is no situation that the inaccuracy
can cause such obvious bugs? I just wonder why the
interactive command line gives us 12.949999999 while
print 12.5 will give us 12.95.... why doesn't the
command line lie also?
More information about the Python-list
mailing list