[Python-ideas] PEP 485: A Function for testing approximate equality

Alexander Belopolsky alexander.belopolsky at gmail.com
Tue Jan 27 18:57:03 CET 2015


On Tue, Jan 27, 2015 at 11:20 AM, Guido van Rossum <guido at python.org> wrote:

> It feels like absolute tolerance is a completely different test. And it is
> a much simpler test for which w don't need a helper function -- it's just
> abs(x) < tolerance.


+1

Just as a point of reference, APL and its derivatives use tolerant
comparison in the default equal (=) operator.  The definition that they use
for finite x and y is simply

  x = y <=> abs(x-y) <= tol * max(abs(x), abs(y))

The tolerance can be adjusted globally or in some languages (such as J [1])
in the expression using additional syntax.

In J, the default tolerance is 2**-44, which is about 5.7e-14.  APL
restricts the range of tolerance values to 0 through 2**-32.

I would be +0 on adding something like

def tolerant_equal(x, y, tol=2**-44):
    return abs(x-y) <= tol * max(abs(x), abs(y))

(name subject to bikeshedding)

to math, but -1 on anything more complicated.  I would rather see

if tolerant_equal(x, y) or abs(x-y) <= 1e-10: ..

than

if tolerant_equal(x, y, atol=1e-10): ..


[1] http://www.jsoftware.com/help/dictionary/d000.htm
[2]
http://www.dyalog.com/uploads/documents/Papers/tolerant_comparison/tolerant_comparison.htm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150127/d403a04c/attachment.html>


More information about the Python-ideas mailing list