首页 > 解决方案 > OverflowError: factorial() 参数不应超过 9223372036854775807

问题描述

我尝试了 math.factorial() 作为

In [18]: math.factorial(10**20)                                                                                   
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-18-88c39696b921> in <module>
----> 1 math.factorial(10**20)

OverflowError: factorial() argument should not exceed 9223372036854775807

我检查了:

In [19]: math.log2(9223372036854775807)                                                                           
Out[19]: 63.0

为什么限制不设置为 2^64,因为它是 8 个字节,63 看起来很奇怪。

标签: python

解决方案


是的,可以在 64 位上描述有符号整数,但其中一位用于符号,因此数字本身只剩下 63 位(https://en.wikipedia.org/wiki/Two%27s_complement)。

尽管 Python 整数可以表示任何有符号数,但在某些模块中存在限制。


推荐阅读