tags:

views:

140

answers:

1

Given the following code:

def slope(x1, y1, x2, y2):
    """
      >>> slope(5, 3, 4, 2)
      1.0
      >>> slope(1, 2, 3, 2)
      0.0
      >>> slope(1, 2, 3, 3)
      0.5
      >>> slope(2, 4, 1, 2)
      2.0
    """
    xa = float (x1)
    xb = float (x2)
    ya = float (y1)
    yb = float (y2)
    return (ya-yb)/(xa-xb)

if name_ == '__main__':
    import doctest
    doctest.testmod()

The second doctest fails:

Failed example:
    slope(1, 2, 3, 2)
Expected:
    0.0
Got:
    -0.0

However, we all know that -0.0 == 0.0. Is doctest doing a string comparison to check results here? Why does the second test fail?

+9  A: 

It fails because doctest does string comparison. It merely checks whether the output is identical to what would have been outputted if the code had been executed at the Python interactive interpreter:

>>> 0 / -2
-0.0

Edit:: The link referenced by Daniel Lew below gives some more hints about how this works, and how you may be able to influence this behaviour.

Stephan202
Reference link: http://docs.python.org/library/doctest.html#doctest.OutputChecker.check_output
Daniel Lew