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§
Sourcefn approx_eq_eps(&self, other: &Other, rel_eps: &Epsilon) -> bool
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.
Sourcefn relative_epsilon() -> Epsilon
fn relative_epsilon() -> Epsilon
Returns the default relative epsilon of type E.
Provided Methods§
Sourcefn approx_eq(&self, other: &Other) -> bool
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.