malachite_float/comparison/
partial_cmp_abs_integer.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::comparison::traits::PartialOrdAbs;
13use malachite_base::num::logic::traits::SignificantBits;
14use malachite_nz::integer::Integer;
15
16impl PartialOrdAbs<Integer> for Float {
17    /// Compares the absolute values of a [`Float`] and an [`Integer`].
18    ///
19    /// NaN is not comparable to any [`Integer`]. $\infty$ and $-\infty$ are greater in absolute
20    /// value than any [`Integer`]. Both the [`Float`] zero and the [`Float`] negative zero are
21    /// equal to the [`Integer`] zero.
22    ///
23    /// # Worst-case complexity
24    /// $T(n) = O(n)$
25    ///
26    /// $M(n) = O(1)$
27    ///
28    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
29    /// other.significant_bits())`.
30    ///
31    /// # Examples
32    /// ```
33    /// use malachite_base::num::basic::traits::{Infinity, NegativeInfinity};
34    /// use malachite_base::num::comparison::traits::PartialOrdAbs;
35    /// use malachite_float::Float;
36    /// use malachite_nz::integer::Integer;
37    ///
38    /// assert!(Float::from(80).lt_abs(&Integer::from(100)));
39    /// assert!(Float::from(-80).lt_abs(&Integer::from(-100)));
40    /// assert!(Float::INFINITY.gt_abs(&Integer::from(100)));
41    /// assert!(Float::NEGATIVE_INFINITY.gt_abs(&Integer::from(-100)));
42    /// ```
43    fn partial_cmp_abs(&self, other: &Integer) -> Option<Ordering> {
44        match (self, other) {
45            (float_nan!(), _) => None,
46            (Self(Infinity { .. }), _) => Some(Greater),
47            (float_either_zero!(), y) => Some(if *y == 0 { Equal } else { Less }),
48            (
49                Self(Finite {
50                    exponent: e_x,
51                    significand: x,
52                    ..
53                }),
54                y,
55            ) => Some(if *other == 0 {
56                Greater
57            } else if *e_x <= 0 {
58                Less
59            } else {
60                u64::from(e_x.unsigned_abs())
61                    .cmp(&other.significant_bits())
62                    .then_with(|| x.cmp_normalized(y.unsigned_abs_ref()))
63            }),
64        }
65    }
66}
67
68impl PartialOrdAbs<Float> for Integer {
69    /// Compares the absolute values of an [`Integer`] and a [`Float`].
70    ///
71    /// No [`Integer`] is comparable to NaN. Every [`Integer`] is smaller in absolute value than
72    /// $\infty$ and $-\infty$. The [`Integer`] zero is equal to both the [`Float`] zero and the
73    /// [`Float`] negative zero.
74    ///
75    /// # Worst-case complexity
76    /// $T(n) = O(n)$
77    ///
78    /// $M(n) = O(1)$
79    ///
80    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
81    /// other.significant_bits())`.
82    ///
83    /// # Examples
84    /// ```
85    /// use malachite_base::num::basic::traits::{Infinity, NegativeInfinity};
86    /// use malachite_base::num::comparison::traits::PartialOrdAbs;
87    /// use malachite_float::Float;
88    /// use malachite_nz::integer::Integer;
89    ///
90    /// assert!(Integer::from(100).gt_abs(&Float::from(80)));
91    /// assert!(Integer::from(100).lt_abs(&Float::INFINITY));
92    /// assert!(Integer::from(-100).lt_abs(&Float::INFINITY));
93    /// assert!(Integer::from(-100).lt_abs(&Float::NEGATIVE_INFINITY));
94    /// ```
95    #[inline]
96    fn partial_cmp_abs(&self, other: &Float) -> Option<Ordering> {
97        other.partial_cmp_abs(self).map(Ordering::reverse)
98    }
99}