123.3 + 0.1 is 123.3999999999 ?

Isaac To kkto at csis.hku.hk
Fri May 16 05:00:30 EDT 2003


>>>>> "Simon" == Simon Brunning <SBrunning at trisystems.co.uk> writes:

    >> 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.
 
    Simon> Ah, but try this:

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

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

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

    Simon> This works as expected.

But what if you divide the number by 3, multiply it by 3 again, and compare
with 1?

Regards,
Isaac.





More information about the Python-list mailing list