malachite_q/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::Rational;
10use malachite_base::num::comparison::traits::EqAbs;
11use malachite_nz::natural::Natural;
12
13impl EqAbs<Natural> for Rational {
14 /// Determines whether the absolute values of a [`Rational`] 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::natural::Natural;
28 /// use malachite_q::Rational;
29 ///
30 /// assert_eq!(Rational::from(-123).eq_abs(&Natural::from(122u32)), false);
31 /// assert_eq!(Rational::from(-123).eq_abs(&Natural::from(124u32)), false);
32 /// assert_eq!(
33 /// Rational::from_signeds(22, 7).eq_abs(&Natural::from(123u32)),
34 /// false
35 /// );
36 /// assert_eq!(
37 /// Rational::from_signeds(-22, 7).eq_abs(&Natural::from(123u32)),
38 /// false
39 /// );
40 /// assert_eq!(Rational::from(123).eq_abs(&Natural::from(123u32)), true);
41 /// assert_eq!(Rational::from(-123).eq_abs(&Natural::from(123u32)), true);
42 /// assert_eq!(
43 /// Rational::from_signeds(22, 7).eq_abs(&Natural::from(123u32)),
44 /// false
45 /// );
46 /// assert_eq!(
47 /// Rational::from_signeds(22, 7).eq_abs(&Natural::from(123u32)),
48 /// false
49 /// );
50 /// ```
51 #[inline]
52 fn eq_abs(&self, other: &Natural) -> bool {
53 self.denominator == 1 && self.numerator == *other
54 }
55}
56
57impl EqAbs<Rational> for Natural {
58 /// Determines whether the absolute values of a [`Rational`] and a [`Natural`] are equal.
59 ///
60 /// # Worst-case complexity
61 /// $T(n) = O(n)$
62 ///
63 /// $M(n) = O(1)$
64 ///
65 /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
66 /// other.significant_bits())`.
67 ///
68 /// # Examples
69 /// ```
70 /// use malachite_base::num::comparison::traits::EqAbs;
71 /// use malachite_nz::natural::Natural;
72 /// use malachite_q::Rational;
73 ///
74 /// assert_eq!(Natural::from(122u32).eq_abs(&Rational::from(-123)), false);
75 /// assert_eq!(Natural::from(124u32).eq_abs(&Rational::from(-123)), false);
76 /// assert_eq!(
77 /// Natural::from(124u32).eq_abs(&Rational::from_signeds(22, 7)),
78 /// false
79 /// );
80 /// assert_eq!(
81 /// Natural::from(124u32).eq_abs(&Rational::from_signeds(-22, 7)),
82 /// false
83 /// );
84 /// assert_eq!(Natural::from(123u32).eq_abs(&Rational::from(123)), true);
85 /// assert_eq!(Natural::from(123u32).eq_abs(&Rational::from(-123)), true);
86 /// assert_eq!(
87 /// Natural::from(123u32).eq_abs(&Rational::from_signeds(22, 7)),
88 /// false
89 /// );
90 /// assert_eq!(
91 /// Natural::from(123u32).eq_abs(&Rational::from_signeds(-22, 7)),
92 /// false
93 /// );
94 /// ```
95 #[inline]
96 fn eq_abs(&self, other: &Rational) -> bool {
97 other.denominator == 1 && other.numerator == *self
98 }
99}