malachite_float/comparison/
eq_abs_rational.rs1use crate::Float;
10use crate::InnerFloat::{Finite, Zero};
11use core::cmp::Ordering::*;
12use malachite_base::num::arithmetic::traits::CheckedLogBase2;
13use malachite_base::num::comparison::traits::EqAbs;
14use malachite_base::num::conversion::traits::ExactFrom;
15use malachite_base::num::logic::traits::SignificantBits;
16use malachite_q::Rational;
17
18impl EqAbs<Rational> for Float {
19 #[inline]
20 fn eq_abs(&self, other: &Rational) -> bool {
21 match self {
22 float_either_zero!() => *other == 0u32,
23 Self(Finite {
24 exponent,
25 significand,
26 ..
27 }) => {
28 *other != 0
29 && if let Some(log_d) = other.denominator_ref().checked_log_base_2() {
30 let n = other.numerator_ref();
31 i64::from(*exponent)
32 == i64::exact_from(n.significant_bits()) - i64::exact_from(log_d)
33 && significand.cmp_normalized(n) == Equal
34 } else {
35 false
36 }
37 }
38 _ => false,
39 }
40 }
41}
42
43impl EqAbs<Float> for Rational {
44 #[inline]
45 fn eq_abs(&self, other: &Float) -> bool {
46 other.eq_abs(self)
47 }
48}