为什么浮点数无穷大,不像NaN,等于? [英] Why are floating point infinities, unlike NaNs, equal?

查看:303
本文介绍了为什么浮点数无穷大,不像NaN,等于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么无限比较遵循应用于NaNs的逻辑?此代码打印出 false 三次:

Why doesn't infinity comparison follow the logic applied to NaNs? This code prints out false three times:

double a = Double.NaN;
double b = Double.NaN;
System.out.println(a == b); // false
System.out.println(a < b); //  false
System.out.println(a > b); //  false

但是,如果我更改 Double.NaN Double.POSITIVE_INFINITY ,我得到 true 以等于,但 code>对于大于和小于比较:

However, if I change Double.NaN to Double.POSITIVE_INFINITY, I get true for equality, but false for the greater-than and less-than comparisons:

double a = Double.POSITIVE_INFINITY;
double b = Double.POSITIVE_INFINITY;
System.out.println(a == b); // true
System.out.println(a < b); //  false
System.out.println(a > b); //  false

这似乎很危险。假设无限值是由溢出引起的,我想象更有可能的是,最终以无穷大的两个变量在完美的算术中实际上不相等。

This seems dangerous. Assuming that infinite values result from overflows, I imagine it's more likely that two variables that ended up as infinities wouldn't actually be equal in perfect arithmetic.

推荐答案

您的推理是, Double.POSITIVE_INFINITY 不应该等于它自己,因为它是可能已经获得作为精度损失的结果。

Your reasoning is that Double.POSITIVE_INFINITY should not be equal to itself because it is "likely" to have been obtained as the result of a loss of accuracy.

这一推理适用于所有浮点型。作为不准确操作的结果,可以获得任何有限值。这并没有推动IEEE 754标准化委员会将 == 定义为总是对有限值求值为false,为什么无穷大不同?

This line of reasoning applies to all of floating-point. Any finite value can be obtained as the result of an inaccurate operation. That did not push the IEEE 754 standardization committee to define == as always evaluating to false for finite values, so why should infinities be different?

根据定义, == 对于了解其功能的人已获得的浮点值,当然不是应通过实际计算获得的值)。对于任何理解这种情况的人,你需要理解它使用浮点,即使是不涉及无穷大的计算, Double.POSITIVE_INFINITY == Double.POSITIVE_INFINITY 计算true是方便的,如果只是为了测试浮点计算的浮点结果是否 Double.POSITIVE_INFINITY

As defined, == is useful for people who understand what it does (that is, test the floating-point values that have been obtained, and certainly not the values that should have been obtained with real computations). For anyone who understands that, and you need to understand it to use floating-point even for computations that do not involve infinity, having Double.POSITIVE_INFINITY == Double.POSITIVE_INFINITY evaluate to true is convenient, if only to test if the floating-point result of a floating-point computation is Double.POSITIVE_INFINITY.

这留下了为什么NaN可以负担得起特殊行为的问题,无限性应该遵循与有限值相同的一般原则。 NaN不等于无穷大:IEEE 754标准的基本原理是值正好是它们是什么,但是操作的结果可以相对于实际结果近似,并且在这种情况下,得到的浮点值根据舍入模式获得。

That leaves the question of why NaN can afford to have special behavior, and infinities should follow the same general principles as finite values. NaN is different from infinities: the underlying principle of the IEEE 754 standard is that values are exactly what they are, but the result of an operation can be approximated with respect to the real result, and in this case, the resulting floating-point value is obtained according to the rounding mode.

忘记将 1.0 / 0.0 定义为+ inf,这是本讨论中的烦恼。仅考虑 1.0e100 / 1.0e-300 的操作结果 Double.POSITIVE_INFINITY Double.MAX_VALUE + Double.MAX_VALUE 。对于这些操作,+ inf是真实结果的最接近的近似,就像产生有限结果的操作一样。相反,NaN是当操作没有意义时获得的结果。

Forget for an instant that 1.0 / 0.0 is defined as +inf, which is an annoyance in this discussion. Think for the moment of Double.POSITIVE_INFINITY only as the result of operations such as 1.0e100 / 1.0e-300 or Double.MAX_VALUE + Double.MAX_VALUE. For these operations, +inf is the closest approximation of the real result, just like for operations that produce a finite result. By contrast, NaN is the result you obtain when the operation doesn't make sense. It is defensible to have NaN behave specially, but inf is just an approximation of all the values too large to represent.

实际上, 1.0 /

In reality, 1.0 / 0.0 also produces +inf, but that should be considered an exception. It would have been just as coherent to define the result of that operation as NaN, but defining it as +inf was more convenient in the implementation of some algorithms. An example is provided page 10 in Kahan's notes. More details than most will wish for are in the article "Branch Cuts for Complex Elementary Functions, or Much Ado About Nothing's Sign Bit". I would also interpret the existence in IEEE 754 of a "division by zero" flag separate from the NaN flag as recognition that the user may want to treat division by zero specially although it is not defined as producing NaN.

这篇关于为什么浮点数无穷大,不像NaN,等于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆