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