[Kirby Urner]
... Question:
Why does float -> long work like this:
>>> long(1.0826786300000142e+045) 1082678630000014218353234260713996413124476928L
and not like this?
>>> long(1.0826786300000142e+045) 1082678630000014200000000000000000000000000000L
Kirby
Because your machine floating-point isn't decimal, it's binary:
x = 2.**100 print x 1.26765060023e+030 print long(x) 1267650600228229401496703205376 print 2L ** 100 1267650600228229401496703205376
So making long(2.**100) 1267650600230000000000000000000L instead would be plain wrong. The same thing happens in your example, but is harder to picture because, offhand, I bet you don't know the exact IEEE-754 bit pattern corresponding to 1.0826786300000142e+045 <wink>.
x = 1.0826786300000142e+045 import math mantissa, exponent = math.frexp(x) print mantissa 0.758577950788 print exponent 150 print math.ldexp(mantissa, 53) 6.832662753e+015 mantissa_as_long = long(math.ldexp(mantissa, 53)) mantissa_as_long 6832662753002049L print mantissa_as_long << (exponent-53) 1082678630000014218353234260713996413124476928 print long(x) 1082678630000014218353234260713996413124476928
IOW, long(some_huge_integer_in_float_format) *does* have a large number of trailing zeroes, but in base 2, not necessarily in base 10.
print hex(long(x)) 0x308C8A88868482000000000000000000000000L