HELP: restore my faith in Python

John W. Baxter jwbnews at scandaroon.com
Sat Mar 4 02:24:45 CET 2000


In article <mtsny7ua0n.fsf at astron.berkeley.edu>, 
johann at physics.berkeley.edu wrote:

> In short, you can't trust floats to give precise answers, if the last
> few decimal places matter.  If you're truncating to int, rather than
> rounding, then those last few places do matter.  This is true in any
> language.  Just don't do it.  ;-)

Although this particular example would appear to work correctly in a 
language which implements decimal (BCD or otherwise) floating point.

There are lots of surprises:  0.1 isn't precise in binary floating 
point, either (.0 and .5 are).  And in .00 .. .99, only .25, .5, .75 and 
.00 are precise.

This is why, without LOTS of care, binary floating point is unsuitable 
for accounting for decimal currencies...of which there are some in the 
world.  In US $ for example, an available alternative is to use 
long-enough integers (for the problem at hand) to account for cents or 
mils rather than dollars, and deal with the dollars in input and output 
routines.

  --John (pointing out that we would be better off here if our Noble 
Ancesters had realized that their Thumbs were Sacred, and not to be used 
for mundane things like counting)

-- 
John W. Baxter   Port Ludlow, WA USA  jwbnews at scandaroon.com



More information about the Python-list mailing list