pub struct RelativeTolerance<RealType: RealScalar>(/* 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.
§Implementation
This type is a #[repr(transparent)] wrapper around NonNegativeRealScalar<T>.
All validation is performed by the underlying NonNegativeRealScalar, and errors
are reported as ErrorsNonNegativeRealScalar.
§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_raw_scalar_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_raw_scalar_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_raw_scalar_ref(), 5.0); // 1% of |-500| = 5Implementations§
Source§impl<RealType: RealScalar> RelativeTolerance<RealType>
impl<RealType: RealScalar> RelativeTolerance<RealType>
Sourcepub fn into_inner(self) -> NonNegativeRealScalar<RealType>
pub fn into_inner(self) -> NonNegativeRealScalar<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_raw_scalar_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_raw_scalar_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_raw_scalar_ref(), 10.0);
// 10% of |-50| = 5
let abs_tol_neg = rel_tol.absolute_tolerance(-50.0);
assert_eq!(*abs_tol_neg.as_raw_scalar_ref(), 5.0);
// 10% of 0 = 0
let abs_tol_zero = rel_tol.absolute_tolerance(0.0);
assert_eq!(*abs_tol_zero.as_raw_scalar_ref(), 0.0);Sourcepub fn new(value: NonNegativeRealScalar<RealType>) -> Self
pub fn new(value: NonNegativeRealScalar<RealType>) -> Self
Wraps a NonNegativeRealScalar<T> as a RelativeTolerance<T>.
This constructor allows explicit conversion from a validated non-negative scalar to a tolerance type, providing semantic clarity.
§Examples
use num_valid::{*, scalars::*};
use try_create::TryNew;
let non_neg = NonNegativeRealScalar::try_new(0.01_f64).unwrap(); // 1%
let tol = RelativeTolerance::new(non_neg);
assert_eq!(tol.as_raw_scalar_ref(), &0.01);Sourcepub fn try_new(
value: RealType,
) -> Result<Self, ErrorsNonNegativeRealScalar<RealType>>
pub fn try_new( value: RealType, ) -> Result<Self, ErrorsNonNegativeRealScalar<RealType>>
Attempts to create a RelativeTolerance from a value.
§Errors
Returns ErrorsNonNegativeRealScalar::NegativeValue if the input is negative.
§Panics (Debug Mode Only)
In debug builds, panics if the input is not finite (NaN or infinity).
Trait Implementations§
Source§impl<RealType: RealScalar> AsRef<NonNegativeRealScalar<RealType>> for RelativeTolerance<RealType>
impl<RealType: RealScalar> AsRef<NonNegativeRealScalar<RealType>> for RelativeTolerance<RealType>
Source§fn as_ref(&self) -> &NonNegativeRealScalar<RealType>
fn as_ref(&self) -> &NonNegativeRealScalar<RealType>
Source§impl<RealType: Clone + RealScalar> Clone for RelativeTolerance<RealType>
impl<RealType: Clone + RealScalar> Clone for RelativeTolerance<RealType>
Source§fn clone(&self) -> RelativeTolerance<RealType>
fn clone(&self) -> RelativeTolerance<RealType>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<RealType: Debug + RealScalar> Debug for RelativeTolerance<RealType>
impl<RealType: Debug + RealScalar> Debug for RelativeTolerance<RealType>
Source§impl<'de, RealType> Deserialize<'de> for RelativeTolerance<RealType>where
RealType: for<'a> Deserialize<'a> + RealScalar,
impl<'de, RealType> Deserialize<'de> for RelativeTolerance<RealType>where
RealType: for<'a> Deserialize<'a> + RealScalar,
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: RealScalar> Display for RelativeTolerance<RealType>where
NonNegativeRealScalar<RealType>: Display,
impl<RealType: RealScalar> Display for RelativeTolerance<RealType>where
NonNegativeRealScalar<RealType>: Display,
Source§impl<RealType: RealScalar> IntoInner for RelativeTolerance<RealType>
impl<RealType: RealScalar> IntoInner for RelativeTolerance<RealType>
Source§fn into_inner(self) -> Self::InnerType
fn into_inner(self) -> Self::InnerType
Consumes the struct and returns the inner value.
Source§type InnerType = NonNegativeRealScalar<RealType>
type InnerType = NonNegativeRealScalar<RealType>
Source§impl<RealType: RealScalar> LowerExp for RelativeTolerance<RealType>where
NonNegativeRealScalar<RealType>: LowerExp,
impl<RealType: RealScalar> LowerExp for RelativeTolerance<RealType>where
NonNegativeRealScalar<RealType>: LowerExp,
Source§impl<RealType: PartialEq + RealScalar> PartialEq for RelativeTolerance<RealType>
impl<RealType: PartialEq + RealScalar> PartialEq for RelativeTolerance<RealType>
Source§impl<RealType: PartialOrd + RealScalar> PartialOrd for RelativeTolerance<RealType>
impl<RealType: PartialOrd + RealScalar> PartialOrd for RelativeTolerance<RealType>
Source§impl<RealType: RealScalar> ScalarCore for RelativeTolerance<RealType>
impl<RealType: RealScalar> ScalarCore for RelativeTolerance<RealType>
Source§type RawScalarType = <RealType as ScalarCore>::RawScalarType
type RawScalarType = <RealType as ScalarCore>::RawScalarType
Source§fn as_raw_scalar_ref(&self) -> &Self::RawScalarType
fn as_raw_scalar_ref(&self) -> &Self::RawScalarType
Source§fn into_raw_scalar(self) -> Self::RawScalarType
fn into_raw_scalar(self) -> Self::RawScalarType
Source§impl<RealType> Serialize for RelativeTolerance<RealType>where
RealType: Serialize + RealScalar,
impl<RealType> Serialize for RelativeTolerance<RealType>where
RealType: Serialize + RealScalar,
impl<RealType: PartialEq + RealScalar> StructuralPartialEq for RelativeTolerance<RealType>
Auto Trait Implementations§
impl<RealType> Freeze for RelativeTolerance<RealType>where
NonNegativeRealScalar<RealType>: Freeze,
impl<RealType> RefUnwindSafe for RelativeTolerance<RealType>where
NonNegativeRealScalar<RealType>: RefUnwindSafe,
impl<RealType> Send for RelativeTolerance<RealType>where
NonNegativeRealScalar<RealType>: Send,
impl<RealType> Sync for RelativeTolerance<RealType>where
NonNegativeRealScalar<RealType>: Sync,
impl<RealType> Unpin for RelativeTolerance<RealType>where
NonNegativeRealScalar<RealType>: Unpin,
impl<RealType> UnsafeUnpin for RelativeTolerance<RealType>where
NonNegativeRealScalar<RealType>: UnsafeUnpin,
impl<RealType> UnwindSafe for RelativeTolerance<RealType>where
NonNegativeRealScalar<RealType>: 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
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more