float comparison in doctest
Erik Bray has created a nifty addition to doctest to compare floating point numbers using a `+FLOAT_CMP` flag. It is implemented within Astropy (http://www.astropy.org/) and the related issue is https://github.com/astropy/astropy/issues/2662. According to Erik, it may need some tweaks, but I think it would be a really useful feature to make available in the doctest package itself. Kevin
On 8/11/2014 12:25 PM, Kevin Davies wrote:
Erik Bray has created a nifty addition to doctest to compare floating point numbers using a `+FLOAT_CMP` flag.
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.
It is implemented within Astropy (http://www.astropy.org/) and the related issue is https://github.com/astropy/astropy/issues/2662.
The related issue is applying flags to a block of test with a Sphinx directive.
According to Erik, it may need some tweaks, but I think it would be a really useful feature to make available in the doctest package itself.
Erik would have to offer his code. -- Terry Jan Reedy
Terry Reedy wrote:
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.
what if a < b? or if a or b (or both) are NaN/+Inf/-Inf? Or... There are many pitfalls comparing floats, isn't better to delegate these checks in a function? -- By ZeD
On 12 Aug 2014 04:16, "Terry Reedy"
On 8/11/2014 12:25 PM, Kevin Davies wrote:
Erik Bray has created a nifty addition to doctest to compare floating point numbers using a `+FLOAT_CMP` flag.
The problem with a simple flag is that almost_equal should sometimes be
"a - b < delta" is shorter than "a.almost_equat(b, abs=delta)" and
absolute and sometimes relative and in both cases a parameter in needed. I think there have been vague proposals for a float method, but probably easier to understand. For more formal tests, we already have unittest.TestCase.assertAlmostEqual. This sounds like a reasonable flag for doctest's original purpose of checking that examples in docs are showing sensible answers. Cheers, Nick.
It is implemented within
Astropy (http://www.astropy.org/) and the related issue is https://github.com/astropy/astropy/issues/2662.
The related issue is applying flags to a block of test with a Sphinx
directive.
According to Erik, it may need some tweaks, but I think it would be a really useful feature to make available in the doctest package itself.
Erik would have to offer his code.
-- Terry Jan Reedy
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
participants (4)
-
Kevin Davies
-
Nick Coghlan
-
Terry Reedy
-
Vito De Tullio