python - 为什么 Python 不抛出溢出错误?

标签 python overflow

<分区>

我正在学习 Python,我对数据类型的范围有疑问。

这个程序:

print("8 bits:", pow(2, 8)-1)
print("16 bits:", pow(2, 16)-1)
print("32 bits:", pow(2, 32)-1)
print("64 bits:", pow(2, 64)-1)

print( pow(18446744073709551615+18446744073709551615+2, 9) )

产生以下输出:

8 bits: 255
16 bits: 65535
32 bits: 4294967295
64 bits: 18446744073709551615
12663316555422952143897729076205936129798725073982046203600028471956337925454431
59912019973433564390346740077701202633417478988975650566195033836314121693019733
02667340133957632

我的问题是:Python 如何计算最后一次调用 pow() 的结果?我的 CPU 无法处理超过 64 位的整数,因此我预计该操作会产生溢出。

最佳答案

Python 长整数类型仅受可用内存的限制。在内存不足之前,数字会不断出现。

引自numeric types documentation :

Long integers have unlimited precision.

当您需要无限精度时,Python 将透明地使用长整数。在 Python 3 中,所有整数都是长整数;没有区别。

关于python - 为什么 Python 不抛出溢出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22224631/

相关文章:

python - Python 包安装程序 exe 如何工作?

c - 如何执行这个缓冲区溢出?在Ubuntu中

html - CSS 空白 nowrap 不起作用

css - 无法让滚动条出现在溢出时

css - HTML:如何为长段落创建只有垂直滚动条的 DIV?

python - 如何在 Django 2.1 中使用多种形式?

python - Pygame如何使用行走动画

python - 如果我们使用 Scikit-Learn 库的 RandomForestRegressor 有多个输出,如何计算分割的杂质减少量

python - 从字符串中提取元素作为 Pandas 列

CSS 父 DIV 溢出