1.6, tutorial, and floating point (was Re: varibles)

Simon Brunning SBrunning at trisystems.co.uk
Fri Sep 22 04:50:02 EDT 2000


> From:	Brendhan Horne [SMTP:brendhan at bellsouth.net]
> 
> >>> tax = 17.5 / 100
> >>> price = 3.50
> >>> price * tax
> 0.6125
> >>> price + _
> 4.1125
> >>> round(_, 2)
> 4.11
> 
> What I ended up with when I did that was off by a miniscule amount and I
> was
> just wondering if it was do to editting needs (rounding numbers is not
> unusal in thing)
> or if I had done something wrong.
> 
Floating point numbers are inexact. From Kernighan and Plauger, The Elements
of Programming Style:

"As a wise programmer once said, 'Floating point numbers are like sandpiles:
every time you move one, you lose a little sand and you pick up a little
dirt'. And after a few computations, things can get pretty dirty."

If you want to *see* the number rounded, try something like:

print '%.2f' % round(_, 2)

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

N.B. Sorry about the legal stuff - nothing I can do about it.




-----------------------------------------------------------------------
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