gpt4 book ai didi

Python3 无穷大/NaN : Decimal vs. float

转载 作者:行者123 更新时间:2023-11-28 16:34:27 27 4
gpt4 key购买 nike

给定(Python3):

>>> float('inf') == Decimal('inf')
True

>>> float('-inf') <= float('nan') <= float('inf')
False

>>> float('-inf') <= Decimal(1) <= float('inf')
True

为什么以下无效?我读过Special values .

无效

>>> Decimal('-inf') <= Decimal('nan') <= Decimal('inf')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]

>>> Decimal('-inf') <= float('nan') <= Decimal('inf')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]

>>> float('-inf') <= Decimal('nan') <= float('inf')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]

最佳答案

来自 decimal.py source code :

# Note: The Decimal standard doesn't cover rich comparisons for
# Decimals. In particular, the specification is silent on the
# subject of what should happen for a comparison involving a NaN.
# We take the following approach:
#
# == comparisons involving a quiet NaN always return False
# != comparisons involving a quiet NaN always return True
# == or != comparisons involving a signaling NaN signal
# InvalidOperation, and return False or True as above if the
# InvalidOperation is not trapped.
# <, >, <= and >= comparisons involving a (quiet or signaling)
# NaN signal InvalidOperation, and return False if the
# InvalidOperation is not trapped.
#
# This behavior is designed to conform as closely as possible to
# that specified by IEEE 754.

并且来自 Special values section你说你读过:

An attempt to compare two Decimals using any of the <, <=, > or >= operators will raise the InvalidOperation signal if either operand is a NaN, and return False if this signal is not trapped.

请注意,IEEE 754 使用 NaN 作为浮点异常值;例如你做了一些无法计算的事情,而是得到了一个异常。它是一个信号值,应该被视为一个错误,而不是用来与其他 float 进行比较的东西,这就是为什么在 IEEE 754 标准中它不等于其他任何东西。

此外,特殊值部分提到:

Note that the General Decimal Arithmetic specification does not specify the behavior of direct comparisons; these rules for comparisons involving a NaN were taken from the IEEE 854 standard (see Table 3 in section 5.7).

looking at IEEE 854 section 5.7我们发现:

In addition to the true/false response, an invalid operation exception (see 7.1) shall be signaled when, as indicated in the last column of Table 3, “unordered” operands are compared using one of the predicates involving “<” or “>” but not “?.” (Here the symbol “?” signifies “unordered.” )

与分类为无序的 NaN 进行比较。

默认 InvalidOperation被捕获,因此在使用 <= 时引发 Python 异常和 >=反对Decimal('NaN') .这是一个合乎逻辑的扩展; Python 有实际 异常,因此如果您与 NaN 异常值进行比较,您可以预期会引发异常。

您可以使用 Decimal.localcontext() 来禁用陷印:

>>> from decimal import localcontext, Decimal, InvalidOperation
>>> with localcontext() as ctx:
... ctx.traps[InvalidOperation] = 0
... Decimal('-inf') <= Decimal('nan') <= Decimal('inf')
...
False

关于Python3 无穷大/NaN : Decimal vs. float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28371355/

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