Skip to main content

malachite_nz/gaussian_integer/conversion/
primitive_float_from_gaussian_integer.rs

1// Copyright © 2026 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::gaussian_integer::GaussianInteger;
10use crate::integer::Integer;
11use malachite_base::num::conversion::traits::ConvertibleFrom;
12
13#[derive(Clone, Copy, Debug, Eq, PartialEq)]
14pub struct PrimitiveFloatFromGaussianIntegerError;
15
16fn try_from_helper<'a, T: TryFrom<&'a Integer>>(
17    x: &'a GaussianInteger,
18) -> Result<T, PrimitiveFloatFromGaussianIntegerError> {
19    if x.imaginary == 0u32 {
20        T::try_from(&x.real).map_err(|_| PrimitiveFloatFromGaussianIntegerError)
21    } else {
22        Err(PrimitiveFloatFromGaussianIntegerError)
23    }
24}
25
26fn convertible_from_helper<T: for<'a> ConvertibleFrom<&'a Integer>>(x: &GaussianInteger) -> bool {
27    x.imaginary == 0u32 && T::convertible_from(&x.real)
28}
29
30macro_rules! float_impls {
31    ($f: ident) => {
32        impl TryFrom<&GaussianInteger> for $f {
33            type Error = PrimitiveFloatFromGaussianIntegerError;
34
35            /// Converts a [`GaussianInteger`] to a primitive float, returning an error if the
36            /// [`GaussianInteger`] is not real or isn't exactly equal to some float.
37            ///
38            /// # Worst-case complexity
39            /// $T(n) = O(n)$
40            ///
41            /// $M(n) = O(1)$
42            ///
43            /// where $T$ is time, $M$ is additional memory, and $n$ is `x.real.significant_bits()`.
44            ///
45            /// # Examples
46            /// See [here](super::primitive_float_from_gaussian_integer#try_from).
47            #[inline]
48            fn try_from(x: &GaussianInteger) -> Result<$f, Self::Error> {
49                try_from_helper(x)
50            }
51        }
52
53        impl ConvertibleFrom<&GaussianInteger> for $f {
54            /// Determines whether a [`GaussianInteger`] can be converted to a primitive float (that
55            /// is, whether it is real and exactly equal to some float), taking the
56            /// [`GaussianInteger`] by reference.
57            ///
58            /// # Worst-case complexity
59            /// $T(n) = O(n)$
60            ///
61            /// $M(n) = O(1)$
62            ///
63            /// where $T$ is time, $M$ is additional memory, and $n$ is `x.real.significant_bits()`.
64            ///
65            /// # Examples
66            /// See [here](super::primitive_float_from_gaussian_integer#convertible_from).
67            #[inline]
68            fn convertible_from(x: &GaussianInteger) -> bool {
69                convertible_from_helper::<$f>(x)
70            }
71        }
72    };
73}
74apply_to_primitive_floats!(float_impls);