IEEE754 中 inf==inf 的基本原理是什么

What is the rationale for inf==inf in IEEE754

当您有符合 IEEE754 标准的浮点实现时,与 NaN 的任何比较都是 false,甚至 NaN == NaN,但 +inf == +inftrue,为什么?

从我的角度来看,说 +inf == +inf 是错误的更有意义,原因:

编辑

正如 nwellnhof allready 所写:链接问题:,不一样,有问题 "why is the language implementation this way?",这里是问题 "Why is the standard defined this way?"。 (而且两个问题都来自同一个用户)

您可能需要询问 IEEE 754-1985 背后的主要架构师 William Kahan,但 this answer 阐明了该主题:

more importantly, there was no isnan( ) predicate at the time that NaN was formalized in the 8087 arithmetic; it was necessary to provide programmers with a convenient and efficient means of detecting NaN values that didn’t depend on programming languages providing something like isnan( ) which could take many years. I’ll quote Kahan’s own writing on the subject:

Were there no way to get rid of NaNs, they would be as useless as Indefinites on CRAYs; as soon as one were encountered, computation would be best stopped rather than continued for an indefinite time to an Indefinite conclusion. That is why some operations upon NaNs must deliver non-NaN results. Which operations? … The exceptions are C predicates “ x == x ” and “ x != x ”, which are respectively 1 and 0 for every infinite or finite number x [emphasis added] but reverse if x is Not a Number ( NaN ); these provide the only simple unexceptional distinction between NaNs and numbers [emphasis added] in languages that lack a word for NaN and a predicate IsNaN(x).

如果 +inf 不等于 +inf,则对 NaN 的 x != x 测试将不起作用,因为它也会捕获无穷大。回到 1985 年,C 程序员可以写:

#define is_nan(x)     ((x) != (x))
#define is_pos_inf(x) ((x) ==  1.0/0.0)
#define is_neg_inf(x) ((x) == -1.0/0.0)

对于 inf != inf,您需要这样的东西:

#define is_nan(x)     (!((x) >= 0) && !((x) <= 0))
#define is_pos_inf(x) ((x) != (x) && (x) > 0.0)
#define is_neg_inf(x) ((x) != (x) && (x) < 0.0)

我明白你的意思,我同意从纯数学的角度来看 +inf != +inf 更正确。但是 IMO,它并没有超过实际考虑。

The [sets] of natural numbers and rational numbers, both are infinite but [have] not the same [cardinality].

这与 floating-point 计算没有多大关系。

If you have X=1e200 and Y=1e300 (both, X and Y are 64-bit doubles), so x==y is false, but x*1e200==y*1e200 is true true (both are +inf), which is mathematical incorrect.

浮点数学本质上是不正确的。您可以找到许多 finite floating-point 数字,XYZX != Y,其中 X <op> Z == Y <op> Z.

I do not see any advantage, or any application which require the fact that +inf == +inf. You should not compare any floating point values with == anyway.

我也看不到需要 +inf != +inf 的应用程序。

X==Y is [...] true, if X-Y==0 is true, but inf-inf is NaN.

这实际上是 +inf != +inf 可以解决的不一致问题。但这对我来说似乎是一个小细节。