浮点比较是连续的吗?如果 !(a <= b) 是 (b > a) 保证?

Are floating point comparisons continuous? If !(a <= b) is (b > a) guaranteed?

是否保证浮点数:

我目前正在处理跨语言,但如果需要,您可以假设使用 C、C++ 或 C# 之一的 floatdouble。假设不涉及 NaN

我假设 IEEE 的规则适用于此。我不确定他们的订购规则是否暗示了上述内容。

我特别担心的是 ab 并不总是相同的变量,但会是相同的值。它们总是来自完全相同的计算。但是,由于比较是在不同的代码位置进行的,所以我无法保证它们是否始终是 stored/truncated 形式(最终可能会以更高的精度结束)。

因为我正在寻找保证,答案应该包括做出这些保证的参考,或者 officialy/strongly 暗示保证。

根据 IEEE 754 (2008) 标准 (§ 5.11):

Four mutually exclusive relations are possible: less than, equal, greater than, and unordered. The last case arises when at least one operand is NaN. Every NaN shall compare unordered with everything, including itself. Comparisons shall ignore the sign of zero (so +0 = −0). Infinite operands of the same sign shall compare equal.

所以是的,假设两个参数都不是 NaN,那么这两个陈述都是正确的。

至于隐式扩展精度格式的使用,问题是允许这些意味着它不再严格遵循 IEEE 754 标准。请参阅 this excellent answer and discussion 关于启用严格合规性。

更新:据我了解,如果您使用的是最近的标准(例如 -std=c99),那么您不必担心中间精度,只要您正在比较的东西被声明变量(例如 double a = 0.1; if (a < b) ...)而不是常量(例如 if (0.1 < b) ...)。

答案是,但这只是因为根据我对 IEEE-754 标准的阅读,您排除了 NaN。通常答案是。标准的相关部分内容如下:

5.11 Details of comparison predicates

For every supported arithmetic format, it shall be possible to compare one floating-point datum to another in that format (see 5.6.1). Additionally, floating-point data represented in different formats shall be comparable as long as the operands’ formats have the same radix.

Four mutually exclusive relations are possible: less than, equal, greater than, and unordered. The last case arises when at least one operand is NaN. Every NaN shall compare unordered with everything, including itself. Comparisons shall ignore the sign of zero (so +0 = −0). Infinite operands of the same sign shall compare equal.

Languages define how the result of a comparison shall be delivered, in one of two ways: either as a relation identifying one of the four relations listed above, or as a true-false response to a predicate that names the specific comparison desired.

Table 5.1, Table 5.2, and Table 5.3 exhibit twenty-two functionally distinct useful predicates and negations with various ad-hoc and traditional names and symbols. Each predicate is true if any of its indicated relations is true. The relation “?” indicates an unordered relation. Table 5.2 lists five unordered-signaling predicates and their negations that cause an invalid operation exception when the relation is unordered. That invalid operation exception defends against unexpected quiet NaNs arising in programs written using the standard predicates {<, <=, >=, >} and their negations, without considering the possibility of a quiet NaN operand. Programs that explicitly take account of the possibility of quiet NaN operands may use the unordered-quiet predicates in Table 5.3 which do not signal such an invalid operation exception.

Comparisons never signal an exception other than the invalid operation exception.

Note that predicates come in pairs, each a logical negation of the other; applying a prefix such as NOT to negate a predicate in Table 5.1, Table 5.2, and Table 5.3 reverses the true/false sense of its associated entries, but does not change whether unordered relations cause an invalid operation exception.

The unordered-quiet predicates in Table 5.1 do not signal an exception on quiet NaN operands:

Table 5.1—Required unordered-quiet predicate and negation

The unordered-signaling predicates in Table 5.2, intended for use by programs not written to take into account the possibility of NaN operands, signal an invalid operation exception on quiet NaN operands:

Table 5.2—Required unordered-signalling predicates and negations

The unordered-quiet predicates in Table 5.3, intended for use by programs written to take into account the possibility of NaN operands, do not signal an exception on quiet NaN operands:

Table 5.3—Required unordered-quiet predicates and negations

There are two ways to write the logical negation of a predicate, one using NOT explicitly and the other reversing the relational operator. Thus in programs written without considering the possibility of a NaN operand, the logical negation of the unordered-signaling predicate (X < Y) is just the unordered-signaling predicate NOT(X < Y); the unordered-quiet reversed predicate (X ?>= Y) is different in that it does not signal an invalid operation exception when X and Y are unordered (unless X or Y is a signaling NaN). In contrast, the logical negation of (X = Y) might be written as either NOT(X = Y) or (X ?<> Y); in this case both expressions are functionally equivalent to (X ≠ Y).

因此,对我而言,由于 UN 关系因不存在 NaN 而被排除,因此剩下的就是集合 EQ LT GT。肯定LT EQ就意味着否定GT,否定LT EQ就是GT.