> Kirby
> 
> PS:  since you've been studying subclassing primitive types using __new__,
> maybe you could show us a user-defined floating point type that reports
> two numbers equal if their absolute value difference is less than e.  
> Anyone?
> 

Like this I guess:

 >>> class Fuzzy(float):
         tol = 1e-8
         
       def __new__(cls, arg=0.0):
                return float.__new__(cls, arg)

         def __eq__(self, other):
                if abs(self-other)<self.tol:
                        return True
                else:
                        return False

                
 >>> j = Fuzzy(10)
 >>> type(j)
 <class '__main__.Fuzzy'>

 >>> k = Fuzzy(10.000000001)

 >>> j == k
 True

 >>> k = Fuzzy(10.01)

 >>> j == k
 False

Kirby


_______________________________________________
Edu-sig mailing list
Edu-sig@python.org
http://mail.python.org/mailman/listinfo/edu-sig

Reply via email to