我正在做一个难题,我必须处理 10^18 的订单数。但是,我发现 python 无法在所有领域处理非常大的数字。
具体来说,如果我们指定 a = 1000000000000000000 (10^18) 并进行基本的算术计算(+、-、/、*),它的响应。但是,当我在 range() 中使用它时,它会显示 OverflowError
>>> a = 1000000000000000000
>>> a/2
500000000000000000L
>>> a*2
2000000000000000000L
>>> a+a
2000000000000000000L
>>> a*a
1000000000000000000000000000000000000L
>>> range(a)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
range(a)
OverflowError: range() result has too many items
>>> xrange(a)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
xrange(a)
OverflowError: Python int too large to convert to C long
我使用了 Python 2.7。
- 我该如何处理这种情况?,处理持有这些数字的谜题的最佳方法是什么。(教程/书籍参考将不胜感激)
- 为什么 Python 不能在 range()/xrange() 中处理它
我想在 python 2.7 中使用内置函数来完成它。这不可能吗?