[Python-ideas] PEP 485: A Function for testing approximate equality

Chris Barker chris.barker at noaa.gov
Sat Jan 24 20:59:46 CET 2015

  >> One option would be to add a zero_tol argument, which is an absolute

> >> tolerance that is only applied if expected == 0.

OK -- now I know what the problem is here -- I thought I"d explored it

If you have a tolerance that you use only when expected is zero (or when
either is...) then you have teh odd reslut that a samll number will be
"close" to zero, but NOT close to a smaller number. I implemented this on a
branch in github:


And you get the odd result:

In [9]: is_close_to(1e-9, 0.0)
Out[9]: True

fine -- the default zero_tol is 1e-8

In [10]: is_close_to(1e-9, 1e-12)
Out[10]: False

but huh??? 1e-9 is close to zero, but not close to 1e-12????

This is why I dropped the idea before.

I'm back to the key point -- relative error compared to zero is not defined
-- you need to set an absolute tolerance if you want to test against zero
-- and there is no default that makes sense for all (or even most) cases.

I'd much rather require people to have to think about what makes sense for
their use case than get trapped by a default that's totally inappropriate.



Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150124/6913f606/attachment.html>

More information about the Python-ideas mailing list