gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:07:32 36 4
gpt4 key购买 nike

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

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

  • 自然数和有理数的数,都是无限的,但又不一样。

  • 如果您有 X=1e200Y=1e300(X 和 Y 都是 64 位 double ),那么 x== yfalse,但 x*1e200==y*1e200 为 true true(均为 +inf),即数学不正确。

  • 对于NaN,已经需要特殊处理,其中X==Xfalse,所以不会有实现 +inf == +inf 返回 false 的实现复杂度要高得多。甚至可能更少,因为 infNaN 我们使用相同的“指数”。

  • 我没有看到任何优势,或者任何需要 +inf == +inf 的应用程序。无论如何,您都不应该将任何浮点值与 == 进行比较。

  • X==Y 是generel 然后true,如果X-Y==0true , 但 inf-infNaN

编辑

正如 nwellnhof allready 所写:链接的问题:C IEEE-Floats inf equal inf ,不一样,有问题“为什么语言实现是这样的?”,这里有问题“为什么标准是这样定义的?”。 (而且两个问题都来自同一个用户)

最佳答案

您可能不得不问 William Kahan,IEEE 754-1985 背后的主要架构师,但是 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 , x != x测试 NaN 是行不通的,因为它也会捕获无穷大。早在 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].

这与浮点计算没有太大关系。

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.

float 学本质上是不正确的。您可以找到许多有限 float ,X , Y , Z , 与 X != 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 不一致会解决。但对我来说这似乎是一个小细节。

关于floating-point - IEEE754 中 inf==inf 的基本原理是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41958609/

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