malachite_q/comparison/
partial_eq_primitive_float.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::Rational;
10use malachite_base::num::arithmetic::traits::{FloorLogBase2, IsPowerOf2};
11use malachite_base::num::conversion::traits::ExactFrom;
12
13macro_rules! impl_float {
14    ($t: ident) => {
15        impl PartialEq<$t> for Rational {
16            /// Determines whether a [`Rational`] is equal to a primitive float.
17            ///
18            /// # Worst-case complexity
19            /// $T(n) = O(n)$
20            ///
21            /// $M(m) = O(m)$
22            ///
23            /// where $T$ is time, $M$ is additional memory, $n$ is `max(self.significant_bits(),
24            /// other.sci_exponent().abs())`, and $m$ is `other.sci_exponent().abs()`.
25            ///
26            /// # Examples
27            /// See [here](super::partial_eq_primitive_float#partial_eq).
28            #[allow(clippy::cmp_owned)]
29            fn eq(&self, other: &$t) -> bool {
30                if !other.is_finite() {
31                    false
32                } else if *other == 0.0 {
33                    *self == 0u32
34                } else {
35                    *self != 0u32
36                        && self.sign == (*other > 0.0)
37                        && self.denominator.is_power_of_2()
38                        && self.floor_log_base_2_abs() == other.abs().floor_log_base_2()
39                        && *self == Rational::exact_from(*other)
40                }
41            }
42        }
43
44        impl PartialEq<Rational> for $t {
45            /// Determines whether a primitive float is equal to a [`Rational`].
46            ///
47            /// # Worst-case complexity
48            /// $T(n) = O(n)$
49            ///
50            /// $M(m) = O(m)$
51            ///
52            /// where $T$ is time, $M$ is additional memory, $n$ is `max(self.sci_exponent().abs(),
53            /// other.significant_bits())`, and $m$ is `self.sci_exponent().abs()`.
54            ///
55            /// # Examples
56            /// See [here](super::partial_eq_primitive_float#partial_eq).
57            #[inline]
58            fn eq(&self, other: &Rational) -> bool {
59                other == self
60            }
61        }
62    };
63}
64apply_to_primitive_floats!(impl_float);