malachite_float/float/conversion/from_gaussian_rational.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::Float;
10use malachite_base::num::conversion::traits::ConvertibleFrom;
11use malachite_q::gaussian_rational::GaussianRational;
12
13#[derive(Clone, Copy, Debug, Eq, PartialEq)]
14pub struct FloatFromGaussianRationalError;
15
16impl TryFrom<GaussianRational> for Float {
17 type Error = FloatFromGaussianRationalError;
18
19 /// Converts a [`GaussianRational`] to a [`Float`], taking the [`GaussianRational`] by value. If
20 /// the [`GaussianRational`] is not real or is not exactly representable as a dyadic rational,
21 /// an error is returned.
22 ///
23 /// If the [`GaussianRational`] is nonzero, the precision of the [`Float`] is the minimum
24 /// possible precision to represent it exactly.
25 ///
26 /// # Worst-case complexity
27 /// $T(n) = O(n)$
28 ///
29 /// $M(n) = O(n)$
30 ///
31 /// where $T$ is time, $M$ is additional memory, and $n$ is `x.real.significant_bits()`.
32 ///
33 /// # Examples
34 /// ```
35 /// use malachite_float::Float;
36 /// use malachite_float::float::conversion::from_gaussian_rational::*;
37 /// use malachite_q::gaussian_rational::GaussianRational;
38 /// use std::str::FromStr;
39 ///
40 /// let x = GaussianRational::from_str("5/2").unwrap();
41 /// assert_eq!(Float::try_from(x).unwrap().to_string(), "2.5");
42 ///
43 /// let x = GaussianRational::from_str("1/3").unwrap();
44 /// assert_eq!(Float::try_from(x), Err(FloatFromGaussianRationalError));
45 ///
46 /// let x = GaussianRational::from_str("2-3i").unwrap();
47 /// assert_eq!(Float::try_from(x), Err(FloatFromGaussianRationalError));
48 /// ```
49 fn try_from(x: GaussianRational) -> Result<Self, Self::Error> {
50 if x.imaginary == 0u32 {
51 Self::try_from(x.real).map_err(|_| FloatFromGaussianRationalError)
52 } else {
53 Err(FloatFromGaussianRationalError)
54 }
55 }
56}
57
58impl TryFrom<&GaussianRational> for Float {
59 type Error = FloatFromGaussianRationalError;
60
61 /// Converts a [`GaussianRational`] to a [`Float`], taking the [`GaussianRational`] by
62 /// reference. If the [`GaussianRational`] is not real or is not exactly representable as a
63 /// dyadic rational, an error is returned.
64 ///
65 /// If the [`GaussianRational`] is nonzero, the precision of the [`Float`] is the minimum
66 /// possible precision to represent it exactly.
67 ///
68 /// # Worst-case complexity
69 /// $T(n) = O(n)$
70 ///
71 /// $M(n) = O(n)$
72 ///
73 /// where $T$ is time, $M$ is additional memory, and $n$ is `x.real.significant_bits()`.
74 ///
75 /// # Examples
76 /// ```
77 /// use malachite_float::Float;
78 /// use malachite_float::float::conversion::from_gaussian_rational::*;
79 /// use malachite_q::gaussian_rational::GaussianRational;
80 /// use std::str::FromStr;
81 ///
82 /// let x = GaussianRational::from_str("5/2").unwrap();
83 /// assert_eq!(Float::try_from(&x).unwrap().to_string(), "2.5");
84 ///
85 /// let x = GaussianRational::from_str("1/3").unwrap();
86 /// assert_eq!(Float::try_from(&x), Err(FloatFromGaussianRationalError));
87 ///
88 /// let x = GaussianRational::from_str("2-3i").unwrap();
89 /// assert_eq!(Float::try_from(&x), Err(FloatFromGaussianRationalError));
90 /// ```
91 fn try_from(x: &GaussianRational) -> Result<Self, Self::Error> {
92 if x.imaginary == 0u32 {
93 Self::try_from(&x.real).map_err(|_| FloatFromGaussianRationalError)
94 } else {
95 Err(FloatFromGaussianRationalError)
96 }
97 }
98}
99
100impl ConvertibleFrom<&GaussianRational> for Float {
101 /// Determines whether a [`GaussianRational`] can be converted to a [`Float`] (that is, whether
102 /// it is real or is not exactly representable as a dyadic rational), taking the
103 /// [`GaussianRational`] by reference.
104 ///
105 /// # Worst-case complexity
106 /// $T(n) = O(n)$
107 ///
108 /// $M(n) = O(1)$
109 ///
110 /// where $T$ is time, $M$ is additional memory, and $n$ is `x.real.significant_bits()`.
111 ///
112 /// # Examples
113 /// ```
114 /// use malachite_base::num::conversion::traits::ConvertibleFrom;
115 /// use malachite_float::Float;
116 /// use malachite_q::gaussian_rational::GaussianRational;
117 /// use std::str::FromStr;
118 ///
119 /// let x = GaussianRational::from_str("5/2").unwrap();
120 /// assert_eq!(Float::convertible_from(&x), true);
121 ///
122 /// let x = GaussianRational::from_str("1/3").unwrap();
123 /// assert_eq!(Float::convertible_from(&x), false);
124 ///
125 /// let x = GaussianRational::from_str("2-3i").unwrap();
126 /// assert_eq!(Float::convertible_from(&x), false);
127 /// ```
128 #[inline]
129 fn convertible_from(x: &GaussianRational) -> bool {
130 x.imaginary == 0u32 && Self::convertible_from(&x.real)
131 }
132}