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