pub struct NonNegF64(/* private fields */);Expand description
A non-negative, non-NaN f64 cost; +∞ is allowed and acts as the largest cost.
Raw f64 cannot implement Cost: NaN breaks Ord and negative values break the
non-negativity contract. This newtype checks both once, in NonNegF64::new, and keeps
the invariant closed under every operation:
- the sum of two values in
[0, +∞]is in[0, +∞](a finite overflow becomes+∞;+∞ + +∞ = +∞), so+never produces NaN; Cost::saturating_timeswith factor0returns zero instead of computing+∞ × 0 = NaN;-0.0is stored as+0.0, soEq,OrdandHashagree (bitwise hashing would otherwise tell the two zeros apart).
§Rounding
Float addition rounds and is not associative: the same path summed in a different order
can differ in the last bits. Searches stay terminating and panic-free, but “optimal” then
means optimal up to rounding, and ties may break differently than with exact arithmetic.
Prefer an integer or Decimal cost when exact results matter.
§Examples
use search_core::{Cost, NonNegF64};
let a = NonNegF64::new(1.5).unwrap();
let b = NonNegF64::new(2.25).unwrap();
assert_eq!((a + b).get(), 3.75);
assert!(NonNegF64::ZERO < a);
assert_eq!(NonNegF64::new(f64::NAN), None);
assert_eq!(NonNegF64::new(-1.0), None);Implementations§
Trait Implementations§
impl Copy for NonNegF64
Source§impl Cost for NonNegF64
impl Cost for NonNegF64
Source§fn saturating_add(self, other: Self) -> Self
fn saturating_add(self, other: Self) -> Self
Adds two costs, saturating at the largest value instead of overflowing. Read more
Source§fn saturating_times(self, factor: u32) -> Self
fn saturating_times(self, factor: u32) -> Self
Multiplies the cost by a small integer factor, saturating at the largest value. Read more
Source§impl CostExt for NonNegF64
Available on crate feature float only.
impl CostExt for NonNegF64
Available on crate feature
float only.Source§fn saturating_sub(self, other: Self) -> Self
fn saturating_sub(self, other: Self) -> Self
Subtracts other, clamping at zero; ∞ - ∞ is zero.
impl Eq for NonNegF64
Source§impl ExactArith for NonNegF64
Available on crate feature float only.
impl ExactArith for NonNegF64
Available on crate feature
float only.Source§impl ExactNumber for NonNegF64
Available on crate feature float only.
impl ExactNumber for NonNegF64
Available on crate feature
float only.Source§impl Ord for NonNegF64
impl Ord for NonNegF64
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
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the minimum of two values. Read more
Source§impl PartialOrd for NonNegF64
impl PartialOrd for NonNegF64
Auto Trait Implementations§
impl Forget for NonNegF64
impl Freeze for NonNegF64
impl Move for NonNegF64
impl RefUnwindSafe for NonNegF64
impl Send for NonNegF64
impl Sync for NonNegF64
impl Unpin for NonNegF64
impl UnsafeUnpin for NonNegF64
impl UnwindSafe for NonNegF64
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