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