[Python-Dev] math.areclose ...? (original) (raw)

Scott David Daniels Scott.Daniels at Acm.Org
Sun Feb 5 19:53:30 CET 2006


Georg Brandl wrote:

Alex Martelli wrote:

So I was wondering if module math (and perhaps by symmetry module cmath, too) shouldn't grow a function 'areclose' ...maybe ... 'almostequal') def areclose(x, y, rtol=1.e-5, atol=1.e-8): return abs(x-y)<atol+rtol*abs(y) atol sounds suspicious to me, but otherwise fine.

"almost_equal", "closeto", or some variant of "near" (no nasty verb to worry about) would do for me. atol / rtol would be better as either abs_tol / rel_tol or even absolute_tolerance / relative_tolerance.

As to the equation itself, wouldn't a symmetric version be somewhat better?

 def nearby(x, y, rel_tol=1.e-5, abs_tol=1.e-8):
     return abs(x - y) < abs_tol + rel_tol * (abs(x) + abs(y))

This avoids areclose(0, 1e-8) != areclose(1e-8, 0), for example.

--Scott David Daniels Scott.Daniels at Acm.Org



More information about the Python-Dev mailing list