malachite_float/float/comparison/partial_cmp_abs_rational.rs
1// Copyright © 2026 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::InnerFloat::{Finite, Infinity, NaN, Zero};
10use crate::{Float, significand_bits};
11use core::cmp::Ordering::{self, *};
12use malachite_base::num::arithmetic::traits::Sign;
13use malachite_base::num::comparison::traits::PartialOrdAbs;
14use malachite_base::num::conversion::traits::ExactFrom;
15use malachite_q::Rational;
16
17impl PartialOrdAbs<Rational> for Float {
18 /// Compares the absolute values of a [`Float`] and a [`Rational`].
19 ///
20 /// NaN is not comparable to any [`Rational`]. $\infty$ and $-\infty$ are greater in absolute
21 /// value than any [`Rational`]. Both the [`Float`] zero and the [`Float`] negative zero are
22 /// equal to the [`Rational`] zero.
23 ///
24 /// # Worst-case complexity
25 /// $T(n) = O(n \log n \log\log n)$
26 ///
27 /// $M(n) = O(n \log n)$
28 ///
29 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
30 /// other.significant_bits())`.
31 ///
32 /// # Examples
33 /// ```
34 /// use malachite_base::num::basic::traits::{Infinity, NegativeInfinity};
35 /// use malachite_base::num::comparison::traits::PartialOrdAbs;
36 /// use malachite_float::Float;
37 /// use malachite_q::Rational;
38 ///
39 /// assert!(Float::from(80).lt_abs(&Rational::from(100)));
40 /// assert!(Float::from(-80).lt_abs(&Rational::from(-100)));
41 /// assert!(Float::INFINITY.gt_abs(&Rational::from(100)));
42 /// assert!(Float::NEGATIVE_INFINITY.gt_abs(&Rational::from(-100)));
43 /// assert!(Float::from(1.0f64 / 3.0).lt_abs(&Rational::from_unsigneds(1u8, 3)));
44 /// ```
45 // The exact exponent comparison above already decides every case the screens of `cmp_product`
46 // could, so the final cross-multiplication is formed directly.
47 #[cfg_attr(dylint_lib = "malachite_lints", allow(use_cmp_product))]
48 fn partial_cmp_abs(&self, other: &Rational) -> Option<Ordering> {
49 match (self, other) {
50 (float_nan!(), _) => None,
51 (float_either_infinity!(), _) => Some(Greater),
52 (float_either_zero!(), y) => Some(if *y == 0 { Equal } else { Less }),
53 (
54 Self(Finite {
55 exponent: e_x,
56 significand: significand_x,
57 ..
58 }),
59 y,
60 ) => Some(if *y == 0u32 {
61 Greater
62 } else {
63 let e_x = i64::from(*e_x);
64 let exp_cmp = (e_x - 1).cmp(&y.floor_log_base_2_abs());
65 if exp_cmp != Equal {
66 return Some(exp_cmp);
67 }
68 let shift = e_x - i64::exact_from(significand_bits(significand_x));
69 let abs_shift = shift.unsigned_abs();
70 match shift.sign() {
71 Equal => (significand_x * other.denominator_ref()).cmp(other.numerator_ref()),
72 Greater => ((significand_x * other.denominator_ref()) << abs_shift)
73 .cmp(other.numerator_ref()),
74 Less => {
75 let n_trailing_zeros = significand_x.trailing_zeros().unwrap();
76 if abs_shift <= n_trailing_zeros {
77 ((significand_x >> abs_shift) * other.denominator_ref())
78 .cmp(other.numerator_ref())
79 } else {
80 ((significand_x >> n_trailing_zeros) * other.denominator_ref())
81 .cmp(&(other.numerator_ref() << (abs_shift - n_trailing_zeros)))
82 }
83 }
84 }
85 }),
86 }
87 }
88}
89
90impl PartialOrdAbs<Float> for Rational {
91 /// Compares the absolute values of a [`Rational`] and a [`Float`].
92 ///
93 /// No [`Rational`] is comparable to NaN. Every [`Rational`] is smaller in absolute value than
94 /// $\infty$ and $-\infty$. The [`Rational`] zero is equal to both the [`Float`] zero and the
95 /// [`Float`] negative zero.
96 ///
97 /// # Worst-case complexity
98 /// $T(n) = O(n \log n \log\log n)$
99 ///
100 /// $M(n) = O(n \log n)$
101 ///
102 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
103 /// other.significant_bits())`.
104 ///
105 /// # Examples
106 /// ```
107 /// use malachite_base::num::basic::traits::{Infinity, NegativeInfinity};
108 /// use malachite_base::num::comparison::traits::PartialOrdAbs;
109 /// use malachite_float::Float;
110 /// use malachite_q::Rational;
111 ///
112 /// assert!(Rational::from(100).gt_abs(&Float::from(80)));
113 /// assert!(Rational::from(-100).gt_abs(&Float::from(-80)));
114 /// assert!(Rational::from(100).lt_abs(&Float::INFINITY));
115 /// assert!(Rational::from(-100).lt_abs(&Float::NEGATIVE_INFINITY));
116 /// assert!(Rational::from_unsigneds(1u8, 3).gt_abs(&Float::from(1.0f64 / 3.0)));
117 /// ```
118 #[inline]
119 fn partial_cmp_abs(&self, other: &Float) -> Option<Ordering> {
120 other.partial_cmp_abs(self).map(Ordering::reverse)
121 }
122}