malachite_float/comparison/partial_cmp_abs_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::basic::floats::PrimitiveFloat;
13use malachite_base::num::comparison::traits::PartialOrdAbs;
14use malachite_nz::natural::Natural;
15
16fn float_partial_cmp_abs_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.is_finite() => Some(Equal),
21 (Float(Infinity { .. }), _) => Some(Greater),
22 (_, y) if !y.is_finite() => Some(Less),
23 (float_either_zero!(), y) => Some(if *y == T::ZERO { Equal } else { Less }),
24 (_, y) if *y == T::ZERO => Some(Greater),
25 (
26 Float(Finite {
27 exponent: e_x,
28 significand: m_x,
29 ..
30 }),
31 y,
32 ) => Some(
33 (i64::from(*e_x) - 1)
34 .cmp(&y.sci_exponent())
35 .then_with(|| m_x.cmp_normalized(&Natural::from(y.integer_mantissa()))),
36 ),
37 }
38}
39
40macro_rules! impl_partial_cmp_abs_primitive_float {
41 ($t: ident) => {
42 impl PartialOrdAbs<$t> for Float {
43 /// Compares the absolute values of a [`Float`] and a primitive float.
44 ///
45 /// The [`Float`] NaN is not comparable to any primitive float, not even the primitive
46 /// float NaN. Every [`Float`] zero is equal to every primitive float zero, regardless
47 /// of sign.
48 ///
49 /// # Worst-case complexity
50 /// $T(n) = O(n)$
51 ///
52 /// $M(n) = O(1)$
53 ///
54 /// where $T$ is time, $M$ is additional memory, and $n$ is
55 /// `max(self.significant_bits(), other.sci_exponent().abs())`.
56 ///
57 /// # Examples
58 /// See [here](super::partial_cmp_abs_primitive_float#partial_cmp_abs).
59 #[inline]
60 fn partial_cmp_abs(&self, other: &$t) -> Option<Ordering> {
61 float_partial_cmp_abs_primitive_float(self, other)
62 }
63 }
64
65 impl PartialOrdAbs<Float> for $t {
66 /// Compares the absolute values of a primitive float and a [`Float`].
67 ///
68 /// The primitive float NaN is not comparable to any primitive float, not even the
69 /// [`Float`] NaN. Every primitive float zero is equal to every [`Float`] zero,
70 /// regardless of sign.
71 ///
72 /// # Worst-case complexity
73 /// $T(n) = O(n)$
74 ///
75 /// $M(n) = O(1)$
76 ///
77 /// where $T$ is time, $M$ is additional memory, and $n$ is
78 /// `max(self.significant_bits(), other.sci_exponent().abs())`.
79 ///
80 /// # Examples
81 /// See [here](super::partial_cmp_primitive_float#partial_cmp_abs).
82 #[inline]
83 fn partial_cmp_abs(&self, other: &Float) -> Option<Ordering> {
84 other.partial_cmp_abs(self).map(Ordering::reverse)
85 }
86 }
87 };
88}
89apply_to_primitive_floats!(impl_partial_cmp_abs_primitive_float);