pub trait ApproxEq {
type BaseType: BaseFloat;
// Required method
fn is_close_to(&self, rhs: &Self, max_diff: Self::BaseType) -> bool;
// Provided method
fn is_approx_eq(&self, rhs: &Self) -> bool { ... }
}
Expand description
Trait for comparing types that are derived from float numbers.
§Note
Comparing float numbers is tricky. This trait is mainly for convenience. See this article for the details of comparing float numbers.
Required Associated Types§
Required Methods§
Sourcefn is_close_to(&self, rhs: &Self, max_diff: Self::BaseType) -> bool
fn is_close_to(&self, rhs: &Self, max_diff: Self::BaseType) -> bool
Returns true
if the difference between x
and y
is less than
max_diff
.
§Note
The meanings of “difference” and “less than” are up to the implementations.
§Example
use glm::*;
let v1 = vec2(1000., 2000.); let v2 = vec2(1010., 1080.); assert!(v1.is_close_to(&v2, 50.)); assert!(!v1.is_close_to(&v2, 10.));
Provided Methods§
Sourcefn is_approx_eq(&self, rhs: &Self) -> bool
fn is_approx_eq(&self, rhs: &Self) -> bool
Returns true
if the difference between x
and y
is less than
machine epsilon.
§Example
use glm::*;
let f = 0.1_f32;
let mut sum = 0f32;
for i in 0..10 {
sum += f;
}
assert_eq!(1.0_f32 == sum, false);
assert_eq!(1f32.is_approx_eq(&sum), true);
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.