use xingo_sparse::Real;
#[inline]
fn scale<T: Real>(a: T, b: T) -> T {
T::ONE.max(a.abs()).max(b.abs())
}
#[inline]
pub(crate) fn lt<T: Real>(a: T, b: T) -> bool {
debug_assert!(!is_nan(a) && !is_nan(b), "lt called with NaN");
if a.is_finite() && b.is_finite() {
b - a > T::ZERO * scale(a, b)
} else {
a < b
}
}
#[inline]
pub(crate) fn le_exact<T: Real>(a: T, b: T) -> bool {
debug_assert!(!is_nan(a) && !is_nan(b), "le_exact called with NaN");
a <= b
}
#[inline]
pub(crate) fn is_exact_zero<T: Real>(x: T) -> bool {
x == T::ZERO
}
#[inline]
#[allow(clippy::eq_op)] pub(crate) fn is_nan<T: Real>(x: T) -> bool {
x != x
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn comparisons_match_their_definitions() {
assert!(lt(1.0, 2.0) && !lt(2.0, 2.0) && !lt(3.0, 2.0));
assert!(lt(f64::NEG_INFINITY, 0.0) && !lt(f64::INFINITY, 1e308));
assert!(le_exact(2.0, 2.0) && !le_exact(2.0 + f64::EPSILON * 2.0, 2.0));
assert!(is_exact_zero(-0.0) && !is_exact_zero(f64::MIN_POSITIVE));
assert!(is_nan(f64::NAN) && !is_nan(f64::INFINITY));
}
}