1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright © 2026 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
/// Implementations of [`From`] for converting values to a
/// [`GaussianInteger`](crate::gaussian_integer::GaussianInteger).
/// Implementations of traits for converting a primitive float to a
/// [`GaussianInteger`](crate::gaussian_integer::GaussianInteger).
///
/// The traits are [`TryFrom`] and
/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom).
///
/// # try_from
/// ```
/// use malachite_nz::gaussian_integer::GaussianInteger;
/// use malachite_nz::gaussian_integer::conversion::from_primitive_float::*;
///
/// assert_eq!(
/// GaussianInteger::try_from(123.0f32).unwrap().to_string(),
/// "123"
/// );
/// assert_eq!(
/// GaussianInteger::try_from(-123.0f64).unwrap().to_string(),
/// "-123"
/// );
///
/// assert_eq!(
/// GaussianInteger::try_from(0.5f32),
/// Err(GaussianIntegerFromPrimitiveFloatError)
/// );
/// assert_eq!(
/// GaussianInteger::try_from(f64::NAN),
/// Err(GaussianIntegerFromPrimitiveFloatError)
/// );
/// assert_eq!(
/// GaussianInteger::try_from(f64::INFINITY),
/// Err(GaussianIntegerFromPrimitiveFloatError)
/// );
/// ```
///
/// # convertible_from
/// ```
/// use malachite_base::num::conversion::traits::ConvertibleFrom;
/// use malachite_nz::gaussian_integer::GaussianInteger;
///
/// assert_eq!(GaussianInteger::convertible_from(123.0f32), true);
/// assert_eq!(GaussianInteger::convertible_from(-123.0f64), true);
/// assert_eq!(GaussianInteger::convertible_from(0.5f32), false);
/// assert_eq!(GaussianInteger::convertible_from(f64::NAN), false);
/// assert_eq!(GaussianInteger::convertible_from(f64::INFINITY), false);
/// ```
/// Implementations of [`ImaginaryFrom`](malachite_base::num::conversion::traits::ImaginaryFrom) for
/// converting values to purely imaginary
/// [`GaussianInteger`](crate::gaussian_integer::GaussianInteger)s.
/// Implementations of traits for converting a
/// [`GaussianInteger`](crate::gaussian_integer::GaussianInteger) to an
/// [`Integer`](crate::integer::Integer).
///
/// The traits are [`TryFrom`] and
/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom).
/// An implementation of
/// [`IsGaussianInteger`](malachite_base::num::conversion::traits::IsGaussianInteger), a trait for
/// determining whether a number is a Gaussian integer.
///
/// A [`GaussianInteger`](crate::gaussian_integer::GaussianInteger) is always a Gaussian integer.
/// An implementation of [`IsInteger`](malachite_base::num::conversion::traits::IsInteger), a trait
/// for determining whether a number is an integer.
///
/// A [`GaussianInteger`](crate::gaussian_integer::GaussianInteger) is an integer if and only if its
/// imaginary part is zero.
/// An implementation of [`IsReal`](malachite_base::num::conversion::traits::IsReal), a trait for
/// determining whether a number is a real number.
///
/// A [`GaussianInteger`](crate::gaussian_integer::GaussianInteger) is a real number if and only if
/// its imaginary part is zero.
/// Implementations of traits for converting a
/// [`GaussianInteger`](crate::gaussian_integer::GaussianInteger) to a
/// [`Natural`](crate::natural::Natural).
///
/// The traits are [`TryFrom`] and
/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom).
/// Implementations of traits for converting a
/// [`GaussianInteger`](crate::gaussian_integer::GaussianInteger) to a primitive float.
///
/// The traits are [`TryFrom`] and
/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom).
///
/// # try_from
/// ```
/// use malachite_nz::gaussian_integer::GaussianInteger;
/// use malachite_nz::gaussian_integer::conversion::primitive_float_from_gaussian_integer::*;
/// use std::str::FromStr;
///
/// let x = GaussianInteger::from_str("123").unwrap();
/// assert_eq!(f32::try_from(&x).unwrap(), 123.0);
///
/// let x = GaussianInteger::from_str("-123").unwrap();
/// assert_eq!(f32::try_from(&x).unwrap(), -123.0);
///
/// let x = GaussianInteger::from_str("16777217").unwrap();
/// assert_eq!(
/// f32::try_from(&x),
/// Err(PrimitiveFloatFromGaussianIntegerError)
/// );
/// assert_eq!(f64::try_from(&x).unwrap(), 16777217.0);
///
/// let x = GaussianInteger::from_str("2-3i").unwrap();
/// assert_eq!(
/// f32::try_from(&x),
/// Err(PrimitiveFloatFromGaussianIntegerError)
/// );
/// ```
///
/// # convertible_from
/// ```
/// use malachite_base::num::conversion::traits::ConvertibleFrom;
/// use malachite_nz::gaussian_integer::GaussianInteger;
/// use std::str::FromStr;
///
/// let x = GaussianInteger::from_str("123").unwrap();
/// assert_eq!(f32::convertible_from(&x), true);
///
/// let x = GaussianInteger::from_str("16777217").unwrap();
/// assert_eq!(f32::convertible_from(&x), false);
/// assert_eq!(f64::convertible_from(&x), true);
///
/// let x = GaussianInteger::from_str("2-3i").unwrap();
/// assert_eq!(f32::convertible_from(&x), false);
/// ```
/// Implementations of traits for converting a
/// [`GaussianInteger`](crate::gaussian_integer::GaussianInteger) to a primitive integer.
///
/// The traits are [`TryFrom`] and
/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom).
///
/// # try_from
/// ```
/// use malachite_nz::gaussian_integer::GaussianInteger;
/// use malachite_nz::gaussian_integer::conversion::primitive_int_from_gaussian_integer::*;
/// use std::str::FromStr;
///
/// let x = GaussianInteger::from_str("123").unwrap();
/// assert_eq!(u32::try_from(&x).unwrap(), 123);
/// assert_eq!(i32::try_from(&x).unwrap(), 123);
///
/// let x = GaussianInteger::from_str("-123").unwrap();
/// assert_eq!(u32::try_from(&x), Err(PrimitiveIntFromGaussianIntegerError));
/// assert_eq!(i32::try_from(&x).unwrap(), -123);
///
/// let x = GaussianInteger::from_str("1000000000000").unwrap();
/// assert_eq!(u32::try_from(&x), Err(PrimitiveIntFromGaussianIntegerError));
/// assert_eq!(u64::try_from(&x).unwrap(), 1000000000000);
///
/// let x = GaussianInteger::from_str("2-3i").unwrap();
/// assert_eq!(u32::try_from(&x), Err(PrimitiveIntFromGaussianIntegerError));
/// assert_eq!(i32::try_from(&x), Err(PrimitiveIntFromGaussianIntegerError));
/// ```
///
/// # convertible_from
/// ```
/// use malachite_base::num::conversion::traits::ConvertibleFrom;
/// use malachite_nz::gaussian_integer::GaussianInteger;
/// use std::str::FromStr;
///
/// let x = GaussianInteger::from_str("123").unwrap();
/// assert_eq!(u32::convertible_from(&x), true);
/// assert_eq!(i32::convertible_from(&x), true);
///
/// let x = GaussianInteger::from_str("-123").unwrap();
/// assert_eq!(u32::convertible_from(&x), false);
/// assert_eq!(i32::convertible_from(&x), true);
///
/// let x = GaussianInteger::from_str("1000000000000").unwrap();
/// assert_eq!(u32::convertible_from(&x), false);
/// assert_eq!(u64::convertible_from(&x), true);
///
/// let x = GaussianInteger::from_str("2-3i").unwrap();
/// assert_eq!(u32::convertible_from(&x), false);
/// assert_eq!(i32::convertible_from(&x), false);
/// ```
/// Conversions to and from strings.