malachite_float/comparison/
partial_cmp_abs_rational.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::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    fn partial_cmp_abs(&self, other: &Rational) -> Option<Ordering> {
46        match (self, other) {
47            (float_nan!(), _) => None,
48            (float_either_infinity!(), _) => Some(Greater),
49            (float_either_zero!(), y) => Some(if *y == 0 { Equal } else { Less }),
50            (
51                Self(Finite {
52                    exponent: e_x,
53                    significand: significand_x,
54                    ..
55                }),
56                y,
57            ) => Some(if *y == 0u32 {
58                Greater
59            } else {
60                let e_x = i64::from(*e_x);
61                let exp_cmp = (e_x - 1).cmp(&y.floor_log_base_2_abs());
62                if exp_cmp != Equal {
63                    return Some(exp_cmp);
64                }
65                let shift = e_x - i64::exact_from(significand_bits(significand_x));
66                let abs_shift = shift.unsigned_abs();
67                match shift.sign() {
68                    Equal => (significand_x * other.denominator_ref()).cmp(other.numerator_ref()),
69                    Greater => ((significand_x * other.denominator_ref()) << abs_shift)
70                        .cmp(other.numerator_ref()),
71                    Less => {
72                        let n_trailing_zeros = significand_x.trailing_zeros().unwrap();
73                        if abs_shift <= n_trailing_zeros {
74                            ((significand_x >> abs_shift) * other.denominator_ref())
75                                .cmp(other.numerator_ref())
76                        } else {
77                            ((significand_x >> n_trailing_zeros) * other.denominator_ref())
78                                .cmp(&(other.numerator_ref() << (abs_shift - n_trailing_zeros)))
79                        }
80                    }
81                }
82            }),
83        }
84    }
85}
86
87impl PartialOrdAbs<Float> for Rational {
88    /// Compares the absolute values of a [`Rational`] and a [`Float`].
89    ///
90    /// No [`Rational`] is comparable to NaN. Every [`Rational`] is smaller in absolute value than
91    /// $\infty$ and $-\infty$. The [`Rational`] zero is equal to both the [`Float`] zero and the
92    /// [`Float`] negative zero.
93    ///
94    /// # Worst-case complexity
95    /// $T(n) = O(n \log n \log\log n)$
96    ///
97    /// $M(n) = O(n \log n)$
98    ///
99    /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
100    /// other.significant_bits())`.
101    ///
102    /// # Examples
103    /// ```
104    /// use malachite_base::num::basic::traits::{Infinity, NegativeInfinity};
105    /// use malachite_base::num::comparison::traits::PartialOrdAbs;
106    /// use malachite_float::Float;
107    /// use malachite_q::Rational;
108    ///
109    /// assert!(Rational::from(100).gt_abs(&Float::from(80)));
110    /// assert!(Rational::from(-100).gt_abs(&Float::from(-80)));
111    /// assert!(Rational::from(100).lt_abs(&Float::INFINITY));
112    /// assert!(Rational::from(-100).lt_abs(&Float::NEGATIVE_INFINITY));
113    /// assert!(Rational::from_unsigneds(1u8, 3).gt_abs(&Float::from(1.0f64 / 3.0)));
114    /// ```
115    #[inline]
116    fn partial_cmp_abs(&self, other: &Float) -> Option<Ordering> {
117        other.partial_cmp_abs(self).map(Ordering::reverse)
118    }
119}