[Python-Dev] math.areclose ...?

Raymond Hettinger raymond.hettinger at verizon.net
Mon Feb 6 22:37:22 CET 2006


[Chris Smith]
> Does it help to spell it like this?
>
> def areclose(x, y, relative_err = 1.e-5, absolute_err=1.e-8):
>     diff = abs(x - y)
>     ave = (abs(x) + abs(y))/2
>     return diff < absolute_err or diff/ave < relative_err

There is a certain beauty and clarity to this presentation; however, it is 
problematic numerically:

* the division by either absolute_err and relative_err can overflow or 
trigger a ZeroDivisionError

* the 'or' part of the expression can introduce an unnecessary discontinuity 
in the first derivative.

The original Numeric definition is likely to be better for people who know 
what they're doing; however, I still question whether it is an appropriate 
remedy for the beginner issue
 of why 1.1 + 1.1 + 1.1 doesn't equal 3.3.

Raymond 



More information about the Python-Dev mailing list