为什么2 ** 1024工作而2 **(2048/2)导致OverflowError?

Loj*_*jcs 1 python math

当我运行代码

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)
Run Code Online (Sandbox Code Playgroud)

输出为:

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')
Run Code Online (Sandbox Code Playgroud)

c应该与d相同,但不相同。g和h相同。为什么?有一种解决方法,可以让您计算2**(2048/2)2**(2028/2)*1024准确?我认为2048不会太大,因为e的计算没有任何错误。

khe*_*ood 5

不同之处在于它2048/2是一个浮点数,而不是一个整数。因此,结果将被评估为浮点数,并且浮点数的范围有限。

从而:

>>> 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
Run Code Online (Sandbox Code Playgroud)

但是,如果仅使用整数:

>>> 2**(2048//2)
179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
Run Code Online (Sandbox Code Playgroud)