OverflowerError:factorial()参数不应超过9223372036854775807

2024-04-24 18:59:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我试过了数学.阶乘()作为

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看起来很奇怪。你知道吗


Tags: inmostinputipython数学mathcallargument
1条回答
网友
1楼 · 发布于 2024-04-24 18:59:59

是的,一个有符号整数可以用64位来描述,但是其中一位是用来做符号的,所以数字本身只剩下63位(https://en.wikipedia.org/wiki/Two%27s_complement)。你知道吗

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

相关问题 更多 >