[Edu-sig] python versus __python__
Kirby Urner
urnerk at qwest.net
Mon Oct 24 01:32:38 CEST 2005
> 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
More information about the Edu-sig
mailing list