numpy.allclose()

Ian Kelly ian.g.kelly at gmail.com
Sat Jan 17 13:42:35 EST 2015


On Sat, Jan 17, 2015 at 7:26 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> I don't understand why they add the error tolerances together. I can
> understand taking the minimum, or the maximum:

The usual idea is that the tolerance is calculated as a relative
value, but an absolute tolerance is used instead when the values get
close to zero, so under normal circumstances the relative tolerance
will dominate and the absolute tolerance will be very small in
comparison. So why add them instead of taking the maximum? I'm not
sure, but I would guess it's probably for efficiency. Determining the
maximum requires the CPU to perform a conditional branch that a simple
addition does not. In any case, this is not unique to numpy.  See
e.g.:

https://books.google.com/books?id=CbH0AwAAQBAJ&pg=PA164
(equation 8.26)

or:

https://books.google.com/books?id=j0rY3D9fx-0C&pg=PA71
(equation 7.437)

On the other hand, Matlab evidently uses the maximum:
http://www.mathworks.com/help/simbio/ref/absolutetolerance.html

> * taking the minimum is equivalent to the rule "numbers are close if they
>   differ by no more than BOTH the absolute tolerance AND the relative
>   tolerance".

This is generally not the desired semantic, and it's not unheard of to
pass 0 for one of the tolerances, in which case the minimum would
clearly be incorrect.



More information about the Python-list mailing list