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

Chris Barker chris.barker at noaa.gov
Thu Feb 5 20:39:43 CET 2015


I've updated the copy on gitHub with these notes.


> - Which module should this go into? (I think math, but I don't think you
> state it.)
>

added to text.


> - Sometimes the default relative tolerance is described as 1e-8, sometimes
> as 1e-9.
>

that's what I get for changing it -- fixed.

- The formula with "abs(a-b) <= max( ... min(..." is missing an
> all-important close parenthesis. I presume it goes before the last comma.
> :-)
>

fixed.


> - What if a tolerance is inf or nan? What if all args are inf or nan?
>

0.0 < rel_tol < 1.0)

I've enforced this in the sample code -- it's not strictly necessary, but
what does a negative relative tolerance mean, anyway, and a tolerance over
1.0 has some odd behaviour with zero and could hardly be called "close"
anyway. Text added to make this clear.

if a and/or b are inf or nan, it follows IEE754 (which mostly "just works"
with no special casing in the code.


> - I'd argue for allowing the tolerances to be floats even if a and b are
> Decimals. There's not much difference in the outcome. Also, what if exactly
> one of a and b is Decimal?
>

This resulted from tests failing with trying to convert a float to a
Decimal in the code. Though I can't find that problem right now. But maybe
type dispatch is a better way to handle that.


> - Your language about complex confuses me. What is z?
>

"standard" notation for an arbitrary complex value -- I've clarified the
language.


> I'd expect the following behavior:
>   - tolerances cannot be complex
>   - abs(a-b) is the right thing to use
>   Or maybe you meant to say that abs() of the tolerances will be used if
> they are complex? That makes some sense in case someone just mindlessly
> passes a real cast to complex (i.e. with a zero imaginary part, or
> near-zero in case it's the outcome of a very fuzzy computation). But still.
> Maybe you can just give the formula to be used for complex args, if it is
> in fact any different from the one for reals?
>

it's the same, with the exception of using isinf from cmath. At this point,
my example code multiplies the tolerance times the tested values, then
takes the absolute values -- so a complex tolerance will do complex
multiplication, then use the magnitude from that -- I'll have to think
about whether that is reasonable.

- For unittests are you proposing to replace assertAlmostEquals or
> deprecate that and add a new assertIsCloseTo? (I hope the latter.
>

Add an assertIsCloseTo -- whether assertAlmostEquals should be deprecated I
have no opinion on. Though it is useful, as it acts over a sequence -- I"m
guessing fols will want to keep it. Then we'll need them to reference each
other in the docs.

 On Thu, Feb 5, 2015 at 10:46 AM, Nikolaus Rath <Nikolaus at rath.org> wrote:

> > 2) ``(abs(a-b) <= tol*abs(a)) or (abs(a-b) <= tol*abs(a))``
>
> I think the last 'a' should be a 'b'?


indeed -- fixed.

Thanks,

-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/20150205/201c877d/attachment-0001.html>


More information about the Python-ideas mailing list