[Python-ideas] float comparison in doctest

Erik Bray erik.m.bray at gmail.com
Wed Aug 13 17:30:05 CEST 2014


[Sorry for breaking the threading--turns out I didn't have a
python-ideas subscription from this address]

Just a few followup points I wanted to make to Kevin's post about a
proposed +FLOAT_CMP flag for doctest.  A better link for the
implementation is this PR in Astropy:

https://github.com/astropy/astropy/pull/2087

I can't take full credit either--most of the existing code was
borrowed (with some small improvements) from the SymPy project.  I had
started on a similar feature independently, but then borrowed their
implementation upon seeing that it was further along than mine.

On 12 Aug 2014 04:16, "Terry Reedy" <tjreedy at udel.edu> wrote:
> The problem with a simple flag is that almost_equal should sometimes be
> absolute and sometimes relative and in both cases a parameter in needed. I
> think there have been vague proposals for a float method, but "a - b < delta"
> is shorter than "a.almost_equat(b, abs=delta)" and probably easier to
> understand.

Indeed, that's what I had in mind when I told Kevin that there were
still issues with this.  There's no obvious way (I can think of at
least) to parameterize doctest flags.

However, the original immediate purpose of this feature was to handle
very small differences in representation of the same value between
different platforms, and in that respect it has worked very well.
Astropy has a lot of doctests, and many of which have floating point
outputs.  This +FLOAT_CMP flag enabled removing tons of ellipses from
the test outputs, and restoring the full outputs which certainly read
better in the docs.

For more complete unit tests of course we use assert_almost_equal type
functions.

That said, if anyone has any ideas for allowing tweaking the
tolerances for a doctest flag that would be great.  If this otherwise
seems like a good idea in general to include in the doctest module I
will offer a patch.

Erik


More information about the Python-ideas mailing list