Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document signed zero hazards. #159

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,26 @@ fn canonicalize_signed_zero<T: FloatCore>(x: T) -> T {
/// # use ordered_float::OrderedFloat;
/// # use std::collections::HashSet;
/// # use std::f32::NAN;
///
/// let mut s: HashSet<OrderedFloat<f32>> = HashSet::new();
/// s.insert(OrderedFloat(NAN));
/// assert!(s.contains(&OrderedFloat(NAN)));
/// ```
///
/// Some non-identical values are still considered equal by the [`PartialEq`] implementation,
/// and will therefore also be considered equal by maps, sets, and the `==` operator:
///
/// * `-0.0` and `+0.0` are considered equal.
/// This different sign may show up in printing, or when dividing by zero (the sign of the zero
/// becomes the sign of the resulting infinity).
/// * All NaN values are considered equal, even though they may have different
/// [bits](https://doc.rust-lang.org/std/primitive.f64.html#method.to_bits), and therefore
/// different [sign](https://doc.rust-lang.org/std/primitive.f64.html#method.is_sign_positive),
/// signaling/quiet status, and NaN payload bits.
///
/// Therefore, `OrderedFloat` may be unsuitable for use as a key in interning and memoization
/// applications which require equal results from equal inputs, unless these cases make no
/// difference or are canonicalized before insertion.
///
/// # Representation
///
/// `OrderedFloat` has `#[repr(transparent)]` and permits any value, so it is sound to use
Expand Down Expand Up @@ -1152,13 +1166,18 @@ impl<T: FloatCore + Num> Num for OrderedFloat<T> {
/// ```
/// # use ordered_float::NotNan;
/// # use std::collections::HashSet;
///
/// let mut s: HashSet<NotNan<f32>> = HashSet::new();
/// let key = NotNan::new(1.0).unwrap();
/// s.insert(key);
/// assert!(s.contains(&key));
/// ```
///
/// `-0.0` and `+0.0` are still considered equal. This different sign may show up in printing,
/// or when dividing by zero (the sign of the zero becomes the sign of the resulting infinity).
/// Therefore, `NotNan` may be unsuitable for use as a key in interning and memoization
/// applications which require equal results from equal inputs, unless signed zeros make no
/// difference or are canonicalized before insertion.
///
/// Arithmetic on NotNan values will panic if it produces a NaN value:
///
/// ```should_panic
Expand Down
Loading