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

Paul Moore p.f.moore at gmail.com
Fri Jan 23 17:05:05 CET 2015


On 23 January 2015 at 00:40, Chris Barker <chris.barker at noaa.gov> wrote:
> Expected Uses
> =============
>
> The primary expected use case is various forms of testing -- "are the
> results computed near what I expect as a result?" This sort of test
> may or may not be part of a formal unit testing suite.
>
> The function might be used also to determine if a measured value is
> within an expected value.

This section is very weak. As someone who doesn't do numerically
intensive computing I would start with the assumption that people who
do would have the appropriate tools in packages like numpy, and they
would have the knowledge and understanding to use them properly. So my
expectation is that this function is intended specifically for
non-specialists like me.

Based on that, I can't imagine when I'd use this function. You mention
testing, but unittest has a function to do this already. Sure, it's
tied tightly to unittest, so it's not useful for something like
py.test, but that's because unittest is the stdlib testing framework.
If you wanted to make that check more widely available, why not simply
make it into a full-fledged function rather than an assertion? And if
it's not suitable for that purpose, why does this PEP not propose
updating the unittest assertion to use the new function? It can't be
right to have 2 *different* "nearly equal" functions in the stdlib.

Outside of testing, there seems to be no obvious use for the new
function. You mention measured values, but what does that mean?
"Measure in the length of the line and type in the result, and I'll
confirm if it matches the value calculated"? That seems a bit silly.

I'd like to see a couple of substantial, properly explained examples
that aren't testing and aren't specialist. My worry is that what this
function will *actually* be used for is to allow naive users to gloss
over their lack of understanding of floating point:

    n = 0.0
    while not is_close_to(n, 1.0): # Because I don't understand floating point
        do_something_with(n)
        n += 0.1

BTW, when writing that I had to keep scrolling up to see which order
actual and expected went in. I'd imagine plenty of naive users will
assume "it's symmetrical so it shouldn't matter" and get the order
wrong.

In summary - it looks too much like an attractive nuisance to me, and
I don't see enough value in it to counteract that.

Paul


More information about the Python-ideas mailing list