malachite_float/comparison/
partial_cmp_abs_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::Float;
10use crate::InnerFloat::{Finite, Infinity, NaN, Zero};
11use core::cmp::Ordering::{self, *};
12use malachite_base::num::comparison::traits::PartialOrdAbs;
13use malachite_base::num::logic::traits::SignificantBits;
14use malachite_nz::natural::Natural;
15
16impl PartialOrdAbs<Natural> for Float {
17    /// Compares the absolute value of a [`Float`] to a [`Natural`].
18    ///
19    /// NaN is not comparable to any [`Natural`]. $\infty$ and $-\infty$ are greater in absolute
20    /// value than any [`Natural`]. Both the [`Float`] zero and the [`Float`] negative zero are
21    /// equal to the [`Natural`] zero.
22    ///
23    /// # Worst-case complexity
24    /// $T(n) = O(n)$
25    ///
26    /// $M(n) = O(1)$
27    ///
28    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
29    /// other.significant_bits())`.
30    ///
31    /// # Examples
32    /// ```
33    /// use malachite_base::num::basic::traits::{Infinity, NegativeInfinity};
34    /// use malachite_base::num::comparison::traits::PartialOrdAbs;
35    /// use malachite_float::Float;
36    /// use malachite_nz::natural::Natural;
37    ///
38    /// assert!(Float::from(80).lt_abs(&Natural::from(100u32)));
39    /// assert!(Float::INFINITY.gt_abs(&Natural::from(100u32)));
40    /// assert!(Float::NEGATIVE_INFINITY.gt_abs(&Natural::from(100u32)));
41    /// ```
42    fn partial_cmp_abs(&self, other: &Natural) -> Option<Ordering> {
43        match (self, other) {
44            (float_nan!(), _) => None,
45            (Self(Infinity { .. }), _) => Some(Greater),
46            (float_either_zero!(), y) => Some(if *y == 0 { Equal } else { Less }),
47            (
48                Self(Finite {
49                    exponent: e_x,
50                    significand: x,
51                    ..
52                }),
53                y,
54            ) => Some(if *other == 0 {
55                Greater
56            } else if *e_x <= 0 {
57                Less
58            } else {
59                u64::from(e_x.unsigned_abs())
60                    .cmp(&other.significant_bits())
61                    .then_with(|| x.cmp_normalized(y))
62            }),
63        }
64    }
65}
66
67impl PartialOrdAbs<Float> for Natural {
68    /// Compares a [`Natural`] to the absolute value of a [`Float`].
69    ///
70    /// No [`Natural`] is comparable to NaN. Every [`Natural`] is smaller in absolute value than
71    /// $\infty$ and $-\infty$. The [`Natural`] zero is equal to both the [`Float`] zero and the
72    /// [`Float`] negative zero.
73    ///
74    /// # Worst-case complexity
75    /// $T(n) = O(n)$
76    ///
77    /// $M(n) = O(1)$
78    ///
79    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
80    /// other.significant_bits())`.
81    ///
82    /// # Examples
83    /// ```
84    /// use malachite_base::num::basic::traits::{Infinity, NegativeInfinity};
85    /// use malachite_base::num::comparison::traits::PartialOrdAbs;
86    /// use malachite_float::Float;
87    /// use malachite_nz::natural::Natural;
88    ///
89    /// assert!(Natural::from(100u32).gt_abs(&Float::from(80)));
90    /// assert!(Natural::from(100u32).lt_abs(&Float::INFINITY));
91    /// assert!(Natural::from(100u32).lt_abs(&Float::NEGATIVE_INFINITY));
92    /// ```
93    #[inline]
94    fn partial_cmp_abs(&self, other: &Float) -> Option<Ordering> {
95        other.partial_cmp_abs(self).map(Ordering::reverse)
96    }
97}