views:

39

answers:

2

Hi I'm developing a program that makes some floating points calculus. is there any way to test the my functions (which deliver floats) with doctest?

A: 

Sure, just format the floats with a reasonable format, based on your knowledge of what precision you expect them to exhibit -- e.g, if you expect accuracy to 2 digits after the decimal point, you could use:

''' Rest of your docstring and then...

    >>> '%.2f' % funcreturningfloat()
    '123.45'

'''
Alex Martelli
A: 

The documentation has a suggestion

Floating-point numbers are also subject to small output variations across platforms, because Python defers to the platform C library for float formatting, and C libraries vary widely in quality here.

>>> 1./7  # risky
0.14285714285714285
>>> print 1./7 # safer
0.142857142857
>>> print round(1./7, 6) # much safer
0.142857
gnibbler