pub trait AlmostEqual {
type Float;
const DEFAULT_TOLERANCE: Self::Float;
const MACHINE_EPSILON: Self::Float;
// Required methods
fn almost_equals_with(self, rhs: Self, tol: Self::Float) -> bool;
fn almost_zero_with(self, tol: Self::Float) -> bool;
// Provided methods
fn almost_zero(self) -> bool
where Self: Sized { ... }
fn almost_equals(self, rhs: Self) -> bool
where Self: Sized { ... }
}
Expand description
A trait for comparing floating point numbers. Not broadly intended to be used by most code (instead, use the functions at the crate root), however it could be useful for generic code too.
Required Associated Constants§
sourceconst DEFAULT_TOLERANCE: Self::Float
const DEFAULT_TOLERANCE: Self::Float
The default tolerance value for this type. Typically equivalent to
T::EPSILON.sqrt()
, as we assume that around half of the precision bits
of any arbitrary computation have been rounded away.
sourceconst MACHINE_EPSILON: Self::Float
const MACHINE_EPSILON: Self::Float
The machine epsilon for this type. This generally should not be used as a tolerance value (it’s frequently too strict), however it can be useful when computing tolerances.
Required Associated Types§
Required Methods§
sourcefn almost_equals_with(self, rhs: Self, tol: Self::Float) -> bool
fn almost_equals_with(self, rhs: Self, tol: Self::Float) -> bool
Equivalent to almost::equal_with
.
const MY_TOLERANCE: f32 = almost::F32_TOLERANCE / 2.0;
assert!(a.almost_equals_with(b, MY_TOLERANCE));
sourcefn almost_zero_with(self, tol: Self::Float) -> bool
fn almost_zero_with(self, tol: Self::Float) -> bool
Equivalent to almost::zero_with
.
assert!(0.01.almost_zero_with(0.05));
Provided Methods§
sourcefn almost_zero(self) -> boolwhere
Self: Sized,
fn almost_zero(self) -> boolwhere
Self: Sized,
Equivalent to almost::zero
.
assert!(v.almost_zero());
sourcefn almost_equals(self, rhs: Self) -> boolwhere
Self: Sized,
fn almost_equals(self, rhs: Self) -> boolwhere
Self: Sized,
Equivalent to almost::equal
.
assert!(a.almost_equals(b));
Object Safety§
This trait is not object safe.