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