[Python-Dev] Comparing heterogeneous types
Jeff Epler
jepler at unpythonic.net
Wed Jun 2 16:12:30 EDT 2004
Can't you do it like so:
def cmp_long_vs_float(l, f):
try:
lf = float(l)
except OverflowError:
if special_float_value(f): # NaN, Inf
return cmp(0.0, f)
return cmp(l, 0L)
else:
return cmp(lf, f)
The try/else control flow seems to be what Python does today.
If the OverflowError is triggered, then the magnitude of l is bigger
than any finite float so you might just as well compare it against 0L.
Infinite floats are handled by the "if special_float_value(f):" branch, or
just ignored if you prefer.. (but having 2L**2000 compare greater than
+INF would bring complaints)
If it turns out to be more efficient, the implementation could LBYL to
see whether l can convert to float without overflow.
Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-dev/attachments/20040602/b42ac11e/attachment.bin
More information about the Python-Dev
mailing list