Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python question about exponents and int

Out of curiosity I ran the following:

>>> int(1e100)

And, the output was:

10000000000000000159028911097599180468360808563945281389781327557747838772170381060813469985856815104L

Why? Why does this not look like:

10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000L

Is this a product of the int function, or of the storage of a large long?

like image 802
orokusaki Avatar asked Aug 10 '11 20:08

orokusaki


2 Answers

1e100 is a floating point number, with 53 bits of precision in the significand. The integer you're trying to represent requires 333 bits for exactness, so there is some rounding involved. The bottom bits are substituted to make the closest base 2 number to your desired result.

See: http://docs.python.org/tutorial/floatingpoint.html#representation-error

like image 54
Mark Ransom Avatar answered Oct 21 '22 18:10

Mark Ransom


It got converted to a float and then displayed as an integer. So welcome to the imprecise world of rounding

like image 43
Pat Avatar answered Oct 21 '22 19:10

Pat