python - 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 看起来很奇怪。
解决方案
是的,可以在 64 位上描述有符号整数,但其中一位用于符号,因此数字本身只剩下 63 位(https://en.wikipedia.org/wiki/Two%27s_complement)。
尽管 Python 整数可以表示任何有符号数,但在某些模块中存在限制。
推荐阅读
- python - Python:输入到datagridview的正确格式
- objective-c - 如何一次更改整个应用程序的文本对齐方式?
- r - 聚合每个观察是否可以属于具有多个分组变量的多个组
- java - 带有枚举列表的 Jpa 存储库
- spring-data-mongodb - Spring Data MongoDB 基于接口的递归投影
- node.js - 如何使用 Knex CLI
- sql - 显示月份的所有日期以及日期名称
- ruby-on-rails - Rails - 简单的表单,不将输入保存到数据库
- php - 如何在 PHP 中将非 UTF-8 编码转换为更具可读性/ UTF-8 编码?
- php - 如何避免在 Joomla 组件的模板之外处理 HTML?