malachite_q/comparison/
partial_eq_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_nz::integer::Integer;
11
12impl PartialEq<Integer> for Rational {
13    /// Determines whether a [`Rational`] is equal to an [`Integer`].
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_q::Rational;
27    ///
28    /// assert!(Rational::from(-123) == Integer::from(-123));
29    /// assert!(Rational::from_signeds(22, 7) != Integer::from(5));
30    /// ```
31    fn eq(&self, other: &Integer) -> bool {
32        self.sign == (*other >= 0)
33            && self.denominator == 1
34            && self.numerator == *other.unsigned_abs_ref()
35    }
36}
37
38impl PartialEq<Rational> for Integer {
39    /// Determines whether an [`Integer`] is equal to a [`Rational`].
40    ///
41    /// # Worst-case complexity
42    /// $T(n) = O(n)$
43    ///
44    /// $M(n) = O(1)$
45    ///
46    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
47    /// other.significant_bits())`.
48    ///
49    /// # Examples
50    /// ```
51    /// use malachite_nz::integer::Integer;
52    /// use malachite_q::Rational;
53    ///
54    /// assert!(Integer::from(-123) == Rational::from(-123));
55    /// assert!(Integer::from(5) != Rational::from_signeds(22, 7));
56    /// ```
57    fn eq(&self, other: &Rational) -> bool {
58        other.sign == (*self >= 0)
59            && other.denominator == 1
60            && *self.unsigned_abs_ref() == other.numerator
61    }
62}