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

Raymond Hettinger python at rcn.com
Sun Feb 5 19:48:51 CET 2006


So I was wondering if module math (and perhaps by symmetry module cmath, too) shouldn't grow a function 'areclose' (calling it just 'close' seems likely to engender confusion, since 'close' is more often used as a verb than as an adjective; maybe some other name would work better, e.g. 'almostequal') taking two float arguments and optional tolerances and using roughly the same specs as Numeric, e.g.:

def areclose(x,y,rtol=1.e-5,atol=1.e-8): return abs(x-y)<atol+rtol*abs(y)

IMO, the cure is worse than the disease. It is easier to learn about the hazards of floating point equality testing than to think through the implications of tolerance testing (such as loss of transitivity) and learning how to set the right tolerance values for a given application (ones that give the right results across the entire domain of expected inputs).

The areclose() function can be a dangerous crutch that temporarily glosses over the issue. Without some numerical sophistication, it would not be hard create programs that look correct and pass a few test but, in fact, contain nasty bugs (non-termination, incorrect acceptance/rejection, etc).

This proposal is one of several that have recently surfaced that aim to help newbies skip learning basic lessons. I think the efforts are noble but misguided.

A language suitable for beginners should be easy to learn, but it should not leave them permanently crippled. All of the above are sets of training wheels that don't come off. To misquote Einstein: The language should be as simple as possible, but no simpler.

Raymond



More information about the Python-Dev mailing list