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

Nathaniel Smith njs at pobox.com
Tue Jan 27 19:05:02 CET 2015

On Tue, Jan 27, 2015 at 4:20 PM, Guido van Rossum <guido at python.org> wrote:
> When does a program need *both* absolute and relative tolerance in a single
> test?

It's reasonably common in numpy, where the equivalent function is
vectorized to do multiple checks at once, e.g. here are some tests
that mix zeros and non-zeros in a single call:


The equivalent would be writing
   assert all([math.is_close_to(a, e) for (a, e) in zip(actual, expected)])
where 'expected' has a mix of zeros and non-zeros.

>> I think that's it for the technical decisions.
>> I would also appreciate suggestions for paramater names --at least if we
>> go with the asymetric test: "actual" and "expected" is a bit confusing -- I
>> like "expected", but we need something better than "actual" for the
>> don't-know-it's-right one.
>> I also would really appreciate someone working out the details and
>> contributing text for the inclusion of this in unittest.TestCase -- unittest
>> is really not my thing.
>> I've pushed some changes to gitHub (sorry, forgot to push yesterday), and
>> once the dust settles I'll incorporate as many of the suggestions in the PEP
>> text as I can.
> I still think this is better off as a recipe than as a helper function.

I don't find it super compelling either way -- at the end of the day,
if 'math' doesn't provide this then many people will use libraries
that will or else write their own. I guess I don't have a good sense
of what the audience for 'math' is these days -- I'm sure it has one,
but aside from tiny one-off usages I'm not sure what it is. None of
the production numerical code I see even bothers importing it. It's
possible that it largely serves a kind of pedagogical role? high
schoolers learning to program etc.? in which case there might be some
more benefit in having a well-documented discussion of the issues here
in the stdlib docs.

The most compelling argument I see is that if we care about unittest,
then it would be good to have a better alternative to
assertAlmostEqual. (...I don't know anyone who uses the unittest API
directly these days, though!)


Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh

More information about the Python-ideas mailing list