pub trait FloatExt: Sized {
const EPS: Self;
fn eq(self, other: Self) -> bool {
self.almost_eq(other, Self::EPS)
}
fn almost_eq(self, other: Self, acceptable_difference: Self) -> bool;
fn not_eq(self, other: Self) -> bool;
fn lerp(value1: Self, value2: Self, amount: Self) -> Self;
}
macro_rules! impl_float_ext {
( $ty:ty ) => {
impl FloatExt for $ty {
const EPS: Self = <$ty>::EPSILON;
fn almost_eq(self, other: Self, acceptable_difference: Self) -> bool {
(self - other).abs() <= acceptable_difference
}
fn not_eq(self, other: Self) -> bool {
(self - other).abs() >= Self::EPS
}
fn lerp(value1: Self, value2: Self, amount: Self) -> Self {
(value1 * (1.0 - amount)) + (value2 * amount)
}
}
};
}
impl_float_ext!(f32);
impl_float_ext!(f64);