Builtin Float Epsilon? (was: Re: Does python suck or I am just stupid? )

Erik Max Francis max at alcyone.com
Sat Feb 22 18:19:39 EST 2003


Stephen Horne wrote:

> One problem is - just how close do two values need to be to count as
> equal. There is no single good answer to this - it depends on the
> context. If you are dealing with numbers that range from zero to
> several billion, you might consider 0 and 0.1 to be equal - if your
> numbers range from 0 to 0.00000001 you will probably take quite a
> different view.

Indeed.  A "float epsilon" would have to be used as a factor, not as a
literal value to be added or substracted.  The C FAQ (when of course C
has precisely the same floating point "problems" that Python does) goes
into some detail about this.  It's important that the approximation test
not be

	if abs(x - y) <= epsilon: ...

but rather

	if abs(x - y) <= x*epsilon: ...

or similarly dependent on either (or both) of the factors involved.

It is still surprising to me how frequently this issue is attributed to
a problem in Python, despite being common to essentially all modern
programming languages (those that implement floating point, anyway).

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ The quality, not the longevity, of one's life is what is important.
\__/ Dr. Martin Luther King, Jr.
    Bosskey.net: Return to Wolfenstein / http://www.bosskey.net/rtcw/
 A personal guide to Return to Castle Wolfenstein.




More information about the Python-list mailing list