malachite_float/comparison/
eq_abs_rational.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use 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}