malachite_float/comparison/
eq_abs_natural.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, Zero};
11use core::cmp::Ordering::*;
12use malachite_base::num::comparison::traits::EqAbs;
13use malachite_base::num::logic::traits::SignificantBits;
14use malachite_nz::natural::Natural;
15
16impl EqAbs<Natural> for Float {
17    /// Determines whether the absolute value of a [`Float`] is equal to a [`Natural`].
18    ///
19    /// $\infty$, $-\infty$, and NaN are not equal to any [`Natural`]. Both the [`Float`] zero and
20    /// the [`Float`] negative zero are equal to the [`Natural`] zero.
21    ///
22    /// # Worst-case complexity
23    /// $T(n) = O(n)$
24    ///
25    /// $M(n) = O(1)$
26    ///
27    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
28    /// other.significant_bits())`.
29    ///
30    /// # Examples
31    /// ```
32    /// use malachite_base::num::basic::traits::{One, OneHalf};
33    /// use malachite_base::num::comparison::traits::EqAbs;
34    /// use malachite_float::Float;
35    /// use malachite_nz::natural::Natural;
36    ///
37    /// assert!(Float::from(123).eq_abs(&Natural::from(123u32)));
38    /// assert!(Float::ONE_HALF.ne_abs(&Natural::ONE));
39    /// ```
40    fn eq_abs(&self, other: &Natural) -> bool {
41        match self {
42            float_either_zero!() => *other == 0u32,
43            Self(Finite {
44                exponent,
45                significand,
46                ..
47            }) => {
48                *other != 0u32
49                    && *exponent >= 0
50                    && other.significant_bits() == u64::from(exponent.unsigned_abs())
51                    && significand.cmp_normalized(other) == Equal
52            }
53            _ => false,
54        }
55    }
56}
57
58impl EqAbs<Float> for Natural {
59    /// Determines whether a [`Natural`] is equal to the absolute value of a [`Float`].
60    ///
61    /// No [`Natural`] is equal to $\infty$, $-\infty$, or NaN. The [`Natural`] zero is equal to
62    /// both the [`Float`] zero and the [`Float`] negative zero.
63    ///
64    /// # Worst-case complexity
65    /// $T(n) = O(n)$
66    ///
67    /// $M(n) = O(1)$
68    ///
69    /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
70    /// other.significant_bits())`.
71    ///
72    /// # Examples
73    /// ```
74    /// use malachite_base::num::basic::traits::{One, OneHalf};
75    /// use malachite_base::num::comparison::traits::EqAbs;
76    /// use malachite_float::Float;
77    /// use malachite_nz::natural::Natural;
78    ///
79    /// assert!(Natural::from(123u32).eq_abs(&Float::from(123)));
80    /// assert!(Natural::ONE.ne_abs(&Float::ONE_HALF));
81    /// ```
82    #[inline]
83    fn eq_abs(&self, other: &Float) -> bool {
84        other.eq_abs(self)
85    }
86}