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

Chris or Leslie Smith smiles at worksmail.net
Mon Feb 6 21:12:03 CET 2006


|| 
|| def areclose(x,y,rtol=1.e-5,atol=1.e-8):
||      return abs(x-y)<atol+rtol*abs(y)
| 
| Looks interesting.  I don't quite understand what atol/rtol are,
| though. 

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
###

Also, separating the two terms with 'or' rather than '+' makes the two error terms mean more what they are named. The '+' mixes the two effects and even though the result is basically the same, it makes it difficult to explain when the test will be true.

/c


More information about the Python-Dev mailing list