Skip to content

Commit 88275c3

Browse files
authored
Merge pull request #159 from kpreid/eq
Document signed zero hazards.
2 parents e2c7a82 + 85a4b9d commit 88275c3

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/lib.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,26 @@ fn canonicalize_signed_zero<T: FloatCore>(x: T) -> T {
7070
/// # use ordered_float::OrderedFloat;
7171
/// # use std::collections::HashSet;
7272
/// # use std::f32::NAN;
73-
///
7473
/// let mut s: HashSet<OrderedFloat<f32>> = HashSet::new();
7574
/// s.insert(OrderedFloat(NAN));
7675
/// assert!(s.contains(&OrderedFloat(NAN)));
7776
/// ```
7877
///
78+
/// Some non-identical values are still considered equal by the [`PartialEq`] implementation,
79+
/// and will therefore also be considered equal by maps, sets, and the `==` operator:
80+
///
81+
/// * `-0.0` and `+0.0` are considered equal.
82+
/// This different sign may show up in printing, or when dividing by zero (the sign of the zero
83+
/// becomes the sign of the resulting infinity).
84+
/// * All NaN values are considered equal, even though they may have different
85+
/// [bits](https://doc.rust-lang.org/std/primitive.f64.html#method.to_bits), and therefore
86+
/// different [sign](https://doc.rust-lang.org/std/primitive.f64.html#method.is_sign_positive),
87+
/// signaling/quiet status, and NaN payload bits.
88+
///
89+
/// Therefore, `OrderedFloat` may be unsuitable for use as a key in interning and memoization
90+
/// applications which require equal results from equal inputs, unless these cases make no
91+
/// difference or are canonicalized before insertion.
92+
///
7993
/// # Representation
8094
///
8195
/// `OrderedFloat` has `#[repr(transparent)]` and permits any value, so it is sound to use
@@ -1152,13 +1166,18 @@ impl<T: FloatCore + Num> Num for OrderedFloat<T> {
11521166
/// ```
11531167
/// # use ordered_float::NotNan;
11541168
/// # use std::collections::HashSet;
1155-
///
11561169
/// let mut s: HashSet<NotNan<f32>> = HashSet::new();
11571170
/// let key = NotNan::new(1.0).unwrap();
11581171
/// s.insert(key);
11591172
/// assert!(s.contains(&key));
11601173
/// ```
11611174
///
1175+
/// `-0.0` and `+0.0` are still considered equal. This different sign may show up in printing,
1176+
/// or when dividing by zero (the sign of the zero becomes the sign of the resulting infinity).
1177+
/// Therefore, `NotNan` may be unsuitable for use as a key in interning and memoization
1178+
/// applications which require equal results from equal inputs, unless signed zeros make no
1179+
/// difference or are canonicalized before insertion.
1180+
///
11621181
/// Arithmetic on NotNan values will panic if it produces a NaN value:
11631182
///
11641183
/// ```should_panic

0 commit comments

Comments
 (0)