malachite_float/comparison/partial_cmp_abs_primitive_int.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::UnsignedAbs;
13use malachite_base::num::basic::signeds::PrimitiveSigned;
14use malachite_base::num::basic::unsigneds::PrimitiveUnsigned;
15use malachite_base::num::comparison::traits::PartialOrdAbs;
16use malachite_nz::natural::Natural;
17
18fn float_partial_cmp_abs_unsigned<T: PrimitiveUnsigned>(x: &Float, y: &T) -> Option<Ordering>
19where
20 Natural: From<T>,
21{
22 match (x, y) {
23 (float_nan!(), _) => None,
24 (Float(Infinity { .. }), _) => Some(Greater),
25 (float_either_zero!(), y) => Some(if *y == T::ZERO { Equal } else { Less }),
26 (
27 Float(Finite {
28 exponent: e_x,
29 significand: sig_x,
30 ..
31 }),
32 y,
33 ) => Some(if *y == T::ZERO {
34 Greater
35 } else if *e_x <= 0 {
36 Less
37 } else {
38 u64::from(e_x.unsigned_abs())
39 .cmp(&y.significant_bits())
40 .then_with(|| sig_x.cmp_normalized(&Natural::from(*y)))
41 }),
42 }
43}
44
45macro_rules! impl_from_unsigned {
46 ($t: ident) => {
47 impl PartialOrdAbs<$t> for Float {
48 /// Compares the absolute values of a [`Float`] and an unsigned primitive integer.
49 ///
50 /// NaN is not comparable to any primitive integer. $\infty$ and $-\infty$ are greater
51 /// in absolute value than any primitive integer. Both the [`Float`] zero and the
52 /// [`Float`] negative zero are equal to the integer zero.
53 ///
54 /// # Worst-case complexity
55 /// $T(n) = O(n)$
56 ///
57 /// $M(n) = O(1)$
58 ///
59 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
60 ///
61 /// # Examples
62 /// See [here](super::partial_cmp_abs_primitive_int#partial_cmp_abs).
63 #[inline]
64 fn partial_cmp_abs(&self, other: &$t) -> Option<Ordering> {
65 float_partial_cmp_abs_unsigned(self, other)
66 }
67 }
68
69 impl PartialOrdAbs<Float> for $t {
70 /// Compares the absolute values of an unsigned primitive integer and a [`Float`].
71 ///
72 /// No primitive integer is comparable to NaN. Every primitive integer is smaller in
73 /// absolute value than $\infty$ and $-\infty$. The integer zero is equal to both the
74 /// [`Float`] zero and the [`Float`] 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 `other.significant_bits()`.
82 ///
83 /// See [here](super::partial_cmp_abs_primitive_int#partial_cmp_abs).
84 #[inline]
85 fn partial_cmp_abs(&self, other: &Float) -> Option<Ordering> {
86 other.partial_cmp_abs(self).map(Ordering::reverse)
87 }
88 }
89 };
90}
91apply_to_unsigneds!(impl_from_unsigned);
92
93fn float_partial_cmp_abs_signed<T: PrimitiveSigned>(x: &Float, y: &T) -> Option<Ordering>
94where
95 Natural: From<<T as UnsignedAbs>::Output>,
96{
97 match (x, y) {
98 (float_nan!(), _) => None,
99 (Float(Infinity { .. }), _) => Some(Greater),
100 (float_either_zero!(), y) => Some(if *y == T::ZERO { Equal } else { Less }),
101 (
102 Float(Finite {
103 exponent: e_x,
104 significand: sig_x,
105 ..
106 }),
107 y,
108 ) => Some(if *y == T::ZERO {
109 Greater
110 } else if *e_x <= 0 {
111 Less
112 } else {
113 u64::from(e_x.unsigned_abs())
114 .cmp(&y.significant_bits())
115 .then_with(|| sig_x.cmp_normalized(&Natural::from(y.unsigned_abs())))
116 }),
117 }
118}
119
120macro_rules! impl_from_signed {
121 ($t: ident) => {
122 impl PartialOrdAbs<$t> for Float {
123 /// Compares the absolute values of a [`Float`] and a signed primitive integer.
124 ///
125 /// NaN is not comparable to any primitive integer. $\infty$ and $-\infty$ are greater
126 /// in absolute value than any primitive integer. Both the [`Float`] zero and the
127 /// [`Float`] negative zero are equal to the integer zero.
128 ///
129 /// # Worst-case complexity
130 /// $T(n) = O(n)$
131 ///
132 /// $M(n) = O(1)$
133 ///
134 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
135 ///
136 /// # Examples
137 /// See [here](super::partial_cmp_abs_primitive_int#partial_cmp_abs).
138 #[inline]
139 fn partial_cmp_abs(&self, other: &$t) -> Option<Ordering> {
140 float_partial_cmp_abs_signed(self, other)
141 }
142 }
143
144 impl PartialOrdAbs<Float> for $t {
145 /// Compares the absolute values of a signed primitive integer and a [`Float`].
146 ///
147 /// No primitive integer is comparable to NaN. Every primitive integer is smaller in
148 /// absolute value than $\infty$ and $-\infty$. The integer zero is equal to both the
149 /// [`Float`] zero and the [`Float`] negative zero.
150 ///
151 /// # Worst-case complexity
152 /// $T(n) = O(n)$
153 ///
154 /// $M(n) = O(1)$
155 ///
156 /// where $T$ is time, $M$ is additional memory, and $n$ is `other.significant_bits()`.
157 ///
158 /// See [here](super::partial_cmp_abs_primitive_int#partial_cmp_abs).
159 #[inline]
160 fn partial_cmp_abs(&self, other: &Float) -> Option<Ordering> {
161 other.partial_cmp_abs(self).map(Ordering::reverse)
162 }
163 }
164 };
165}
166apply_to_signeds!(impl_from_signed);