malachite_float/comparison/partial_eq_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::logic::traits::SignificantBits;
13use malachite_nz::natural::Natural;
14
15impl PartialEq<Natural> for Float {
16 /// Determines whether a [`Float`] is equal to a [`Natural`].
17 ///
18 /// $\infty$, $-\infty$, and NaN are not equal to any [`Natural`]. Both the [`Float`] zero and
19 /// the [`Float`] negative zero are equal to the [`Natural`] zero.
20 ///
21 /// # Worst-case complexity
22 /// $T(n) = O(n)$
23 ///
24 /// $M(n) = O(1)$
25 ///
26 /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
27 /// other.significant_bits())`.
28 ///
29 /// # Examples
30 /// ```
31 /// use malachite_base::num::basic::traits::{One, OneHalf};
32 /// use malachite_float::Float;
33 /// use malachite_nz::natural::Natural;
34 ///
35 /// assert!(Float::from(123) == Natural::from(123u32));
36 /// assert!(Float::ONE_HALF != Natural::ONE);
37 /// ```
38 fn eq(&self, other: &Natural) -> bool {
39 match self {
40 float_either_zero!() => *other == 0u32,
41 Self(Finite {
42 sign,
43 exponent,
44 significand,
45 ..
46 }) => {
47 *sign
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 PartialEq<Float> for Natural {
59 /// Determines whether a [`Natural`] is equal to 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_float::Float;
76 /// use malachite_nz::natural::Natural;
77 ///
78 /// assert!(Natural::from(123u32) == Float::from(123));
79 /// assert!(Natural::ONE != Float::ONE_HALF);
80 /// ```
81 #[inline]
82 fn eq(&self, other: &Float) -> bool {
83 other == self
84 }
85}