malachite_float/float/comparison/eq_abs_gaussian_rational.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::Float;
10use malachite_base::num::arithmetic::traits::AbsSquared;
11use malachite_base::num::comparison::traits::{EqAbs, PartialOrdAbs};
12use malachite_base::num::conversion::traits::ExactFrom;
13use malachite_q::Rational;
14use malachite_q::gaussian_rational::GaussianRational;
15
16impl EqAbs<GaussianRational> for Float {
17 /// Determines whether the absolute values of a [`Float`] and a [`GaussianRational`] are equal.
18 ///
19 /// The absolute value of a complex number is its distance from the origin, so two values are
20 /// equal in absolute value exactly when their squared absolute values are equal. Equality is
21 /// impossible unless the float exceeds both components in absolute value, so the squared
22 /// absolute values are only computed in that case. $\infty$, $-\infty$, and NaN are not equal
23 /// in absolute value to any [`GaussianRational`].
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 significant
31 /// bits of `self` and of the real and imaginary parts of `other`.
32 ///
33 /// # Examples
34 /// ```
35 /// use malachite_base::num::comparison::traits::EqAbs;
36 /// use malachite_float::Float;
37 /// use malachite_q::gaussian_rational::GaussianRational;
38 /// use std::str::FromStr;
39 ///
40 /// // |3/2+2i| = 5/2
41 /// let y = GaussianRational::from_str("3/2+2i").unwrap();
42 /// assert!(Float::from(2.5).eq_abs(&y));
43 /// assert_eq!(Float::from(2).eq_abs(&y), false);
44 /// ```
45 fn eq_abs(&self, other: &GaussianRational) -> bool {
46 if other.imaginary == 0u32 {
47 self.eq_abs(&other.real)
48 } else if other.real == 0u32 {
49 self.eq_abs(&other.imaginary)
50 } else if self.is_finite() {
51 self.gt_abs(&other.real)
52 && self.gt_abs(&other.imaginary)
53 && Rational::exact_from(self).abs_squared() == other.abs_squared()
54 } else {
55 false
56 }
57 }
58}
59
60impl EqAbs<Float> for GaussianRational {
61 /// Determines whether the absolute values of a [`GaussianRational`] and a [`Float`] are equal.
62 ///
63 /// No [`GaussianRational`] is equal in absolute value to $\infty$, $-\infty$, or NaN.
64 ///
65 /// # Worst-case complexity
66 /// $T(n) = O(n \log n \log\log n)$
67 ///
68 /// $M(n) = O(n \log n)$
69 ///
70 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
71 /// bits of `other` and of the real and imaginary parts of `self`.
72 ///
73 /// # Examples
74 /// ```
75 /// use malachite_base::num::comparison::traits::EqAbs;
76 /// use malachite_float::Float;
77 /// use malachite_q::gaussian_rational::GaussianRational;
78 /// use std::str::FromStr;
79 ///
80 /// // |3/2+2i| = 5/2
81 /// let x = GaussianRational::from_str("3/2+2i").unwrap();
82 /// assert!(x.eq_abs(&Float::from(2.5)));
83 /// assert_eq!(x.eq_abs(&Float::from(2)), false);
84 /// ```
85 #[inline]
86 fn eq_abs(&self, other: &Float) -> bool {
87 other.eq_abs(self)
88 }
89}