[TIP] doctest and floats

Tim Head betatim at gmail.com
Mon Mar 10 01:58:23 PDT 2008


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

-- 
http://tim.jottit.com/



More information about the testing-in-python mailing list