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

Chris Barker chris.barker at noaa.gov
Tue Jan 27 22:31:25 CET 2015


On Tue, Jan 27, 2015 at 9:42 AM, Zero Piraeus <schesis at gmail.com> wrote:


> Disclaimer: I haven't read all of this thread, and what I have read I've
> sometimes skimmed (and apparently some of the discussion was off list
> anyway).


Nothing substantial was off-list -- thank god, there's been plenty on list!


> Once this exists, at some point somebody's going to write:
>
>     def near_miss(data, tol):
>         pairs = itertools.combinations(data.values(), 2)
>         return any(is_close(a, b, tol) for a, b in pairs)
>
> and then (if it's asymmetric) be very surprised when this:
>
>     example = {'A': 0.01, 'B': 2.34, 'C': 5.67, 'D': 5.68, 'E': 9.99}
>
>     near_miss(example, 1/568)
>
> returns True half the time and False the other half.


huh? itertools combinations returns a deterministic result, yes?

But that is indeed a case where the question really is "are these two
values close to each-other", with no implied order. IS that a lilkey real
world use-case? I have no idea.


> That's going to be
> a really nasty heisenbug when it crops up in real code, especially since
> it's only going to change for each invocation of the interpreter.
>

I still don't follow you here -- the asymmetric test is order dependent,
it's not random.


> I don't really buy the idea that it'll almost always be used with
> tolerances of 1e-8 etc. If it goes in the stdlib, it'll be used in ways
> no-one here anticipates (and which are not, intuitively, "wrong").
>

Indeed -- absolutely the case -- but all we can do is document the
behavior. On the face of it, a symmetric approach seems less surprising,
but really the surprize is simply different:

In the asymmetric case, the results may depend on the order of the
arguments.

In the symmetric case, the results may depend on whether the actual value
is less than or greater than the expected value.

I hark back to the point Steven made: what really matter is the difference
between the values. if someone is asking:

Is this value within 10% of the value 10 -- they are expecting that the
difference used will be 10% of ten, or 1.0 -- so any value between 9 and 11
is "close". But with a symmetric test -- the actual difference accepted
will be a function of whether the tested value is less than or greater than
the expected value -- that could be equally or more surprising than the
asymmetry.

I'm still looking for a case where a user would likely pass the same value
into the function in a different order -- wouldn't s/he pick an order
(maybe arbitratily) and use that?

-Chris



-- 

Christopher Barker, Ph.D.
Oceanographer

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/20150127/8d5cf2e0/attachment-0001.html>


More information about the Python-ideas mailing list