[英]When does python raise a FloatingPointError?
Python documentation says that FloatingPointError is raised when a float calculation fails. Python文档说,当float计算失败时,将引发FloatingPointError。 But what is exactly meant here by "a float calculation" ?
但是,这里的“浮点计算”到底是什么意思? I tried adding, multiplying and dividing with floats but never managed to raise this specific error.
我尝试用浮点数进行加,乘和除,但从未设法引发此特定错误。 Instead, i got a
TypeError
: 相反,我得到了
TypeError
:
10/'a'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'int' and 'str'
Can someone help me understand when a FloatingPointError
is raised in python? 有人可以帮助我了解python中引发
FloatingPointError
情况吗?
It is part of the fpectl
module. 它是
fpectl
模块的一部分。 The FloatingPointError
shouldn't be raised if you don't explicitly turn it on ( fpectl.turnon_sigfpe()
). 如果您未明确将其打开,则不应引发
FloatingPointError
( fpectl.turnon_sigfpe()
)。
However mind the note: 不过请注意:
The
fpectl
module is not built by default, and its usage is discouraged and may be dangerous except in the hands of experts.fpectl
模块不是默认构建的,不鼓励使用,除非在专家手中,否则它可能很危险。 See also the sectionfpectl-limitations
on limitations for more details.另请参阅
fpectl-limitations
限制一节以获取更多详细信息。
Update: The fpectl
module has been removed as of Python 3.7 . 更新:
fpectl
模块已从Python 3.7中删除 。
Even with FloatingPointErrors turned on, 10/'a'
will never raise one. 即使启用了FloatingPointErrors,
10/'a'
也永远不会引发1。 It will always raise a TypeError. 它将始终引发TypeError。 A FloatingPointError will only be raised for operations that reach the point of actually performing floating-point math, like
1.0/0.0
. 仅对于达到实际执行浮点数学运算的操作,例如
1.0/0.0
,才会引发FloatingPointError。 10/'a'
doesn't get that far. 10/'a'
没那么远。
You can also trigger a FloatingPointError
within numpy
, by setting the appropriate numpy.seterr
(or numpy.errstate
context manager) flag. 您还可以通过设置适当的
numpy.seterr
(或numpy.errstate
上下文管理器)标志来触发numpy
的FloatingPointError
。 For an example taken from the documentation: 有关从文档中获取的示例:
>>> np.sqrt(-1) nan >>> with np.errstate(invalid='raise'): ... np.sqrt(-1) Traceback (most recent call last): File "<stdin>", line 2, in <module> FloatingPointError: invalid value encountered in sqrt
Interestingly, it also raises FloatingPointError
when all operands are integers: 有趣的是,当所有操作数都是整数时,它也会引发
FloatingPointError
:
>>> old_settings = np.seterr(all='warn', over='raise') >>> np.int16(32000) * np.int16(3) Traceback (most recent call last): File "<stdin>", line 1, in <module> FloatingPointError: overflow encountered in short_scalars
The documentation notes the conditions under which the FloatingPointError
will be raised: 该文档指出了引发
FloatingPointError
的条件:
The floating-point exceptions are defined in the IEEE 754 standard [1]:
浮点异常在IEEE 754标准[1]中定义:
- Division by zero: infinite result obtained from finite numbers.
除以零:从有限数获得的无限结果。
- Overflow: result too large to be expressed.
溢出:结果太大而无法表达。
- Underflow: result so close to zero that some precision was lost.
下溢:结果非常接近零,以至于损失了一些精度。
- Invalid operation: result is not an expressible number, typically indicates that a NaN was produced.
无效的运算:结果不是可表示的数字,通常表示产生了NaN。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.