[Tutor] Random equation generator

Paul McGuire ptmcg at austin.rr.com
Sat Dec 6 10:20:51 CET 2008


I would say, though, that you should be careful in your implementation of
is_on_line, for floating point round-off errors.  Try this at the command
prompt (Python 2.5.2, with __future__ division imported):

>>> 49 * (1/49) == 1
False
>>> 1 - 49 * (1/49)
1.1102230246251565e-016

I would suggest a slightly modified version of is_on_line:

class Line(object):
     EPSILON = 1e-15
     ...

     def is_on_line(self,(x,y)):
         'Test if point represtented by an (x,y) tuple is on the line'
         return abs(self. m * x + self.c - y) < Line.EPSILON

-- Paul




More information about the Tutor mailing list