RelativeTolerance

Struct RelativeTolerance 

Source
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% tolerance
  • 0.001 = 0.1% tolerance
  • 1e-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| = 5

Implementations§

Source§

impl<RealType> RelativeTolerance<RealType>

Source

pub fn into_inner(self) -> RealType

Consumes the struct and returns the inner value.

Source§

impl<RealType: RealScalar> RelativeTolerance<RealType>

Source

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);
Source

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);
Source

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>

Source§

fn as_ref(&self) -> &RealType

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<RealType: Clone> Clone for RelativeTolerance<RealType>

Source§

fn clone(&self) -> RelativeTolerance<RealType>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<RealType: Debug> Debug for RelativeTolerance<RealType>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<RealType> Display for RelativeTolerance<RealType>
where RealType: Display,

Source§

fn fmt(&self, __derive_more_f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<RealType> IntoInner for RelativeTolerance<RealType>

Source§

fn into_inner(self) -> Self::InnerType

Consumes the struct and returns the inner value.

Source§

type InnerType = RealType

The type of the inner value that will be extracted.
Source§

impl<RealType> LowerExp for RelativeTolerance<RealType>
where RealType: LowerExp,

Source§

fn fmt(&self, __derive_more_f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<RealType: PartialEq> PartialEq for RelativeTolerance<RealType>

Source§

fn eq(&self, other: &RelativeTolerance<RealType>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<RealType: PartialOrd> PartialOrd for RelativeTolerance<RealType>

Source§

fn partial_cmp(&self, other: &RelativeTolerance<RealType>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<RealType> Serialize for RelativeTolerance<RealType>
where RealType: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<RealType: RealScalar> TryNew for RelativeTolerance<RealType>

Source§

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>

The error type that can be returned by the try_new method.
Source§

impl<RealType> StructuralPartialEq for RelativeTolerance<RealType>

Auto Trait Implementations§

§

impl<RealType> Freeze for RelativeTolerance<RealType>
where RealType: Freeze,

§

impl<RealType> RefUnwindSafe for RelativeTolerance<RealType>
where RealType: RefUnwindSafe,

§

impl<RealType> Send for RelativeTolerance<RealType>
where RealType: Send,

§

impl<RealType> Sync for RelativeTolerance<RealType>
where RealType: Sync,

§

impl<RealType> Unpin for RelativeTolerance<RealType>
where RealType: Unpin,

§

impl<RealType> UnwindSafe for RelativeTolerance<RealType>
where RealType: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,