malachite_nz/integer/comparison/
eq_abs_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::integer::Integer;
10use crate::natural::Natural;
11use malachite_base::num::comparison::traits::EqAbs;
12
13impl EqAbs<Natural> for Integer {
14    /// Determines whether the absolute values of an [`Integer`] and a [`Natural`] are equal.
15    ///
16    /// # Worst-case complexity
17    /// $T(n) = O(n)$
18    ///
19    /// $M(n) = O(1)$
20    ///
21    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
22    /// other.significant_bits())`.
23    ///
24    /// # Examples
25    /// ```
26    /// use malachite_base::num::comparison::traits::EqAbs;
27    /// use malachite_nz::integer::Integer;
28    /// use malachite_nz::natural::Natural;
29    ///
30    /// assert_eq!(Integer::from(-123).eq_abs(&Natural::from(122u32)), false);
31    /// assert_eq!(Integer::from(-123).eq_abs(&Natural::from(124u32)), false);
32    /// assert_eq!(Integer::from(123).eq_abs(&Natural::from(123u32)), true);
33    /// assert_eq!(Integer::from(-123).eq_abs(&Natural::from(123u32)), true);
34    /// ```
35    #[inline]
36    fn eq_abs(&self, other: &Natural) -> bool {
37        self.abs == *other
38    }
39}
40
41impl EqAbs<Integer> for Natural {
42    /// Determines whether the absolute values of an [`Integer`] and a [`Natural`] are equal.
43    ///
44    /// # Worst-case complexity
45    /// $T(n) = O(n)$
46    ///
47    /// $M(n) = O(1)$
48    ///
49    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
50    /// other.significant_bits())`.
51    ///
52    /// # Examples
53    /// ```
54    /// use malachite_base::num::comparison::traits::EqAbs;
55    /// use malachite_nz::integer::Integer;
56    /// use malachite_nz::natural::Natural;
57    ///
58    /// assert_eq!(Natural::from(122u32).eq_abs(&Integer::from(-123)), false);
59    /// assert_eq!(Natural::from(124u32).eq_abs(&Integer::from(-123)), false);
60    /// assert_eq!(Natural::from(123u32).eq_abs(&Integer::from(123)), true);
61    /// assert_eq!(Natural::from(123u32).eq_abs(&Integer::from(-123)), true);
62    /// ```
63    #[inline]
64    fn eq_abs(&self, other: &Integer) -> bool {
65        *self == other.abs
66    }
67}