floating point in 2.0

Chris Barker chrishbarker at home.net
Thu Jun 7 15:52:37 EDT 2001


Boyd Roberts wrote:
> it's simple:
> 
>     floating point represented in finite binary is not precise
>     for all values.
> 
> learn to live with it.  it's one of the first things they teach you
> in comp sci.  unless you really know what you're doing it is not a
> subject that you re-invent as a weekend project, not unless you're
> a fan of dire, unforeseen consequences.

Unfortunately, if you know no more than the fact that some amount of
error can be introduced, you are doomed to your "dire, unforeseen
consequences". Either than or not using floating point at all. Some
operations produce no or little error, and some produce a great deal of
error. If you have no idea which is which, than you are stuck. Many of
us can get away with ignoring floating point error (especially when
using doubles), but the more you know , the better off you are, and it
is difficult stuff, but not unknowable.

By the way, one of my collegues introduced a bug with floating ppoint
round off in a way that I di not forsee, and it took us a long time to
find it. We need to store a LOT of values in a file, and so as not to
waste space, we used the array module, and stored them as single
precision floats. Whe we used them after reading the file, they got
typcast to s Python Float (C double). As it happens, these values were
angles, with none greater than 2*pi. later, when we checked the values,
we found that when you round a double representation of pi to a float,
and then back to a double, you get a bigger number, which we didn't
expect. Here is an example:

>>> from array import array
>>> from math import pi
>>> pi2 = array('f',[pi])
>>> a = array('f',[pi])
>>> pi2 = a[0]
>>> pi2 > pi1
1

This took a while to find!

-Chris





-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list