use crate::natural::Natural;
use core::cmp::Ordering::{self, *};
use malachite_base::num::comparison::traits::PartialOrdAbs;
use malachite_base::num::conversion::traits::{ExactFrom, IntegerMantissaAndExponent};
use malachite_base::num::logic::traits::SignificantBits;
macro_rules! impl_float {
($t: ident) => {
impl PartialOrdAbs<$t> for Natural {
fn partial_cmp_abs(&self, other: &$t) -> Option<Ordering> {
if other.is_nan() {
None
} else if !other.is_finite() {
Some(Less)
} else if *other == 0.0 {
self.partial_cmp_abs(&0u32)
} else if *self == 0u32 {
Some(Less)
} else {
let (m, e) = other.integer_mantissa_and_exponent();
let log_cmp = i64::exact_from(self.significant_bits())
.cmp(&(i64::exact_from(m.significant_bits()) + e));
Some(if log_cmp != Equal {
log_cmp
} else {
self.cmp_normalized(&Natural::from(m))
})
}
}
}
impl PartialOrdAbs<Natural> for $t {
#[inline]
fn partial_cmp_abs(&self, other: &Natural) -> Option<Ordering> {
other.partial_cmp_abs(self).map(Ordering::reverse)
}
}
};
}
apply_to_primitive_floats!(impl_float);