pub struct RelativeTolerance<RealType>(/* private fields */);Expand description
Type-safe wrapper for relative tolerance values.
RelativeTolerance<T> represents a non-negative (≥ 0) tolerance value used for
relative (proportional) error comparisons. It can be converted to an absolute
tolerance based on a reference value.
§Mathematical Definition
A relative tolerance ε_rel is used in comparisons like:
|a - b| ≤ ε_rel × |reference|Common relative tolerances:
0.01= 1% tolerance0.001= 0.1% tolerance1e-6= one part per million
§Examples
§Basic Usage
use num_valid::scalars::RelativeTolerance;
use try_create::TryNew;
let one_percent = RelativeTolerance::try_new(0.01_f64).unwrap();
assert_eq!(*one_percent.as_ref(), 0.01);§Converting to Absolute Tolerance
use num_valid::scalars::RelativeTolerance;
use try_create::TryNew;
let rel_tol = RelativeTolerance::try_new(0.01_f64).unwrap(); // 1%
// Convert based on reference value
let abs_tol = rel_tol.absolute_tolerance(1000.0);
assert_eq!(*abs_tol.as_ref(), 10.0); // 1% of 1000 = 10
// Works with negative references (uses absolute value)
let abs_tol_neg = rel_tol.absolute_tolerance(-500.0);
assert_eq!(*abs_tol_neg.as_ref(), 5.0); // 1% of |-500| = 5Implementations§
Source§impl<RealType> RelativeTolerance<RealType>
impl<RealType> RelativeTolerance<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> RelativeTolerance<RealType>
impl<RealType: RealScalar> RelativeTolerance<RealType>
Sourcepub fn zero() -> Self
pub fn zero() -> Self
Creates a relative tolerance of zero.
A zero relative tolerance represents an exact comparison.
§Examples
use num_valid::scalars::RelativeTolerance;
let zero_tol = RelativeTolerance::<f64>::zero();
assert_eq!(*zero_tol.as_ref(), 0.0);Sourcepub fn epsilon() -> Self
pub fn epsilon() -> Self
Creates a relative tolerance equal to machine epsilon.
§Examples
use num_valid::scalars::RelativeTolerance;
let eps_tol = RelativeTolerance::<f64>::epsilon();
assert_eq!(*eps_tol.as_ref(), f64::EPSILON);Sourcepub fn absolute_tolerance(
&self,
reference: RealType,
) -> AbsoluteTolerance<RealType>
pub fn absolute_tolerance( &self, reference: RealType, ) -> AbsoluteTolerance<RealType>
Converts this relative tolerance to an absolute tolerance based on a reference value.
The absolute tolerance is computed as:
absolute_tolerance = relative_tolerance × |reference|§Parameters
reference: The reference value to scale by. The absolute value is used.
§Examples
use num_valid::scalars::RelativeTolerance;
use try_create::TryNew;
let rel_tol = RelativeTolerance::try_new(0.1_f64).unwrap(); // 10%
// 10% of 100 = 10
let abs_tol = rel_tol.absolute_tolerance(100.0);
assert_eq!(*abs_tol.as_ref(), 10.0);
// 10% of |-50| = 5
let abs_tol_neg = rel_tol.absolute_tolerance(-50.0);
assert_eq!(*abs_tol_neg.as_ref(), 5.0);
// 10% of 0 = 0
let abs_tol_zero = rel_tol.absolute_tolerance(0.0);
assert_eq!(*abs_tol_zero.as_ref(), 0.0);Trait Implementations§
Source§impl<RealType> AsRef<RealType> for RelativeTolerance<RealType>
impl<RealType> AsRef<RealType> for RelativeTolerance<RealType>
Source§impl<RealType: Clone> Clone for RelativeTolerance<RealType>
impl<RealType: Clone> Clone for RelativeTolerance<RealType>
Source§fn clone(&self) -> RelativeTolerance<RealType>
fn clone(&self) -> RelativeTolerance<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 RelativeTolerance<RealType>
impl<RealType: Debug> Debug for RelativeTolerance<RealType>
Source§impl<'de, RealType> Deserialize<'de> for RelativeTolerance<RealType>where
RealType: Deserialize<'de>,
impl<'de, RealType> Deserialize<'de> for RelativeTolerance<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 RelativeTolerance<RealType>where
RealType: Display,
impl<RealType> Display for RelativeTolerance<RealType>where
RealType: Display,
Source§impl<RealType> IntoInner for RelativeTolerance<RealType>
impl<RealType> IntoInner for RelativeTolerance<RealType>
Source§impl<RealType> LowerExp for RelativeTolerance<RealType>where
RealType: LowerExp,
impl<RealType> LowerExp for RelativeTolerance<RealType>where
RealType: LowerExp,
Source§impl<RealType: PartialEq> PartialEq for RelativeTolerance<RealType>
impl<RealType: PartialEq> PartialEq for RelativeTolerance<RealType>
Source§impl<RealType: PartialOrd> PartialOrd for RelativeTolerance<RealType>
impl<RealType: PartialOrd> PartialOrd for RelativeTolerance<RealType>
Source§impl<RealType> Serialize for RelativeTolerance<RealType>where
RealType: Serialize,
impl<RealType> Serialize for RelativeTolerance<RealType>where
RealType: Serialize,
Source§impl<RealType: RealScalar> TryNew for RelativeTolerance<RealType>
impl<RealType: RealScalar> TryNew for RelativeTolerance<RealType>
Source§fn try_new(value: RealType) -> Result<Self, Self::Error>
fn try_new(value: RealType) -> Result<Self, Self::Error>
Attempts to create a RelativeTolerance 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).
Source§type Error = ErrorsTolerance<RealType>
type Error = ErrorsTolerance<RealType>
try_new method.