[Python-ideas] float comparison in doctes

Steven D'Aprano steve at pearwood.info
Mon Sep 15 22:31:44 CEST 2014


On Mon, Sep 15, 2014 at 12:02:20PM -0700, Kevin Davies wrote:

> It seems that this didn't reach the list directly (see 
> https://mail.python.org/pipermail/python-ideas/2014-August/028956.html), 
> so I'm resending:

> 
> Erik Bray (the author of the +FLOAT_CMP extension in Astropy), Bruce 
> Leban, and I had a short off-thread email discussion. Here are the 
> points:
> 
> - [Bruce]: ALMOST_EQUAL is the best flag name.
> - [Erik]: If there's agreement on this, Erik will develop a patch as soon as he can.
> - [Erik]: There's no way to adjust the tolerance because there seems 
> to be no easy way to parameterize doctest flags. Ideas are welcome.

With no way to choose between (at minimum) *four* different "almost 
equal" models, and no way to specify a tolerance, I don't think doctest 
ought to have such a directive.

Almost equal can mean:

- round and compare with == (as unittest does)
- absolute difference
- relative difference
- ULP difference


Given what a blunt instrument doctest is, I think the nicest solution is 
also the most explicit: just use ellipses.

> - [Erik]: Still, "This +FLOAT_CMP flag enabled removing tons of 
> ellipses from the test outputs [of Astropy], 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.
> - [Erik]: This PR is a better link than the one I gave: 
> https://github.com/astropy/astropy/pull/2087
> - [Erik]: Most of the code is from the SymPy project with 
> improvements. Erik had started on a similar feature when he found that 
> their implementation was further developed.




More information about the Python-ideas mailing list