简体   繁体   English

python什么时候引发FloatingPointError?

[英]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() ). 如果您未明确将其打开,则不应引发FloatingPointErrorfpectl.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 section fpectl-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上下文管理器)标志来触发numpyFloatingPointError 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.

相关问题 Python中提升的内容是什么? - What does raise in Python raise? pandas:使用np.seterr(all =&#39;raise&#39;)和缺少数据的FloatingPointError - pandas: FloatingPointError with np.seterr(all='raise') and missing data Python:当关键字不匹配时,如何引发异常? - Python: How to raise an exception when keyword does not match up? 为什么给定 python 字符串时 exec 会引发语法错误? - Why does exec raise a syntax error when given python string? 除以零时获取FloatingPointError而不是ZeroDivisionError - Getting FloatingPointError instead of ZeroDivisionError when dividing by zero 为什么python不会引发NameError - Why python does not raise NameError 当 class 变量被分配为列表时,python 中的数据类不会引发错误(但有输入提示) - Dataclass in python does not raise error when the class variable is assigned as a list (but does with typing hints) 为什么Python的reduce用于汇总列表列表时会引发TypeError? - Why does Python's reduce raise a TypeError when used to sum a list of lists? 如果在子进程执行时执行Ctrl + C,Python是否总是引发异常? - Does Python always raise an exception if you do Ctrl+C when a subprocess is executing? python 3 configparser.read() 在给定不存在的文件时不会引发异常 - python 3 configparser.read() does not raise an exception when given a non-existing file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM