malachite_q/comparison/
partial_eq_natural.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_nz::natural::Natural;
11
12impl PartialEq<Natural> for Rational {
13    /// Determines whether a [`Rational`] is equal to a [`Natural`].
14    ///
15    /// # Worst-case complexity
16    /// $T(n) = O(n)$
17    ///
18    /// $M(n) = O(1)$
19    ///
20    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
21    /// other.significant_bits())`.
22    ///
23    /// # Examples
24    /// ```
25    /// use malachite_nz::natural::Natural;
26    /// use malachite_q::Rational;
27    ///
28    /// assert!(Rational::from(123) == Natural::from(123u32));
29    /// assert!(Rational::from_signeds(22, 7) != Natural::from(5u32));
30    /// ```
31    fn eq(&self, other: &Natural) -> bool {
32        self.sign && self.denominator == 1 && self.numerator == *other
33    }
34}
35
36impl PartialEq<Rational> for Natural {
37    /// Determines whether a [`Natural`] is equal to a [`Rational`].
38    ///
39    /// # Worst-case complexity
40    /// $T(n) = O(n)$
41    ///
42    /// $M(n) = O(1)$
43    ///
44    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
45    /// other.significant_bits())`.
46    ///
47    /// # Examples
48    /// ```
49    /// use malachite_nz::natural::Natural;
50    /// use malachite_q::Rational;
51    ///
52    /// assert!(Natural::from(123u32) == Rational::from(123));
53    /// assert!(Natural::from(5u32) != Rational::from_signeds(22, 7));
54    /// ```
55    fn eq(&self, other: &Rational) -> bool {
56        other.sign && other.denominator == 1 && *self == other.numerator
57    }
58}