malachite_nz/integer/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::integer::Integer;
10use malachite_base::num::arithmetic::traits::UnsignedAbs;
11
12macro_rules! impl_float {
13    ($t: ident) => {
14        impl PartialEq<$t> for Integer {
15            /// Determines whether an [`Integer`] is equal to a primitive float.
16            ///
17            /// # Worst-case complexity
18            /// $T(n) = O(n)$
19            ///
20            /// $M(n) = O(1)$
21            ///
22            /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
23            ///
24            /// # Examples
25            /// See [here](super::partial_eq_primitive_float#partial_eq).
26            fn eq(&self, other: &$t) -> bool {
27                if self.sign {
28                    self.unsigned_abs() == *other
29                } else {
30                    self.unsigned_abs() == -other
31                }
32            }
33        }
34
35        impl PartialEq<Integer> for $t {
36            /// Determines whether a primitive float is equal to an [`Integer`].
37            ///
38            /// # Worst-case complexity
39            /// $T(n) = O(n)$
40            ///
41            /// $M(n) = O(1)$
42            ///
43            /// where $T$ is time, $M$ is additional memory, and $n$ is `other.significant_bits()`.
44            ///
45            /// # Examples
46            /// See [here](super::partial_eq_primitive_float#partial_eq).
47            #[inline]
48            fn eq(&self, other: &Integer) -> bool {
49                other == self
50            }
51        }
52    };
53}
54apply_to_primitive_floats!(impl_float);