malachite_q/comparison/partial_cmp_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 core::cmp::Ordering::{self, *};
11use malachite_base::num::arithmetic::traits::Sign;
12use malachite_base::num::basic::traits::One;
13use malachite_base::num::conversion::traits::ExactFrom;
14use malachite_base::num::logic::traits::SignificantBits;
15use malachite_nz::natural::Natural;
16
17impl PartialOrd<Natural> for Rational {
18 /// Compares a [`Rational`] to a [`Natural`].
19 ///
20 /// # Worst-case complexity
21 /// $T(n) = O(n \log n \log\log n)$
22 ///
23 /// $M(n) = O(n \log n)$
24 ///
25 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
26 /// other.significant_bits())`.
27 ///
28 /// # Examples
29 /// ```
30 /// use malachite_nz::natural::Natural;
31 /// use malachite_q::Rational;
32 ///
33 /// assert!(Rational::from_signeds(22, 7) > Natural::from(3u32));
34 /// assert!(Rational::from_signeds(22, 7) < Natural::from(4u32));
35 /// ```
36 fn partial_cmp(&self, other: &Natural) -> Option<Ordering> {
37 // First check signs
38 let self_sign = self.sign();
39 let other_sign = other.sign();
40 let sign_cmp = self_sign.cmp(&other_sign);
41 if sign_cmp != Equal || self_sign == Equal {
42 return Some(sign_cmp);
43 }
44 // Then check if one is < 1 and the other is > 1
45 let self_cmp_one = self.numerator.cmp(&self.denominator);
46 let other_cmp_one = other.cmp(&Natural::ONE);
47 let one_cmp = self_cmp_one.cmp(&other_cmp_one);
48 if one_cmp != Equal {
49 return Some(one_cmp);
50 }
51 // Then compare numerators and denominators
52 let n_cmp = self.numerator.cmp(other);
53 let d_cmp = self.denominator.cmp(&Natural::ONE);
54 if n_cmp == Equal && d_cmp == Equal {
55 return Some(Equal);
56 }
57 let nd_cmp = n_cmp.cmp(&d_cmp);
58 if nd_cmp != Equal {
59 return Some(nd_cmp);
60 }
61 let log_cmp = self
62 .floor_log_base_2_abs()
63 .cmp(&i64::exact_from(other.significant_bits() - 1));
64 if log_cmp != Equal {
65 return Some(if self.sign {
66 log_cmp
67 } else {
68 log_cmp.reverse()
69 });
70 }
71 // Finally, cross-multiply.
72 Some(self.numerator.cmp(&(&self.denominator * other)))
73 }
74}
75
76impl PartialOrd<Rational> for Natural {
77 /// Compares a [`Natural`] to a [`Rational`].
78 ///
79 /// # Worst-case complexity
80 /// $T(n) = O(n \log n \log\log n)$
81 ///
82 /// $M(n) = O(n \log n)$
83 ///
84 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
85 /// other.significant_bits())`.
86 ///
87 /// # Examples
88 /// ```
89 /// use malachite_nz::natural::Natural;
90 /// use malachite_q::Rational;
91 ///
92 /// assert!(Natural::from(3u32) < Rational::from_signeds(22, 7));
93 /// assert!(Natural::from(4u32) > Rational::from_signeds(22, 7));
94 /// ```
95 #[inline]
96 fn partial_cmp(&self, other: &Rational) -> Option<Ordering> {
97 other.partial_cmp(self).map(Ordering::reverse)
98 }
99}