Numeric comparison anomaly
Steven Taschuk
staschuk at telusplanet.net
Fri Feb 21 01:00:08 EST 2003
Quoth Jim Meyer:
[...]
> Here's a slightly different snippet to break:
>
> class INFINITY:
> def __init__(self, type = 1) :
> self.sign = cmp(type, 0)
> if not self.sign: self.sign = 1
> def __cmp__(self, other) :
> if isinstance(other, INFINITY) :
> return cmp(self.sign, other.sign)
> else :
> return self.sign
[...]
> --j, who wonders if 'self.sign = 0' might ever be interesting ...
If I've understood your code correctly, with self.sign == 0 we'd
have
-inf < 0inf < +inf
and 0inf would compare as equal to all other objects (at least
when it was on the left-hand side of the comparison).
In a sense, then, 0inf means "an arbitrary finite number". Using
"INFINITY(0) == x" to test "x is finite" is a little odd, but the
test itself is useful. (It looks (very slightly) more sensible as
"INFINITY(False) == x".)
--
Steven Taschuk staschuk at telusplanet.net
"What I find most baffling about that song is that it was not a hit."
-- Tony Dylan Davis (CKUA)
More information about the Python-list
mailing list