简体   繁体   English

为什么浮点数与无穷大的比较有效?

[英]Why does comparison of floating-point to infinity work?

As anyone knowledgeable enough will know, you cannot compare two floating-point numbers with simple logic operators and expect a logical result.任何知识渊博的人都知道,您无法使用简单的逻辑运算符比较两个浮点数并期望得到逻辑结果。

Why then, does a logical EQUAL TO comparison to INFINITY always return true when the number is, in fact, INFINITY ?那么,为什么当数字实际上是INFINITY时,与INFINITY的逻辑 EQUAL TO 比较总是返回 true 呢?

PS: I tried the same comparison with NAN , but it seems only INFINITY can be reliably detected as being equal to another number of the same value. PS:我尝试了与NAN相同的比较,但似乎只有INFINITY可以可靠地检测为等于另一个相同值的数字。

(Edit) Here is my implementation's (GCC) definition of INFINITY and NAN : (编辑)这是我的实现(GCC)对INFINITYNAN的定义:

#  define INFINITY (__builtin_inff ())
#  define NAN (__builtin_nanf (""))

As anyone knowledgeable enough will know, you cannot compare two floating-point numbers with simple logic operators and expect a logical result.任何知识渊博的人都知道,您无法使用简单的逻辑运算符比较两个浮点数并期望得到逻辑结果。

This has no basis in the IEEE 754 standard or any other specification of floating-point behavior I am aware of.这在 IEEE 754 标准或我所知道的任何其他浮点行为规范中没有任何依据。 It is an unfortunately common misstatement of floating-point arithmetic.不幸的是,这是浮点运算的常见错误陈述。

The fact is that comparison for equality is a perfect operation in floating-point: It produces true if and only if the two operands represent the same number.事实上,相等性比较在浮点数中是一个完美的运算:当且仅当两个操作数表示相同的数字时,它才会产生 true。 There is never any error in a comparison for equality.平等比较永远不会有任何错误。

Another misstatement is that floating-point numbers approximate real numbers.另一个错误陈述是浮点数近似于实数。 Per IEEE 754, each floating-point value other than NaN represents one number, and it represents that number exactly.根据 IEEE 754,除 NaN 之外的每个浮点值都代表一个数字,并且它准确地代表该数字。

The fact is that floating-point numbers are exact while floating-point operations approximate real arithmetic;事实上,浮点数是精确的,而浮点运算近似于实数算术; correctly-rounded operations produce the nearest representable value (nearest in any direction or in a selected direction, with various rules for ties).正确舍入的操作会产生最接近的可表示值(在任何方向或选定方向上最接近,具有各种平局规则)。

This distinction is critical for understanding, analyzing, designing, and writing proofs about floating-point arithmetic.这种区别对于理解、分析、设计和编写有关浮点运算的证明至关重要。

Why then, does a logical EQUAL TO comparison to INFINITY always return true when the number is, in fact, INFINITY?那么,为什么当数字实际上是 INFINITY 时,与 INFINITY 的逻辑 EQUAL TO 比较总是返回 true?

As stated above, comparison for equality produces true if and only if its operands represent the same number.如上所述,当且仅当其操作数表示相同的数字时,相等性比较才会产生 true。 If x is infinity, then x == INFINITY returns true.如果x是无穷大,则x == INFINITY返回 true。 If x is three, then x == 3 returns true.如果x是三,则x == 3返回 true。

People sometimes run into trouble when they do not understand what value is in a number.当人们不理解数字的价值时,他们有时会遇到麻烦。 For example, in float x = 3.3;例如,在float x = 3.3; , people sometimes do not realize that C converts the double 3.3 to float , and therefore x does not contain the same value as 3.3 . ,人们有时没有意识到 C 将double精度3.3转换为float ,因此x不包含与3.3相同的值。 This is because the conversion operation approximates its results, not because the value of x is anything other than its specific assigned value.这是因为转换操作近似于其结果,而不是因为xvalue不是其指定的特定值。

I tried the same comparison with NAN ,…我尝试与NAN进行相同的比较,...

A NaN is Not a Number, so, in a comparison for equality, it never satisfies “the two operands represent the same number”, so the comparison produces false. NaN is Not a Number,因此,在比较相等性时,它永远不会满足“两个操作数表示相同的数”,因此比较结果为 false。

You cannot compare two floating-point numbers with simple logic operators and expect a logical result.您不能使用简单的逻辑运算符比较两个浮点数并期望得到逻辑结果。

There are traps surrounding comparing two floating-point numbers because they might not be exactly what you expect due to rounding error due lack of precision.比较两个浮点数存在陷阱,因为由于缺乏精度而导致舍入误差,它们可能与您所期望的不完全相同。 Sometimes, a number is very close to but not exactly what we expect.有时,一个数字非常接近但不完全符合我们的预期。 And this is what causes problems.这就是导致问题的原因。

This problem doesn't apply to +Inf.此问题不适用于 +Inf。 +Inf can't accidentally be stored as some number very close to +Inf. +Inf 不会意外地存储为非常接近 +Inf 的某个数字。 +Inf is a specific bit pattern. +Inf 是一个特定的位模式。 It's not something like INT_MAX;它不是 INT_MAX 之类的东西; it's a value that's special to the processor itself.它是处理器本身的特殊值。

Same goes for -Inf. -Inf 也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM