[Python-Dev] math.areclose ...?
Scott David Daniels
Scott.Daniels at Acm.Org
Wed Feb 8 17:11:55 CET 2006
> ... There is a problem with dividing by 'ave' if the x and y are at
> the floating point limits, but the symmetric behaving form (presented
> by Scott Daniels) will have the same problem.
Upon reflection, 'max' is probably better than averaging, and avoiding
divide is also a reasonably good idea. Note that relative_tol < 1.0
(typically) so underflow, rather than overflow, is the issue:
def nearby(x, y, relative_tol=1.e-5, absolute_tol=1.e-8):
difference = abs(x - y)
return (difference <= absolute_tol or
difference <= max(abs(x), abs(y)) * relative_tol)
I use <=, since "zero-tolerance" should pass equal values.
--Scott David Daniels
scott.Daniels at Acm.Org
More information about the Python-Dev