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 { abs_cmp } else { abs_cmp.reverse() }
49 }))
50 }
51 }
52}
53
54macro_rules! impl_partial_cmp_primitive_float {
55 ($t: ident) => {
56 impl PartialOrd<$t> for Float {
57 /// Compares a [`Float`] to a primitive float.
58 ///
59 /// The [`Float`] NaN is not comparable to any primitive float, not even the primitive
60 /// float NaN. Every [`Float`] zero is equal to every primitive float zero, regardless
61 /// of sign.
62 ///
63 /// # Worst-case complexity
64 /// $T(n) = O(n)$
65 ///
66 /// $M(n) = O(1)$
67 ///
68 /// where $T$ is time, $M$ is additional memory, and $n$ is
69 /// `max(self.significant_bits(), other.sci_exponent().abs())`.
70 ///
71 /// # Examples
72 /// See [here](super::partial_cmp_primitive_float#partial_cmp).
73 #[inline]
74 fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
75 float_partial_cmp_primitive_float(self, other)
76 }
77 }
78
79 impl PartialOrd<Float> for $t {
80 /// Compares a primitive float to a [`Float`].
81 ///
82 /// The primitive float NaN is not comparable to any primitive float, not even the
83 /// [`Float`] NaN. Every primitive float zero is equal to every [`Float`] zero,
84 /// regardless of sign.
85 ///
86 /// # Worst-case complexity
87 /// $T(n) = O(n)$
88 ///
89 /// $M(n) = O(1)$
90 ///
91 /// where $T$ is time, $M$ is additional memory, and $n$ is
92 /// `max(self.significant_bits(), other.sci_exponent().abs())`.
93 ///
94 /// # Examples
95 /// See [here](super::partial_cmp_primitive_float#partial_cmp).
96 #[inline]
97 fn partial_cmp(&self, other: &Float) -> Option<Ordering> {
98 other.partial_cmp(self).map(Ordering::reverse)
99 }
100 }
101 };
102}
103apply_to_primitive_floats!(impl_partial_cmp_primitive_float);