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