Search code examples
pythonmath

Why does 2**1024 work while 2**(2048/2) results in an OverflowError?


When I run the code

a = 2**(2028/2)*1023
print("a is",a)

b = 2**(2029/2)*1023
print("b is",b)

c = 2**(2028/2)*1024
print("c is",c)

d = 2**(1014)*1024
print("d is",d)

e = 2**(2048)
print("e is",e)

g = 2**(1024)
print("g is",g)

h = 2**(2048/2)
print("h is",h)

the output is:

a is 1.795937575160302e+308
b is inf
c is inf
d is 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
e is 32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596230656
g is 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
Traceback (most recent call last):
  File "newfile.py", line 19, in <module>
    h = 2**(2048/2)
OverflowError: (34, 'Result too large')

c should be the same as d, but isn't. Same for g and h. Why? Is there a workaround that lets you calculate 2**(2048/2) and 2**(2028/2)*1024 accurately? I don't think it's about 2048 being too large since e calculates without any errors.


Solution

  • The difference is that 2048/2 is a float, not an int. Because of that, the result would be evaluated as a float, and floats have a limited range.

    Thus:

    >>> 2**1024.0
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OverflowError: (34, 'Result too large')
    
    >>> 2**1024
    179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
    
    >>> float(2**1024)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OverflowError: int too large to convert to float
    

    However, if you only use ints:

    >>> 2**(2048//2)
    179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216