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

Grant Edwards ge at nowhere.none
Mon Sep 25 16:31:14 EDT 2000


In article <0uyy5.9506$7g.258835 at news3.mia>, Brendhan Horne wrote:

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

If you use floating point, your answer isn't going to be exact.

For some things, the input values aren't exact, so that's OK
(but you still have to pay attention to what you're doing if
you want to limit the noise introducted in the calculations).

If your input values are exact, and you want your answer to be
exact, then don't use floating point.

Do everything with integer math in whatever units will give you
the precision you need (hundredths of a penny, for example).

Way back when, many language implimentations came with BCD
floating-point math packages that could be used for financial
stuff and yield exact answers -- I haven't seen mention of BCD
math for quite a while, but I don't do financial software...

-- 
Grant Edwards                   grante             Yow!  This ASEXUAL
                                  at               PIG really BOILS
                               visi.com            my BLOOD... He's
                                                   so... so... URGENT!!



More information about the Python-list mailing list