Carrot
Carrot

Reputation: 431

Decimal representation of a whole number is of lower size than the whole number itself?

In python, Why does the decimal representation of same number shows to occupy lesser space in memory(almost half)?

sys.getsizeof(1234561111111111111111111111111111111111111112223567744.0)
24
sys.getsizeof(1234561111111111111111111111111111111111111112223567744)
48

Upvotes: 0

Views: 41

Answers (1)

Thierry Lathuille
Thierry Lathuille

Reputation: 24262

This is not specific to Python 2.7, you would get the same with Python 3.

Your two numbers are not the same: the first one is a float, that will be stored as a float (and so rounded), and will use the same number of bytes whatever its value (in the allowed range for floats):

a = 1234561111111111111111111111111111111111111112223567744.0
>>> a
1.2345611111111112e+54

while the second one is an integer, which will be stored with unlimited precision (all digits are kept):

b = 1234561111111111111111111111111111111111111112223567744
>>> b
1234561111111111111111111111111111111111111112223567744

The size needed to store it will grow without limit with the number of digits.

Upvotes: 2

Related Questions