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    /// Determines whether the absolute value of a [`Float`] is equal to the absolute value of a
20    /// [`Rational`].
21    ///
22    /// $\infty$, $-\infty$, and NaN are not equal to any [`Rational`]. Both the [`Float`] zero and
23    /// the [`Float`] negative zero are equal to the [`Rational`] zero.
24    ///
25    /// # Worst-case complexity
26    /// $T(n) = O(n)$
27    ///
28    /// $M(n) = O(1)$
29    ///
30    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
31    /// other.significant_bits())`.
32    ///
33    /// # Examples
34    /// ```
35    /// use malachite_base::num::basic::traits::OneHalf;
36    /// use malachite_base::num::comparison::traits::EqAbs;
37    /// use malachite_float::Float;
38    /// use malachite_q::Rational;
39    ///
40    /// assert!(Float::from(123).eq_abs(&Rational::from(123)));
41    /// assert!(Float::from(-123).eq_abs(&Rational::from(123)));
42    /// assert!(Float::ONE_HALF.eq_abs(&Rational::ONE_HALF));
43    /// assert!(Float::from(1.0f64 / 3.0).ne_abs(&Rational::from_unsigneds(1u8, 3)));
44    /// ```
45    #[inline]
46    fn eq_abs(&self, other: &Rational) -> bool {
47        match self {
48            float_either_zero!() => *other == 0u32,
49            Self(Finite {
50                exponent,
51                significand,
52                ..
53            }) => {
54                *other != 0
55                    && if let Some(log_d) = other.denominator_ref().checked_log_base_2() {
56                        let n = other.numerator_ref();
57                        i64::from(*exponent)
58                            == i64::exact_from(n.significant_bits()) - i64::exact_from(log_d)
59                            && significand.cmp_normalized(n) == Equal
60                    } else {
61                        false
62                    }
63            }
64            _ => false,
65        }
66    }
67}
68
69impl EqAbs<Float> for Rational {
70    /// Determines whether the absolute value of a [`Rational`] is equal to the absolute value of a
71    /// [`Float`].
72    ///
73    /// No [`Rational`] is equal to $\infty$, $-\infty$, or NaN. The [`Rational`] zero is equal to
74    /// both the [`Float`] zero and the [`Float`] negative zero.
75    ///
76    /// # Worst-case complexity
77    /// $T(n) = O(n)$
78    ///
79    /// $M(n) = O(1)$
80    ///
81    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
82    /// other.significant_bits())`.
83    ///
84    /// # Examples
85    /// ```
86    /// use malachite_base::num::basic::traits::OneHalf;
87    /// use malachite_base::num::comparison::traits::EqAbs;
88    /// use malachite_float::Float;
89    /// use malachite_q::Rational;
90    ///
91    /// assert!(Rational::from(123).eq_abs(&Float::from(123)));
92    /// assert!(Rational::from(-123).eq_abs(&Float::from(123)));
93    /// assert!(Rational::ONE_HALF.eq_abs(&Float::ONE_HALF));
94    /// assert!(Rational::from_unsigneds(1u8, 3).ne_abs(&Float::from(1.0f64 / 3.0)));
95    /// ```
96    #[inline]
97    fn eq_abs(&self, other: &Float) -> bool {
98        other.eq_abs(self)
99    }
100}