Search code examples
pythonunit-testingfloating-pointdoctest

How to test floats results with doctest?


I'm developing a program that makes some floating points calculations. Is there any way to test my functions (which deliver floats) with doctests?


Solution

  • 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'
    
    '''