malachite_q/comparison/eq_abs.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;
11
12impl EqAbs for Rational {
13 /// Determines whether the absolute values of two [`Rational`]s are equal.
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_base::num::comparison::traits::EqAbs;
26 /// use malachite_q::Rational;
27 ///
28 /// assert_eq!(
29 /// Rational::from_signeds(-22, 7).eq_abs(&Rational::from_signeds(-23, 7)),
30 /// false
31 /// );
32 /// assert_eq!(
33 /// Rational::from_signeds(-22, 7).eq_abs(&Rational::from_signeds(-24, 7)),
34 /// false
35 /// );
36 /// assert_eq!(
37 /// Rational::from_signeds(22, 7).eq_abs(&Rational::from_signeds(22, 7)),
38 /// true
39 /// );
40 /// assert_eq!(
41 /// Rational::from_signeds(22, 7).eq_abs(&Rational::from_signeds(-22, 7)),
42 /// true
43 /// );
44 /// assert_eq!(
45 /// Rational::from_signeds(-22, 7).eq_abs(&Rational::from_signeds(22, 7)),
46 /// true
47 /// );
48 /// assert_eq!(
49 /// Rational::from_signeds(-22, 7).eq_abs(&Rational::from_signeds(-22, 7)),
50 /// true
51 /// );
52 /// ```
53 #[inline]
54 fn eq_abs(&self, other: &Self) -> bool {
55 self.numerator == other.numerator && self.denominator == other.denominator
56 }
57}