On 30 May 2013 22:03, Carlos Nepomuceno <carlosnepomuc...@outlook.com> wrote:
>> 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
>
> That's wrong! If abs(a) < abs(a-b)/epsilon you will break the commutative law.

There is no commutative law for relative tolerance floating point
comparisons. If you want to compare with a relative tolerance then you
you should choose carefully what your tolerance is to be relative to
(and how big your relative tolerance should be).

In some applications it's obvious which of a or b you should use to
scale the tolerance but in others it is not or you should compare with
something more complex. For an example where it is obvious, when
testing numerical code I might write something like:

eps = 1e-7
true_answer = 123.4567879
estimate = myfunc(5)
assert abs(estimate - true_answer) < eps * abs(true_answer)


Oscar
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to