IEEE754浮点数在多大程度上满足LessThanComparable? [英] Inhowfar do IEEE754 floats satisfy LessThanComparable?

查看:95
本文介绍了IEEE754浮点数在多大程度上满足LessThanComparable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

TL;DR Do the IEEE754 floating point values including NaN satisfy LessThanComparable?

具体来说,问题"为什么要发布/调试对于std :: min有不同的结果?"使我查找 LessThanComparable :

Specifically, question "Why does Release/Debug have a different result for std::min?" got me looking up LessThanComparable:

该类型必须与<一起使用.运算符,结果应符合标准 语义.

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

如果满足以下条件,则类型T满足LessThanComparable

The type T satisfies LessThanComparable if

给予

  • a,b和c,类型T或const T的表达式

以下表达式必须有效并且具有指定的表达式 效果

The following expressions must be valid and have their specified effects

通过以下方法建立 严格弱排序 关系特性 (...)

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

我在该标准中进行了仔细检查似乎那里的状态基本相同,我看了严格弱排序的Wikipedia定义.

I double checked it in the standard and it seems it states basically the same there, and I looked at the Wikipedia def of strict weak ordering.

有人认为包含NaN的IEEE浮点值集满足 not 这个概念:任何与NaN的比较都会产生错误,但是我一直在查看定义,对我来说一点都不很明显的NaN中断了严格弱排序:

Some think 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:

对于维基百科上给出的列表:

For the list given at Wikipedia:

  • 对于S中的所有x,并非x< x(不反身).
  • 对于x中的所有x,y,如果x< y则y< y并非如此. x(不对称). see below
  • 对于x中的所有x,y,z,如果x< y和y<则z <x z(传递性).
  • 对于S中的所有x,y,z,如果x与y不可比(x
  • For all x in S, it is not the case that x < x (irreflexivity).
  • For all x, y in S, if x < y then it is not the case that y < x (asymmetry). see below
  • For all x, y, z in S, if x < y and y < z then x < z (transitivity).
  • 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).

似乎Wikipedia上定义的严格的弱排序公理明确地指出了可能的incomparable值:NaN在这里似乎是一个很好的候选者?

It seems that the strict weak ordering axiom as defined on Wikipedia explicitly calls out possible incomparable values: NaN seems a good candidate here?

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

On the other hand, the standard says: (25.5/4)

如果我们将equiv(a, b)定义为!comp(a, b) && !comp(b, a),则 要求comp和equiv都是传递关系:

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)暗示comp(a, c)

(4.2)-equiv(a, b) && equiv(b, c)表示equiv(a, c)

根据这些定义,equiv(x, NaN)始终为true(因为!comp(a, NaN)==true !comp(Nan, a)==true:与Nan进行比较会得出错误,否定则得出true)

With these definitions, equiv(x, NaN) is always true (because !comp(a, NaN)==true and !comp(Nan, a)==true: comparison with Nan yields false, negation then yields true)

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

But clearly (4.2) is not satisfied, e.g:

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

该标准所定义的不是严格的弱订购吗?或者-更可能的是-我在这里缺少什么吗?

So is what the standard defines not a Strict Weak Ordering, or -- more likely indeed -- am I missing something here?

推荐答案

严格的弱排序要求存在强排序的等价类. IEEE754并非如此.

Strict weak ordering requires that strongly-ordered equivalence classes exist. This is not true of IEEE754.

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

The problem isn't that there exist multiple NaN values which are equivalent to each other, but that the entire class of NaNs is unordered with respect to the real line.

违反(4.2)会使您在Wikipedia中引用的第四个项目符号点的测试也失败(让y为NaN).

The violation of (4.2) causes the test in the fourth bullet point you quoted from Wikipedia to also fail (let y be a NaN).

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

For an example of incomparability that is allowed in a strict weak ordering, consider sign-magnitude integers. Then:

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

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

-0 < +0+0 < -0都不为真,因此排序很弱.但是,由这些等效值组成的类相对于所有其他值都具有强烈的顺序.

Neither -0 < +0 nor +0 < -0 is true, so the ordering is weak. But the class formed by these equivalent values is strongly ordered with respect to all others.

这篇关于IEEE754浮点数在多大程度上满足LessThanComparable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆