ApproxEq

Trait ApproxEq 

Source
pub trait ApproxEq<Other: ?Sized = Self, Epsilon = Self> {
    // Required methods
    fn approx_eq_eps(&self, other: &Other, rel_eps: &Epsilon) -> bool;
    fn relative_epsilon() -> Epsilon;

    // Provided method
    fn approx_eq(&self, other: &Other) -> bool { ... }
}
Expand description

Trait for testing approximate equality.

Floating-point types are only an approximation of real numbers due to their finite precision. The presence of rounding errors means that two floats may not compare equal even if their counterparts in ℝ would. Even such a simple expression as 0.1 + 0.2 == 0.3 will evaluate to false due to precision issues.

Approximate equality is a more robust way to compare floating-point values than strict equality. Two values are considered approximately equal if their absolute difference is less than some small value, “epsilon”. The choice of the epsilon value is not an exact science, and depends on how much error has accrued in the computation of the values.

Moreover, due to the nature of floating point, a naive comparison against a fixed value does not work well. Rather, the epsilon should be relative to the magnitude of the values being compared.

Required Methods§

Source

fn approx_eq_eps(&self, other: &Other, rel_eps: &Epsilon) -> bool

Returns whether self and other are approximately equal, using the relative epsilon rel_eps.

Source

fn relative_epsilon() -> Epsilon

Returns the default relative epsilon of type E.

Provided Methods§

Source

fn approx_eq(&self, other: &Other) -> bool

Returns whether self and other are approximately equal. Uses the epsilon returned by Self::relative_epsilon.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ApproxEq for f32

Source§

fn approx_eq_eps(&self, other: &Self, rel_eps: &Self) -> bool

Source§

fn relative_epsilon() -> Self

Source§

impl<E, T: Sized + ApproxEq<T, E>> ApproxEq<[T], E> for [T]

Source§

fn approx_eq_eps(&self, other: &Self, rel_eps: &E) -> bool

Source§

fn relative_epsilon() -> E

Source§

impl<E, T: Sized + ApproxEq<T, E>, const N: usize> ApproxEq<[T; N], E> for [T; N]

Source§

fn approx_eq_eps(&self, other: &Self, rel_eps: &E) -> bool

Source§

fn relative_epsilon() -> E

Source§

impl<E, T: ApproxEq<T, E>> ApproxEq<Option<T>, E> for Option<T>

Source§

fn approx_eq_eps(&self, other: &Self, rel_eps: &E) -> bool

Source§

fn relative_epsilon() -> E

Implementors§

Source§

impl ApproxEq for Angle

Source§

impl<Repr, E, M> ApproxEq<Matrix<Repr, M>, E> for Matrix<Repr, M>
where Repr: ApproxEq<Repr, E>,

Source§

impl<Sc: ApproxEq, Sp, const N: usize> ApproxEq<Point<[Sc; N], Sp>, Sc> for Point<[Sc; N], Sp>

Source§

impl<Sc: ApproxEq, Sp, const N: usize> ApproxEq<Vector<[Sc; N], Sp>, Sc> for Vector<[Sc; N], Sp>