malachite_nz/integer/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::integer::Integer;
10use core::cmp::Ordering;
11use malachite_base::num::arithmetic::traits::UnsignedAbs;
12
13macro_rules! impl_float {
14    ($t: ident) => {
15        impl PartialOrd<$t> for Integer {
16            /// Compares an [`Integer`] to a primitive float.
17            ///
18            /// # Worst-case complexity
19            /// $T(n) = O(n)$
20            ///
21            /// $M(n) = O(1)$
22            ///
23            /// where n = `self.significant_bits()`.
24            ///
25            /// # Examples
26            /// See [here](super::partial_cmp_primitive_float#partial_cmp).
27            fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
28                if self.sign {
29                    self.unsigned_abs().partial_cmp(other)
30                } else {
31                    self.unsigned_abs()
32                        .partial_cmp(&-other)
33                        .map(Ordering::reverse)
34                }
35            }
36        }
37
38        impl PartialOrd<Integer> for $t {
39            /// Compares a primitive float to an [`Integer`].
40            ///
41            /// # Worst-case complexity
42            /// $T(n) = O(n)$
43            ///
44            /// $M(n) = O(1)$
45            ///
46            /// where n = `other.significant_bits()`
47            ///
48            /// # Examples
49            /// See [here](super::partial_cmp_primitive_float#partial_cmp).
50            #[inline]
51            fn partial_cmp(&self, other: &Integer) -> Option<Ordering> {
52                other.partial_cmp(self).map(Ordering::reverse)
53            }
54        }
55    };
56}
57apply_to_primitive_floats!(impl_float);