pub const EPSILON: f64 = 1e-14;
pub const DBL_EPSILON: f64 = 2.220446049250313e-16;
#[macro_export]
macro_rules! f64_eq {
($x:expr, $y:expr) => {
($x - $y).abs() < EPSILON
};
}
#[macro_export]
macro_rules! assert_f64_eq {
($x:expr, $y:expr) => {
assert!(($x - $y).abs() < EPSILON)
};
}
#[allow(unused)]
pub fn f64_eq(x: f64, y: f64) -> bool {
f64_near(x, y, EPSILON)
}
#[allow(unused)]
pub fn f64_near(x: f64, y: f64, eps: f64) -> bool {
(x - y).abs() <= eps
}
pub fn remainder(x: f64, y: f64) -> f64 {
::libm::remquo(x, y).0
}
pub fn clamp<T>(val: T, min: T, max: T) -> T
where
T: PartialOrd,
{
if val < min {
min
} else if val > max {
max
} else {
val
}
}
pub fn search_lower_by<F>(len: usize, f: F) -> usize
where
F: Fn(usize) -> bool,
{
let mut i = 0;
let mut j = len;
while i < j {
let h = i + (j - i) / 2;
if !f(h) {
i = h + 1;
} else {
j = h;
}
}
i
}