malachite_q/comparison/
eq_abs_integer.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_base::num::comparison::traits::EqAbs;
11use malachite_nz::integer::Integer;
12
13impl EqAbs<Integer> for Rational {
14    /// Determines whether the absolute values of a [`Rational`] and an [`Integer`] 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_q::Rational;
29    ///
30    /// assert_eq!(Rational::from(-123).eq_abs(&Integer::from(122)), false);
31    /// assert_eq!(Rational::from(-123).eq_abs(&Integer::from(124)), false);
32    /// assert_eq!(
33    ///     Rational::from_signeds(22, 7).eq_abs(&Integer::from(123)),
34    ///     false
35    /// );
36    /// assert_eq!(
37    ///     Rational::from_signeds(-22, 7).eq_abs(&Integer::from(123)),
38    ///     false
39    /// );
40    /// assert_eq!(Rational::from(123).eq_abs(&Integer::from(123)), true);
41    /// assert_eq!(Rational::from(-123).eq_abs(&Integer::from(123)), true);
42    /// assert_eq!(
43    ///     Rational::from_signeds(22, 7).eq_abs(&Integer::from(123)),
44    ///     false
45    /// );
46    /// assert_eq!(
47    ///     Rational::from_signeds(22, 7).eq_abs(&Integer::from(123)),
48    ///     false
49    /// );
50    /// assert_eq!(Rational::from(-123).eq_abs(&Integer::from(-122)), false);
51    /// assert_eq!(Rational::from(-123).eq_abs(&Integer::from(-124)), false);
52    /// assert_eq!(
53    ///     Rational::from_signeds(22, 7).eq_abs(&Integer::from(-123)),
54    ///     false
55    /// );
56    /// assert_eq!(
57    ///     Rational::from_signeds(-22, 7).eq_abs(&Integer::from(1 - 23)),
58    ///     false
59    /// );
60    /// assert_eq!(Rational::from(123).eq_abs(&Integer::from(-123)), true);
61    /// assert_eq!(Rational::from(-123).eq_abs(&Integer::from(-123)), true);
62    /// assert_eq!(
63    ///     Rational::from_signeds(22, 7).eq_abs(&Integer::from(-123)),
64    ///     false
65    /// );
66    /// assert_eq!(
67    ///     Rational::from_signeds(22, 7).eq_abs(&Integer::from(-123)),
68    ///     false
69    /// );
70    /// ```
71    #[inline]
72    fn eq_abs(&self, other: &Integer) -> bool {
73        self.denominator == 1 && self.numerator == *other.unsigned_abs_ref()
74    }
75}
76
77impl EqAbs<Rational> for Integer {
78    /// Determines whether the absolute values of a [`Rational`] and an [`Integer`] are equal.
79    ///
80    /// # Worst-case complexity
81    /// $T(n) = O(n)$
82    ///
83    /// $M(n) = O(1)$
84    ///
85    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
86    /// other.significant_bits())`.
87    ///
88    /// # Examples
89    /// ```
90    /// use malachite_base::num::comparison::traits::EqAbs;
91    /// use malachite_nz::integer::Integer;
92    /// use malachite_q::Rational;
93    ///
94    /// assert_eq!(Integer::from(122).eq_abs(&Rational::from(-123)), false);
95    /// assert_eq!(Integer::from(124).eq_abs(&Rational::from(-123)), false);
96    /// assert_eq!(
97    ///     Integer::from(124).eq_abs(&Rational::from_signeds(22, 7)),
98    ///     false
99    /// );
100    /// assert_eq!(
101    ///     Integer::from(124).eq_abs(&Rational::from_signeds(-22, 7)),
102    ///     false
103    /// );
104    /// assert_eq!(Integer::from(123).eq_abs(&Rational::from(123)), true);
105    /// assert_eq!(Integer::from(123).eq_abs(&Rational::from(-123)), true);
106    /// assert_eq!(
107    ///     Integer::from(123).eq_abs(&Rational::from_signeds(22, 7)),
108    ///     false
109    /// );
110    /// assert_eq!(
111    ///     Integer::from(123).eq_abs(&Rational::from_signeds(-22, 7)),
112    ///     false
113    /// );
114    /// assert_eq!(Integer::from(-122).eq_abs(&Rational::from(-123)), false);
115    /// assert_eq!(Integer::from(-124).eq_abs(&Rational::from(-123)), false);
116    /// assert_eq!(
117    ///     Integer::from(-124).eq_abs(&Rational::from_signeds(22, 7)),
118    ///     false
119    /// );
120    /// assert_eq!(
121    ///     Integer::from(-124).eq_abs(&Rational::from_signeds(-22, 7)),
122    ///     false
123    /// );
124    /// assert_eq!(Integer::from(-123).eq_abs(&Rational::from(123)), true);
125    /// assert_eq!(Integer::from(-123).eq_abs(&Rational::from(-123)), true);
126    /// assert_eq!(
127    ///     Integer::from(-123).eq_abs(&Rational::from_signeds(22, 7)),
128    ///     false
129    /// );
130    /// assert_eq!(
131    ///     Integer::from(-123).eq_abs(&Rational::from_signeds(-22, 7)),
132    ///     false
133    /// );
134    /// ```
135    #[inline]
136    fn eq_abs(&self, other: &Rational) -> bool {
137        other.denominator == 1 && other.numerator == *self.unsigned_abs_ref()
138    }
139}