Skip to main content

malachite_float/float/comparison/
partial_eq_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_q::gaussian_rational::GaussianRational;
11
12impl PartialEq<GaussianRational> for Float {
13    /// Determines whether a [`Float`] is equal to a [`GaussianRational`].
14    ///
15    /// $\infty$, $-\infty$, and NaN are not equal to any [`GaussianRational`]. Both the [`Float`]
16    /// zero and the [`Float`] negative zero are equal to the [`GaussianRational`] zero.
17    ///
18    /// # Worst-case complexity
19    /// $T(n) = O(n)$
20    ///
21    /// $M(n) = O(1)$
22    ///
23    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
24    /// other.real.significant_bits())`.
25    ///
26    /// # Examples
27    /// ```
28    /// use malachite_base::num::basic::traits::OneHalf;
29    /// use malachite_float::Float;
30    /// use malachite_q::gaussian_rational::GaussianRational;
31    /// use std::str::FromStr;
32    ///
33    /// assert!(Float::from(123) == GaussianRational::from(123));
34    /// assert!(Float::ONE_HALF == GaussianRational::from_str("1/2").unwrap());
35    /// assert!(Float::from(123) != GaussianRational::from_str("123+i").unwrap());
36    /// ```
37    fn eq(&self, other: &GaussianRational) -> bool {
38        other.imaginary == 0u32 && *self == other.real
39    }
40}
41
42impl PartialEq<Float> for GaussianRational {
43    /// Determines whether a [`GaussianRational`] is equal to a [`Float`].
44    ///
45    /// No [`GaussianRational`] is equal to $\infty$, $-\infty$, or NaN. The [`GaussianRational`]
46    /// zero is equal to both the [`Float`] zero and the [`Float`] negative zero.
47    ///
48    /// # Worst-case complexity
49    /// $T(n) = O(n)$
50    ///
51    /// $M(n) = O(1)$
52    ///
53    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.real.significant_bits(),
54    /// other.significant_bits())`.
55    ///
56    /// # Examples
57    /// ```
58    /// use malachite_base::num::basic::traits::OneHalf;
59    /// use malachite_float::Float;
60    /// use malachite_q::gaussian_rational::GaussianRational;
61    /// use std::str::FromStr;
62    ///
63    /// assert!(GaussianRational::from(123) == Float::from(123));
64    /// assert!(GaussianRational::from_str("1/2").unwrap() == Float::ONE_HALF);
65    /// assert!(GaussianRational::from_str("123+i").unwrap() != Float::from(123));
66    /// ```
67    fn eq(&self, other: &Float) -> bool {
68        self.imaginary == 0u32 && self.real == *other
69    }
70}