malachite_float/comparison/eq_abs_primitive_float.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::*;
12use malachite_base::num::basic::floats::PrimitiveFloat;
13use malachite_base::num::comparison::traits::EqAbs;
14use malachite_nz::natural::Natural;
15
16fn float_eq_abs_primitive_float<T: PrimitiveFloat>(x: &Float, y: &T) -> bool {
17 match x {
18 float_nan!() => false,
19 float_either_infinity!() => *y == T::INFINITY || *y == T::NEGATIVE_INFINITY,
20 float_either_zero!() => *y == T::ZERO,
21 Float(Finite {
22 exponent,
23 significand,
24 ..
25 }) => {
26 y.is_finite()
27 && *y != T::ZERO
28 && i64::from(*exponent - 1) == y.sci_exponent()
29 && significand.cmp_normalized(&Natural::from(y.integer_mantissa())) == Equal
30 }
31 }
32}
33
34macro_rules! impl_eq_abs_primitive_float {
35 ($t: ident) => {
36 impl EqAbs<$t> for Float {
37 /// Determines whether the absolute value of a [`Float`] is equal to the absolute value
38 /// of a primitive float.
39 ///
40 /// The [`Float`] $\infty$ is equal to the primitive float $\infty$, and the [`Float`]
41 /// $-\infty$ is equal to the primitive float $-\infty$. The [`Float`] NaN is not equal
42 /// to anything, not even the primitive float NaN. Every [`Float`] zero is equal to
43 /// every primitive float zero, regardless of sign.
44 ///
45 /// # Worst-case complexity
46 /// $T(n) = O(n)$
47 ///
48 /// $M(n) = O(1)$
49 ///
50 /// where $T$ is time, $M$ is additional memory, and $n$ is
51 /// `max(self.significant_bits(), other.sci_exponent().abs())`.
52 ///
53 /// # Examples
54 /// See [here](super::eq_abs_primitive_float#eq_abs).
55 #[inline]
56 fn eq_abs(&self, other: &$t) -> bool {
57 float_eq_abs_primitive_float(self, other)
58 }
59 }
60
61 impl EqAbs<Float> for $t {
62 /// Determines whether a primitive float is equal to a [`Float`].
63 ///
64 /// The primitive float $\infty$ is equal to the [`Float`] $\infty$, and the primitive
65 /// float $-\infty$ is equal to the [`Float`] $-\infty$. The primitive float NaN is not
66 /// equal to anything, not even the [`Float`] NaN. Every primitive float zero is equal
67 /// to every [`Float`] zero, regardless of sign.
68 ///
69 /// # Worst-case complexity
70 /// $T(n) = O(n)$
71 ///
72 /// $M(n) = O(1)$
73 ///
74 /// where $T$ is time, $M$ is additional memory, and $n$ is
75 /// `max(self.significant_bits(), other.sci_exponent().abs())`.
76 ///
77 /// # Examples
78 /// See [here](super::eq_abs_primitive_float#eq_abs).
79 #[inline]
80 fn eq_abs(&self, other: &Float) -> bool {
81 other.eq_abs(self)
82 }
83 }
84 };
85}
86apply_to_primitive_floats!(impl_eq_abs_primitive_float);