|
2 | 2 | #![cfg_attr(test, deny(warnings))]
|
3 | 3 | #![deny(missing_docs)]
|
4 | 4 |
|
5 |
| -//! Wrappers for total order on Floats. |
| 5 | +//! Wrappers for total order on Floats. See the [`OrderedFloat`] and [`NotNan`] docs for details. |
6 | 6 |
|
7 | 7 | #[cfg(feature = "std")] extern crate std;
|
8 | 8 | #[cfg(feature = "std")] use std::error::Error;
|
@@ -34,10 +34,32 @@ const MAN_MASK: u64 = 0x000fffffffffffffu64;
|
34 | 34 | const CANONICAL_NAN_BITS: u64 = 0x7ff8000000000000u64;
|
35 | 35 | const CANONICAL_ZERO_BITS: u64 = 0x0u64;
|
36 | 36 |
|
37 |
| -/// A wrapper around Floats providing an implementation of Ord and Hash. |
| 37 | +/// A wrapper around floats providing implementations of `Eq`, `Ord`, and `Hash`. |
38 | 38 | ///
|
39 | 39 | /// NaN is sorted as *greater* than all other values and *equal*
|
40 | 40 | /// to itself, in contradiction with the IEEE standard.
|
| 41 | +/// |
| 42 | +/// ``` |
| 43 | +/// use ordered_float::OrderedFloat; |
| 44 | +/// use std::f32::NAN; |
| 45 | +/// |
| 46 | +/// let mut v = [OrderedFloat(NAN), OrderedFloat(2.0), OrderedFloat(1.0)]; |
| 47 | +/// v.sort(); |
| 48 | +/// assert_eq!(v, [OrderedFloat(1.0), OrderedFloat(2.0), OrderedFloat(NAN)]); |
| 49 | +/// ``` |
| 50 | +/// |
| 51 | +/// Because `OrderedFloat` implements `Ord` and `Eq`, it can be used as a key in a `HashSet`, |
| 52 | +/// `HashMap`, `BTreeMap`, or `BTreeSet` (unlike the primitive `f32` or `f64` types): |
| 53 | +/// |
| 54 | +/// ``` |
| 55 | +/// # use ordered_float::OrderedFloat; |
| 56 | +/// # use std::collections::HashSet; |
| 57 | +/// # use std::f32::NAN; |
| 58 | +/// |
| 59 | +/// let mut s: HashSet<OrderedFloat<f32>> = HashSet::new(); |
| 60 | +/// s.insert(OrderedFloat(NAN)); |
| 61 | +/// assert!(s.contains(&OrderedFloat(NAN))); |
| 62 | +/// ``` |
41 | 63 | #[derive(Debug, Default, Clone, Copy)]
|
42 | 64 | #[repr(transparent)]
|
43 | 65 | pub struct OrderedFloat<T>(pub T);
|
@@ -455,9 +477,44 @@ impl<T: Float + Num> Num for OrderedFloat<T> {
|
455 | 477 | }
|
456 | 478 | }
|
457 | 479 |
|
458 |
| -/// A wrapper around Floats providing an implementation of Ord and Hash. |
| 480 | +/// A wrapper around floats providing an implementation of `Eq`, `Ord` and `Hash`. |
459 | 481 | ///
|
460 | 482 | /// A NaN value cannot be stored in this type.
|
| 483 | +/// |
| 484 | +/// ``` |
| 485 | +/// use ordered_float::NotNan; |
| 486 | +/// |
| 487 | +/// let mut v = [ |
| 488 | +/// NotNan::new(2.0).unwrap(), |
| 489 | +/// NotNan::new(1.0).unwrap(), |
| 490 | +/// ]; |
| 491 | +/// v.sort(); |
| 492 | +/// assert_eq!(v, [1.0, 2.0]); |
| 493 | +/// ``` |
| 494 | +/// |
| 495 | +/// Because `NotNan` implements `Ord` and `Eq`, it can be used as a key in a `HashSet`, |
| 496 | +/// `HashMap`, `BTreeMap`, or `BTreeSet` (unlike the primitive `f32` or `f64` types): |
| 497 | +/// |
| 498 | +/// ``` |
| 499 | +/// # use ordered_float::NotNan; |
| 500 | +/// # use std::collections::HashSet; |
| 501 | +/// |
| 502 | +/// let mut s: HashSet<NotNan<f32>> = HashSet::new(); |
| 503 | +/// let key = NotNan::new(1.0).unwrap(); |
| 504 | +/// s.insert(key); |
| 505 | +/// assert!(s.contains(&key)); |
| 506 | +/// ``` |
| 507 | +/// |
| 508 | +/// Arithmetic on NotNan values will panic if it produces a NaN value: |
| 509 | +/// |
| 510 | +/// ```should_panic |
| 511 | +/// # use ordered_float::NotNan; |
| 512 | +/// let a = NotNan::new(std::f32::INFINITY).unwrap(); |
| 513 | +/// let b = NotNan::new(std::f32::NEG_INFINITY).unwrap(); |
| 514 | +/// |
| 515 | +/// // This will panic: |
| 516 | +/// let c = a + b; |
| 517 | +/// ``` |
461 | 518 | #[derive(PartialOrd, PartialEq, Debug, Default, Clone, Copy)]
|
462 | 519 | #[repr(transparent)]
|
463 | 520 | pub struct NotNan<T>(T);
|
|
0 commit comments