portable doctests despite floating points numbers

Hello all, I use doctest for examples and tests in a program which relies heavily on numpy. As floating point calculations differs slightly across computers (32/64 bits), I have troubles writing portable doctests. The doctest documentation [1] advises to use numbers in the form int/2**n. This is quite restrictive. Using numpy.set_printoptions(suppress=True) also helps, but I still have problems with numbers around 0. On some platform a result prints as 0., on others as -0. Is there a workaround? [1] http://docs.python.org/library/doctest.html#warnings

Le vendredi 15 octobre 2010, Sébastien Barthélemy a écrit :
Hello all, Hi Seb
I use doctest for examples and tests in a program which relies heavily on numpy. As floating point calculations differs slightly across computers (32/64 bits), I have troubles writing portable doctests. The doctest documentation [1] advises to use numbers in the form int/2**n. This is quite restrictive.
Using numpy.set_printoptions(suppress=True) also helps, but I still have problems with numbers around 0. On some platform a result prints as 0., on others as -0.
Is there a workaround? Maybe you should not rely on doctest to do the comparison between computed and expected result. You could use assert_* functions from http://docs.scipy.org/doc/numpy/reference/routines.testing.html if your results are numpy objects. assert_almost_equal and assert_approx_equal are quite powerful!
Fab

I've haven't found any clean way. So I'll use "numpy.allclose" in my doctests. It will be harder to read, less interactive-session-like, but it might show the reader how compare floats properly. On Fri, Oct 15, 2010 at 2:05 PM, Fabrice Silva <silva@lma.cnrs-mrs.fr> wrote:
Maybe you should not rely on doctest to do the comparison between computed and expected result. You could use assert_* functions from http://docs.scipy.org/doc/numpy/reference/routines.testing.html
I didn't knew about them, they might prove useful too, thanks! Cheers

Another solution http://code.activestate.com/recipes/577124-approximately-equal/ -- Fabrice Silva
participants (2)
-
Fabrice Silva
-
Sébastien Barthélemy