Skip to content

Commit d5dc66a

Browse files
pkgwcatamorphism
authored andcommitted
core: Align cmp::le() with the other implementations
Also add comments reminding that IEEE 754 requires unusual semantics for comparison operators as applied to NaNs (x != x, if x = NaN), in case someone in the future wants to get clever.
1 parent 7eae397 commit d5dc66a

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/libcore/cmp.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ and `Eq` to overload the `==` and `!=` operators.
3030
*
3131
* Eventually this may be simplified to only require
3232
* an `eq` method, with the other generated from
33-
* a default implementation.
33+
* a default implementation. However it should
34+
* remain possible to implement `ne` separately, for
35+
* compatibility with floating-point NaN semantics
36+
* (cf. IEEE 754-2008 section 5.11).
3437
*/
3538
#[lang="eq"]
3639
pub trait Eq {
@@ -43,7 +46,10 @@ pub trait Eq {
4346
*
4447
* Eventually this may be simplified to only require
4548
* an `le` method, with the others generated from
46-
* default implementations.
49+
* default implementations. However it should remain
50+
* possible to implement the others separately, for
51+
* compatibility with floating-point NaN semantics
52+
* (cf. IEEE 754-2008 section 5.11).
4753
*/
4854
#[lang="ord"]
4955
pub trait Ord {
@@ -59,8 +65,8 @@ pub pure fn lt<T: Ord>(v1: &T, v2: &T) -> bool {
5965
}
6066

6167
#[inline(always)]
62-
pub pure fn le<T: Ord Eq>(v1: &T, v2: &T) -> bool {
63-
(*v1).lt(v2) || (*v1).eq(v2)
68+
pub pure fn le<T: Ord>(v1: &T, v2: &T) -> bool {
69+
(*v1).le(v2)
6470
}
6571

6672
#[inline(always)]

0 commit comments

Comments
 (0)