AbsoluteTolerance

Struct AbsoluteTolerance 

Source
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

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

Source

pub fn into_inner(self) -> RealType

Consumes the struct and returns the inner value.

Source§

impl<RealType: RealScalar> AbsoluteTolerance<RealType>

Source

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

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>

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 AbsoluteTolerance<RealType>

Source§

fn clone(&self) -> AbsoluteTolerance<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 AbsoluteTolerance<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 AbsoluteTolerance<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 AbsoluteTolerance<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 AbsoluteTolerance<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 AbsoluteTolerance<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 AbsoluteTolerance<RealType>

Source§

fn eq(&self, other: &AbsoluteTolerance<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 AbsoluteTolerance<RealType>

Source§

fn partial_cmp(&self, other: &AbsoluteTolerance<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 AbsoluteTolerance<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 AbsoluteTolerance<RealType>

Source§

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>

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

impl<RealType> StructuralPartialEq for AbsoluteTolerance<RealType>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<RealType> UnwindSafe for AbsoluteTolerance<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>,