[TIP] doctest and floats

Raphael Marvie raphael.marvie at lifl.fr
Mon Mar 10 02:10:03 PDT 2008


Hi,

I use comparisons to define expectations

def foo(n):
     """
     >>> 0.1 == f()
     True
     """
     return n

I don't know if this is the one way, but I used it for scientific 
program testing.

HTH

r.


Tim Head wrote:
> Hi,
> 
> if you want to test a function that returns a float, what is the "one
> way" to write that down in a doctest?
> 
> consider:
> 
> def foo(n):
>    """
>    >>> foo(0.4)
>    0.4
>    >>> foo(0.1)
>    0.1
>    """
>    return n
> 
> this will fail on the second one because 0.1 is one of these numbers
> that can be represented completely in binary.
> 
> It feels wrong to write
>>>> foo(0.1)
> 0.10000000000000001
> 
> however
> 
>>>> foo(0.1)
> 1./10.
> 
> does not work either.
> 
> Does anyone of you clever people have some experience with how to do
> this properly?
> 
> cheers,
> tim
> 

-- 
Raphael Marvie, PhD                http://www.lifl.fr/~marvie/
Maître de Conférences / Associate Professor  @  LIFL  --  USTL
Directeur du Master Informatique Professionnel spécialité IAGL
Head of Master's in Software Engineering     +33 3 28 77 85 83



More information about the testing-in-python mailing list