malachite_float/comparison/
partial_cmp_primitive_float.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::arithmetic::traits::Sign;
13use malachite_base::num::basic::floats::PrimitiveFloat;
14use malachite_nz::natural::Natural;
15
16fn float_partial_cmp_primitive_float<T: PrimitiveFloat>(x: &Float, y: &T) -> Option<Ordering> {
17    match (x, y) {
18        (float_nan!(), _) => None,
19        (_, y) if y.is_nan() => None,
20        (float_infinity!(), y) if *y == T::INFINITY => Some(Equal),
21        (float_negative_infinity!(), y) if *y == T::NEGATIVE_INFINITY => Some(Equal),
22        (float_infinity!(), _) => Some(Greater),
23        (float_negative_infinity!(), _) => Some(Less),
24        (_, y) if *y == T::NEGATIVE_INFINITY => Some(Greater),
25        (_, y) if *y == T::INFINITY => Some(Less),
26        (float_either_zero!(), y) => Some(if *y == T::ZERO {
27            Equal
28        } else if y.is_sign_positive() {
29            Less
30        } else {
31            Greater
32        }),
33        (x, y) if *y == T::ZERO => Some(x.sign()),
34        (
35            Float(Finite {
36                sign: s_x,
37                exponent: e_x,
38                significand: m_x,
39                ..
40            }),
41            y,
42        ) => {
43            let s_y = y.is_sign_positive();
44            Some(s_x.cmp(&s_y).then_with(|| {
45                let abs_cmp = (i64::from(*e_x) - 1)
46                    .cmp(&y.sci_exponent())
47                    .then_with(|| m_x.cmp_normalized(&Natural::from(y.integer_mantissa())));
48                if *s_x {
49                    abs_cmp
50                } else {
51                    abs_cmp.reverse()
52                }
53            }))
54        }
55    }
56}
57
58macro_rules! impl_partial_cmp_primitive_float {
59    ($t: ident) => {
60        impl PartialOrd<$t> for Float {
61            /// Compares a [`Float`] to a primitive float.
62            ///
63            /// The [`Float`] NaN is not comparable to any primitive float, not even the primitive
64            /// float NaN. Every [`Float`] zero is equal to every primitive float zero, regardless
65            /// of sign.
66            ///
67            /// # Worst-case complexity
68            /// $T(n) = O(n)$
69            ///
70            /// $M(n) = O(1)$
71            ///
72            /// where $T$ is time, $M$ is additional memory, and $n$ is
73            /// `max(self.significant_bits(), other.sci_exponent().abs())`.
74            ///
75            /// # Examples
76            /// See [here](super::partial_cmp_primitive_float#partial_cmp).
77            #[inline]
78            fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
79                float_partial_cmp_primitive_float(self, other)
80            }
81        }
82
83        impl PartialOrd<Float> for $t {
84            /// Compares a primitive float to a [`Float`].
85            ///
86            /// The primitive float NaN is not comparable to any primitive float, not even the
87            /// [`Float`] NaN. Every primitive float zero is equal to every [`Float`] zero,
88            /// regardless of sign.
89            ///
90            /// # Worst-case complexity
91            /// $T(n) = O(n)$
92            ///
93            /// $M(n) = O(1)$
94            ///
95            /// where $T$ is time, $M$ is additional memory, and $n$ is
96            /// `max(self.significant_bits(), other.sci_exponent().abs())`.
97            ///
98            /// # Examples
99            /// See [here](super::partial_cmp_primitive_float#partial_cmp).
100            #[inline]
101            fn partial_cmp(&self, other: &Float) -> Option<Ordering> {
102                other.partial_cmp(self).map(Ordering::reverse)
103            }
104        }
105    };
106}
107apply_to_primitive_floats!(impl_partial_cmp_primitive_float);