123.3 + 0.1 is 123.3999999999 ?

Simon Brunning SBrunning at trisystems.co.uk
Fri May 16 05:07:46 EDT 2003


> From:	Isaac To [SMTP:kkto at csis.hku.hk]
> >>>>> "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?
 
In the context of monetary values, (which is what we are talking about, I
believe) then you wouldn't expect this to sum to 1.

£1 / 3 = £0.33. Multiply by 3, and you get £0.99. Accountants expect
rounding errors like this. So, in a *financial* context, fixed point does
the wrong thing, and FixedPoint works as expected.

In *other* contexts, of course, FixedPoint does the wrong thing and fixed
point is right.

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