[Python-Dev] Comparing heterogeneous types

Tim Peters tim.one at comcast.net
Wed Jun 2 20:36:47 EDT 2004


[Jeff Epler]
> Oh -- there are some floats f for which
>     float(long(f))
> raises an OverflowError in float()?

No, at least not unless the machine has very strange arithmetic.  For an
IEEE box,

>>> from math import ldexp
>>> b = 2**53 - 1       # string of 53 1-bits
>>> ldexp(b, 1024-53)   # largest finite IEEE double
1.7976931348623157e+308
>>> long(_)             # as a long
179769313486231570814527423731704356798070567525844996598917476803157
260780028538760589558632766878171540458953514382464234321326889464182
768467546703537516986049910576551282076245490090389328944075868508455
133942304583236903222948165808559332123348274797826204144723168738177
180919299881250404026184124858368L
>>> float(_)            # and back to float
1.7976931348623157e+308
>>>

Similarly for its negation, and those are the worst cases.





More information about the Python-Dev mailing list