malachite_nz/gaussian_integer/comparison/eq_abs_primitive_float.rs
1// Copyright © 2026 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::gaussian_integer::GaussianInteger;
10use crate::integer::Integer;
11use malachite_base::num::arithmetic::traits::AbsSquared;
12use malachite_base::num::comparison::traits::{EqAbs, PartialOrdAbs};
13
14macro_rules! impl_float {
15 ($t: ident) => {
16 impl EqAbs<$t> for GaussianInteger {
17 /// Determines whether the absolute values of a [`GaussianInteger`] and a primitive
18 /// float are equal.
19 ///
20 /// No [`GaussianInteger`] is equal in absolute value to an infinity or NaN. If the
21 /// float is not an integer, its square is not an integer either (its odd mantissa
22 /// contributes an odd square), so it cannot equal the absolute value of any
23 /// [`GaussianInteger`].
24 ///
25 /// # Worst-case complexity
26 /// $T(n) = O(n \log n \log\log n)$
27 ///
28 /// $M(n) = O(n \log n)$
29 ///
30 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of
31 /// significant bits of the real and imaginary parts of `self`.
32 ///
33 /// # Examples
34 /// See [here](super::eq_abs_primitive_float#eq_abs).
35 fn eq_abs(&self, other: &$t) -> bool {
36 if self.imaginary == 0u32 {
37 self.real.eq_abs(other)
38 } else if self.real == 0u32 {
39 self.imaginary.eq_abs(other)
40 } else if !self.real.lt_abs(other) || !self.imaginary.lt_abs(other) {
41 false
42 } else if let Ok(y) = Integer::try_from(*other) {
43 self.abs_squared() == y.abs_squared()
44 } else {
45 false
46 }
47 }
48 }
49
50 impl EqAbs<GaussianInteger> for $t {
51 /// Determines whether the absolute values of a primitive float and a
52 /// [`GaussianInteger`] are equal.
53 ///
54 /// No infinity or NaN is equal in absolute value to a [`GaussianInteger`].
55 ///
56 /// # Worst-case complexity
57 /// $T(n) = O(n \log n \log\log n)$
58 ///
59 /// $M(n) = O(n \log n)$
60 ///
61 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of
62 /// significant bits of the real and imaginary parts of `other`.
63 ///
64 /// # Examples
65 /// See [here](super::eq_abs_primitive_float#eq_abs).
66 #[inline]
67 fn eq_abs(&self, other: &GaussianInteger) -> bool {
68 other.eq_abs(self)
69 }
70 }
71 };
72}
73apply_to_primitive_floats!(impl_float);