[Python-Dev] math.areclose ...? (original) (raw)
Raymond Hettinger raymond.hettinger at verizon.net
Mon Feb 6 22:37:22 CET 2006
- Previous message: [Python-Dev] math.areclose ...?
- Next message: [Python-Dev] math.areclose ...?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
[Chris Smith]
Does it help to spell it like this?
def areclose(x, y, relativeerr = 1.e-5, absoluteerr=1.e-8): diff = abs(x - y) ave = (abs(x) + abs(y))/2 return diff < absoluteerr or diff/ave < relativeerr
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
- Previous message: [Python-Dev] math.areclose ...?
- Next message: [Python-Dev] math.areclose ...?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]