123.3 + 0.1 is 123.3999999999 ?

Simon Brunning SBrunning at trisystems.co.uk
Fri May 16 04:13:26 EDT 2003


> From:	Isaac To [SMTP:kkto at csis.hku.hk]
> 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.
 
Ah, but try this:

>>> sum = 0.0
>>> for i in range(10): sum += 0.1
... 
>>> sum == 1
0

So, if you do any equality tests, even tiny inaccuracies can trip you up.
Using FixedPoint:

>>> import FixedPoint as fp
>>> sum = fp.FixedPoint(0.0)
>>> for i in range(10): sum += sum += 0.1
... 
>>> sum == 1
1

This works as expected.

Cheers,
Simon Brunning
TriSystems Ltd.
sbrunning at trisystems.co.uk




-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.





More information about the Python-list mailing list