Short-circuit Logic

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu May 30 01:42:17 EDT 2013


On Thu, 30 May 2013 13:45:13 +1000, Chris Angelico wrote:

> Let's suppose someone is told to compare floating point numbers by
> seeing if the absolute value of the difference is less than some
> epsilon. 

Which is usually the wrong way to do it! Normally one would prefer 
*relative* error, not absolute:

# absolute error:
abs(a - b) < epsilon


# relative error:
abs(a - b)/a < epsilon


One problem with absolute error is that it can give an entirely spurious 
image of "fuzziness", when in reality it is actually performing the same 
exact equality as == only slower and more verbosely. If a and b are 
sufficiently large, the smallest possible difference between a and b may 
be greater than epsilon (for whichever epsilon you pick). When that 
happens, you might as well just use == and be done with it.

But using relative error also raises questions:

- what if a is negative?

- why relative to a instead of relative to b?

- what if a is zero?

The first, at least, is easy to solve: take the absolute value of a. But 
strangely, you rarely see programming books mention that, so I expect 
that there is a lot of code in the real world that assumes a is positive 
and does the wrong thing when it isn't.

Here's another way, mathematically equivalent (although not necessarily 
equivalent using floating point computations!) which avoids the divide-by-
zero problem:

abs(a - b) < epsilon*a


Whichever method you choose, there are gotchas to watch out for.

> http://xkcd.com/1047/

Nice!


-- 
Steven



More information about the Python-list mailing list