On Thu, May 30, 2013 at 3:42 PM, Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info> wrote: > 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
I was picking an epsilon based on a, though, which comes to pretty much the same thing as the relative error calculation you're using. > 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. One technique I saw somewhere is to use the average of a and b. But probably better is to take the lower absolute value (ie the larger epsilon). However, there's still the question of what epsilon should be - what percentage of a or b you take to mean equal - and that one is best answered by looking at the original inputs. Take these guys, for instance. Doing the same thing I was, only with more accuracy. http://www.youtube.com/watch?v=ZNiRzZ66YN0 ChrisA -- http://mail.python.org/mailman/listinfo/python-list