gpt4 book ai didi

python - OverflowError 真的被提出了吗?

转载 作者:太空狗 更新时间:2023-10-30 00:57:30 29 4
gpt4 key购买 nike

根据python文档

exception OverflowError
Raised when the result of an arithmetic operation is too large to
be represented. This cannot occur for long integers (which would
rather raise MemoryError than give up) and for most operations with
plain integers, which return a long integer instead. Because of the
lack of standardization of floating point exception handling in C,
most floating point operations also aren’t checked.

确实,当溢出的整数没有自动转换为 long 时,这个错误是有意义的。同样, float 溢出到 inf。我真的没有看到标准解释器仍可能引发 OverflowError 的任何情况。某处有这种情况吗?只是出于好奇。

最佳答案

Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> float(10**1000)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to float

想想看(我想我在评论中看到的第一个已经消失了,所以我不确定该归功于谁):

>>> 10.0**1000
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: (34, 'Result too large')
>>> 10j**1000
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: complex exponentiation

这些都是 x-to-int-power 或 int-to-float(或复杂的作品)类型。

而且——因为它出现在相关问题的右侧! -- 有:

>>> xrange(10**100)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long

这是不同的类型。

关于python - OverflowError 真的被提出了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7138387/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com