Skip to main content

malachite_nz/integer/comparison/
partial_eq_natural.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::integer::Integer;
10use crate::natural::Natural;
11
12impl PartialEq<Natural> for Integer {
13    /// Determines whether an [`Integer`] 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::integer::Integer;
26    /// use malachite_nz::natural::Natural;
27    ///
28    /// assert!(Integer::from(123) == Natural::from(123u32));
29    /// assert!(Integer::from(123) != Natural::from(5u32));
30    /// ```
31    fn eq(&self, other: &Natural) -> bool {
32        self.sign && self.abs == *other
33    }
34}
35
36impl PartialEq<Integer> for Natural {
37    /// Determines whether a [`Natural`] is equal to an [`Integer`].
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::integer::Integer;
50    /// use malachite_nz::natural::Natural;
51    ///
52    /// assert!(Natural::from(123u32) == Integer::from(123));
53    /// assert!(Natural::from(123u32) != Integer::from(5));
54    /// ```
55    fn eq(&self, other: &Integer) -> bool {
56        other.sign && *self == other.abs
57    }
58}