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