pub struct Finite<T>(/* private fields */);Expand description
Proof that a float is finite (neither NaN nor ±∞).
Spent by a total order: Finite<T> implements Ord/Eq, which bare
f32/f64 can’t (NaN breaks reflexivity and trichotomy). With NaN gone,
partial_cmp is always Some, so sorting / min/max / BTreeMap keys are
total. repr(transparent); no new_ct (floats are outside the CT model).
use const_num_traits::Finite;
let a = Finite::<f64>::new(1.0).unwrap();
let b = Finite::<f64>::new(2.0).unwrap();
assert!(a < b);
assert_eq!(a.max(b).get(), 2.0);
assert!(Finite::<f64>::new(f64::NAN).is_none());
assert!(Finite::<f64>::new(f64::INFINITY).is_none());Implementations§
Source§impl Finite<f32>
impl Finite<f32>
Source§impl Finite<f64>
impl Finite<f64>
Sourcepub const fn new(value: f64) -> Option<Self>
pub const fn new(value: f64) -> Option<Self>
Some iff value is finite (not NaN, not ±∞).
const fn: finiteness is a bit test — the exponent field is
not all-ones — which sidesteps the non-const is_finite.
Examples found in repository?
examples/typestates.rs (line 118)
107fn finite_floats() {
108 let raw = [2.0f64, f64::NAN, 1.0, f64::INFINITY, 0.5];
109
110 // keep only the finite values; each carries its proof
111 let mut xs: Vec<Finite<f64>> = raw.iter().copied().filter_map(Finite::<f64>::new).collect();
112 xs.sort(); // total Ord — no `partial_cmp().unwrap()`
113 let sorted: Vec<f64> = xs.iter().map(|f| f.get()).collect();
114 assert_eq!(sorted, [0.5, 1.0, 2.0]);
115
116 // finite floats as map keys
117 let mut table: BTreeMap<Finite<f64>, &str> = BTreeMap::new();
118 table.insert(Finite::<f64>::new(1.0).unwrap(), "one");
119 table.insert(Finite::<f64>::new(2.0).unwrap(), "two");
120 assert_eq!(table.get(&Finite::<f64>::new(1.0).unwrap()), Some(&"one"));
121}Sourcepub const unsafe fn new_unchecked(value: f64) -> Self
pub const unsafe fn new_unchecked(value: f64) -> Self
§Safety
value must be finite.
Sourcepub const fn get(self) -> f64
pub const fn get(self) -> f64
The proven-finite value.
Examples found in repository?
examples/typestates.rs (line 113)
107fn finite_floats() {
108 let raw = [2.0f64, f64::NAN, 1.0, f64::INFINITY, 0.5];
109
110 // keep only the finite values; each carries its proof
111 let mut xs: Vec<Finite<f64>> = raw.iter().copied().filter_map(Finite::<f64>::new).collect();
112 xs.sort(); // total Ord — no `partial_cmp().unwrap()`
113 let sorted: Vec<f64> = xs.iter().map(|f| f.get()).collect();
114 assert_eq!(sorted, [0.5, 1.0, 2.0]);
115
116 // finite floats as map keys
117 let mut table: BTreeMap<Finite<f64>, &str> = BTreeMap::new();
118 table.insert(Finite::<f64>::new(1.0).unwrap(), "one");
119 table.insert(Finite::<f64>::new(2.0).unwrap(), "two");
120 assert_eq!(table.get(&Finite::<f64>::new(1.0).unwrap()), Some(&"one"));
121}Trait Implementations§
impl<T: Copy> Copy for Finite<T>
impl Eq for Finite<f32>
impl Eq for Finite<f64>
Source§impl Ord for Finite<f32>
impl Ord for Finite<f32>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl Ord for Finite<f64>
impl Ord for Finite<f64>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl PartialOrd for Finite<f32>
impl PartialOrd for Finite<f32>
Source§impl PartialOrd for Finite<f64>
impl PartialOrd for Finite<f64>
Source§impl TryFrom<f32> for Finite<f32>
Checked construction by value; mirrors Finite::new. The natural
boundary for rejecting NaN/±∞ from external float input with ?.
impl TryFrom<f32> for Finite<f32>
Checked construction by value; mirrors Finite::new. The natural
boundary for rejecting NaN/±∞ from external float input with ?.
Source§type Error = TypestateError
type Error = TypestateError
The type returned in the event of a conversion error.
Auto Trait Implementations§
impl<T> Freeze for Finite<T>where
T: Freeze,
impl<T> RefUnwindSafe for Finite<T>where
T: RefUnwindSafe,
impl<T> Send for Finite<T>where
T: Send,
impl<T> Sync for Finite<T>where
T: Sync,
impl<T> Unpin for Finite<T>where
T: Unpin,
impl<T> UnsafeUnpin for Finite<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Finite<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more