gpt4 book ai didi

python - python 什么时候引发 FloatingPointError?

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

Python 文档说当 float 计算失败时会引发 FloatingPointError。但是,“浮点计算” 在这里的确切含义是什么?我尝试用 float 进行加法、乘法和除法,但从未设法引发此特定错误。相反,我得到了一个 TypeError:

10/'a'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'int' and 'str'

有人可以帮我理解什么时候在 python 中引发了 FloatingPointError 吗?

最佳答案

它是 fpectl 的一部分模块。 FloatingPointError如果您没有明确打开它,则不应引发 (fpectl.turnon_sigfpe())。

不过请注意:

The fpectl module is not built by default, and its usage is discouraged and may be dangerous except in the hands of experts. See also the section fpectl-limitations on limitations for more details.

更新:fpectl 模块已removed as of Python 3.7 .


即使启用了 FloatingPointErrors,10/'a' 也永远不会引发错误。它总是会引发 TypeError。 FloatingPointError 只会针对达到实际执行 float 学运算的操作引发,例如 1.0/0.010/'a' 没有那么远。

关于python - python 什么时候引发 FloatingPointError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41223865/

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