pub struct AbsoluteTolerance<RealType>(/* private fields */);Expand description
Type-safe wrapper for absolute tolerance values.
AbsoluteTolerance<T> represents a non-negative (≥ 0) tolerance value used for
absolute error comparisons. It ensures that the tolerance is always valid by
rejecting negative values at construction time.
§Mathematical Definition
An absolute tolerance ε is used in comparisons like:
|a - b| ≤ εSince ε represents a distance or error bound, it must be non-negative.
§Type Parameters
RealType: The underlying real scalar type (e.g.,f64,RealNative64StrictFinite)
§Examples
§Basic Usage
use num_valid::scalars::AbsoluteTolerance;
use try_create::TryNew;
// Create from f64
let tol = AbsoluteTolerance::try_new(1e-10_f64).unwrap();
assert_eq!(*tol.as_ref(), 1e-10);
// Use convenience constructors
let zero = AbsoluteTolerance::<f64>::zero();
let eps = AbsoluteTolerance::<f64>::epsilon();§In Numerical Comparisons
use num_valid::scalars::AbsoluteTolerance;
use num_valid::RealScalar;
use try_create::TryNew;
fn approximately_equal<T: RealScalar + Clone>(a: T, b: T, tol: &AbsoluteTolerance<T>) -> bool {
let diff = (a - b).abs();
&diff <= tol.as_ref()
}
let tol = AbsoluteTolerance::try_new(1e-10_f64).unwrap();
assert!(approximately_equal(1.0, 1.0 + 1e-11, &tol));
assert!(!approximately_equal(1.0, 1.0 + 1e-9, &tol));§Zero-Cost Abstraction
This type uses #[repr(transparent)] and has zero runtime overhead beyond
the initial validation at construction time.
Implementations§
Source§impl<RealType> AbsoluteTolerance<RealType>
impl<RealType> AbsoluteTolerance<RealType>
Sourcepub fn into_inner(self) -> RealType
pub fn into_inner(self) -> RealType
Consumes the struct and returns the inner value.
Source§impl<RealType: RealScalar> AbsoluteTolerance<RealType>
impl<RealType: RealScalar> AbsoluteTolerance<RealType>
Sourcepub fn zero() -> Self
pub fn zero() -> Self
Creates an absolute tolerance of zero.
A zero tolerance represents an exact comparison (no error allowed).
§Examples
use num_valid::scalars::AbsoluteTolerance;
let zero_tol = AbsoluteTolerance::<f64>::zero();
assert_eq!(*zero_tol.as_ref(), 0.0);Sourcepub fn epsilon() -> Self
pub fn epsilon() -> Self
Creates an absolute tolerance equal to machine epsilon.
Machine epsilon is the smallest value such that 1.0 + epsilon != 1.0.
This is often a good default tolerance for floating-point comparisons.
§Examples
use num_valid::scalars::AbsoluteTolerance;
let eps_tol = AbsoluteTolerance::<f64>::epsilon();
assert_eq!(*eps_tol.as_ref(), f64::EPSILON);Trait Implementations§
Source§impl<RealType> AsRef<RealType> for AbsoluteTolerance<RealType>
impl<RealType> AsRef<RealType> for AbsoluteTolerance<RealType>
Source§impl<RealType: Clone> Clone for AbsoluteTolerance<RealType>
impl<RealType: Clone> Clone for AbsoluteTolerance<RealType>
Source§fn clone(&self) -> AbsoluteTolerance<RealType>
fn clone(&self) -> AbsoluteTolerance<RealType>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<RealType: Debug> Debug for AbsoluteTolerance<RealType>
impl<RealType: Debug> Debug for AbsoluteTolerance<RealType>
Source§impl<'de, RealType> Deserialize<'de> for AbsoluteTolerance<RealType>where
RealType: Deserialize<'de>,
impl<'de, RealType> Deserialize<'de> for AbsoluteTolerance<RealType>where
RealType: Deserialize<'de>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<RealType> Display for AbsoluteTolerance<RealType>where
RealType: Display,
impl<RealType> Display for AbsoluteTolerance<RealType>where
RealType: Display,
Source§impl<RealType> IntoInner for AbsoluteTolerance<RealType>
impl<RealType> IntoInner for AbsoluteTolerance<RealType>
Source§impl<RealType> LowerExp for AbsoluteTolerance<RealType>where
RealType: LowerExp,
impl<RealType> LowerExp for AbsoluteTolerance<RealType>where
RealType: LowerExp,
Source§impl<RealType: PartialEq> PartialEq for AbsoluteTolerance<RealType>
impl<RealType: PartialEq> PartialEq for AbsoluteTolerance<RealType>
Source§impl<RealType: PartialOrd> PartialOrd for AbsoluteTolerance<RealType>
impl<RealType: PartialOrd> PartialOrd for AbsoluteTolerance<RealType>
Source§impl<RealType> Serialize for AbsoluteTolerance<RealType>where
RealType: Serialize,
impl<RealType> Serialize for AbsoluteTolerance<RealType>where
RealType: Serialize,
Source§impl<RealType: RealScalar> TryNew for AbsoluteTolerance<RealType>
impl<RealType: RealScalar> TryNew for AbsoluteTolerance<RealType>
Source§fn try_new(value: RealType) -> Result<Self, Self::Error>
fn try_new(value: RealType) -> Result<Self, Self::Error>
Attempts to create an AbsoluteTolerance from a value.
§Errors
Returns ErrorsTolerance::NegativeValue if the input is negative.
§Panics (Debug Mode Only)
In debug builds, panics if the input is not finite (NaN or infinity).
§Examples
use num_valid::scalars::{AbsoluteTolerance, ErrorsTolerance};
use try_create::TryNew;
// Valid tolerances
assert!(AbsoluteTolerance::try_new(1e-10_f64).is_ok());
assert!(AbsoluteTolerance::try_new(0.0_f64).is_ok());
// Invalid (negative)
assert!(matches!(
AbsoluteTolerance::try_new(-1e-10_f64),
Err(ErrorsTolerance::NegativeValue { .. })
));Source§type Error = ErrorsTolerance<RealType>
type Error = ErrorsTolerance<RealType>
try_new method.