IEEE754 浮点数在多大程度上满足 LessThanComparable?

Inhowfar do IEEE754 floats satisfy LessThanComparable?

TL;DRIEEE754浮点值包括NaN是否满足LessThanComparable?

具体来说,问题“" got me looking up LessThanComparable:

The type must work with < operator and the result should have standard semantics.

Requirements

The type T satisfies LessThanComparable if

Given

  • a, b, and c, expressions of type T or const T

The following expressions must be valid and have their specified effects

Establishes strict weak ordering relation with the following properties (...)

我在 the standard 中仔细检查了它,看起来它在那里的状态基本相同,我查看了 strict weak ordering.[=32 的 Wikipedia def =]

that the set of IEEE floating point vales that includes NaN does not satisfy this concept: Any comparison with NaN on either side will always yield false, but I have been looking at the definitions, and it is not at all apparent to me whether the presence of NaN breaks strict weak ordering:

维基百科给出的列表:

维基百科上定义的严格弱序公理似乎明确指出了可能的 incomparable 值:NaN 在这里似乎是个不错的选择?

另一方面,标准说:(25.5/4)

If we define equiv(a, b) as !comp(a, b) && !comp(b, a), then the requirements are that comp and equiv both be transitive relations:

(4.1) — comp(a, b) && comp(b, c) implies comp(a, c)

(4.2) — equiv(a, b) && equiv(b, c) implies equiv(a, c)

根据这些定义,equiv(x, NaN) 始终是 true(因为 !comp(a, NaN)==true !comp(Nan, a)==true:与 Nan 的比较结果为 false , 否定则产生 true)

但是显然(4.2)不满足,例如:

 equiv(3.0, NaN) && equiv(NaN, 7.0) **does not** imply equiv(3.0, 7.0)

标准定义的不是严格弱排序,或者——更有可能——我在这里遗漏了什么吗?

严格的弱排序要求强排序等价 classes 存在。这不是 IEEE754 的真实情况。

问题不在于存在多个彼此等价的 NaN 值,而是整个 class 个 NaN 相对于实线是无序的。

违反 (4.2) 会导致您从维基百科引用的第四个要点中的测试也失败(设 y 为 NaN)。


有关严格弱排序中允许的不可比性的示例,请考虑符号幅度整数。那么:

-4 < -3 < -2 < -1 < { -0, +0 } < +1 < +2 < +3 < +4

-0 < +0+0 < -0 都不正确,因此排序不正确。但是由这些等效值形成的 class 相对于所有其他值是强有序的。

您似乎是在说维基百科的定义允许 NaN,但标准的定义不允许。我不认为你是对的。来自维基百科的定义:

For all x, y, z in S, if x is incomparable with y (neither x < y nor y < x hold), and y is incomparable with z, then x is incomparable with z (transitivity of incomparability).

设x为3.0,y为NaN,z为7.0。 x与y不可比。 y 与 z 无法比较。 x 不是 与 z.

没有可比性

IEEE754 浮点值包括 NaN 满足 LessThanComparable。

如果您从维基百科获取第 4 个要点并将 uncomparable 替换为 equiv,您的条件与标准中的相同。


这是一个很好的答案,基本上回答了我对 C++ 的所有疑虑。这个:

它甚至引用了 std 中的相同段落,就像我在上面的问题最后所做的那样:

If we define equiv(a, b) as !comp(a, b) && !comp(b, a), then the requirements are that comp and equiv both be transitive relations ... equiv(a, b) && equiv(b, c) implies equiv(a, c)

对于 a = 0.0、b = NaN、c = 1.0、comp = std::less<double>()

,此操作失败

它还解决了一个有趣的问题。要求:

Something I've always considered a little odd is that the standard expresses the requirements in terms of the key type, not in terms of the actual key values added to the container. I believe you could choose to read this as not guaranteeing that map<double, int> has defined behaviour at all if the implementation supports NaNs, regardless of whether you actually add a NaN to an instance or not.