Skip to main content

malachite_float/float/conversion/
mod.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
9/// Functions for creating a [`Float`](crate::Float) from an infinite iterator of bits.
10pub mod from_bits;
11/// Functions for constructing a [`Float`](crate::Float) from a sequence of digits in an arbitrary
12/// base.
13pub mod from_digits;
14/// Implementations of traits for converting a
15/// [`GaussianInteger`](malachite_nz::gaussian_integer::GaussianInteger) to a
16/// [`Float`](crate::Float).
17///
18/// The traits are [`TryFrom`] and
19/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom).
20pub mod from_gaussian_integer;
21/// Implementations of traits for converting a
22/// [`GaussianRational`](malachite_q::gaussian_rational::GaussianRational) to a
23/// [`Float`](crate::Float).
24///
25/// The traits are [`TryFrom`] and
26/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom).
27pub mod from_gaussian_rational;
28/// Implementations of the [`From`] trait for converting an
29/// [`Integer`](malachite_nz::integer::Integer) to a [`Float`](crate::Float).
30pub mod from_integer;
31/// Implementations of the [`From`] trait for converting an
32/// [`Natural`](malachite_nz::natural::Natural) to a [`Float`](crate::Float).
33pub mod from_natural;
34/// Various functions and implementations of the [`From`] trait for converting a primitive float to
35/// a [`Float`](crate::Float).
36///
37/// # from
38/// ```
39/// use malachite_base::num::basic::traits::NegativeInfinity;
40/// use malachite_float::Float;
41///
42/// assert_eq!(Float::from(f64::NAN).to_string(), "NaN");
43/// assert_eq!(Float::from(f64::INFINITY).to_string(), "Infinity");
44/// assert_eq!(Float::from(f64::NEGATIVE_INFINITY).to_string(), "-Infinity");
45/// assert_eq!(Float::from(0.0).to_string(), "0.0");
46/// assert_eq!(Float::from(-0.0).to_string(), "-0.0");
47/// assert_eq!(Float::from(123.0).to_string(), "123.0");
48/// assert_eq!(Float::from(-123.0).to_string(), "-123.0");
49/// ```
50///
51/// # from_primitive_float_prec
52/// ```
53/// use malachite_float::Float;
54/// use std::cmp::Ordering::*;
55///
56/// let (x, o) = Float::from_primitive_float_prec(f64::NAN, 4);
57/// assert_eq!(x.to_string(), "NaN");
58/// assert_eq!(o, Equal);
59///
60/// let (x, o) = Float::from_primitive_float_prec(1.0 / 3.0, 4);
61/// assert_eq!(x.to_string(), "0.344");
62/// assert_eq!(o, Greater);
63///
64/// let (x, o) = Float::from_primitive_float_prec(123.0, 4);
65/// assert_eq!(x.to_string(), "120.0");
66/// assert_eq!(o, Less);
67/// ```
68///
69/// # from_primitive_float_prec_round
70/// ```
71/// use malachite_base::rounding_modes::RoundingMode::*;
72/// use malachite_float::Float;
73/// use std::cmp::Ordering::*;
74///
75/// let (x, o) = Float::from_primitive_float_prec_round(f64::NAN, 4, Floor);
76/// assert_eq!(x.to_string(), "NaN");
77/// assert_eq!(o, Equal);
78///
79/// let (x, o) = Float::from_primitive_float_prec_round(1.0 / 3.0, 4, Floor);
80/// assert_eq!(x.to_string(), "0.312");
81/// assert_eq!(o, Less);
82///
83/// let (x, o) = Float::from_primitive_float_prec_round(1.0 / 3.0, 4, Ceiling);
84/// assert_eq!(x.to_string(), "0.344");
85/// assert_eq!(o, Greater);
86///
87/// let (x, o) = Float::from_primitive_float_prec_round(1.0 / 3.0, 4, Nearest);
88/// assert_eq!(x.to_string(), "0.344");
89/// assert_eq!(o, Greater);
90/// ```
91pub mod from_primitive_float;
92/// Various functions and implementations of the [`From`] trait for converting a primitive integer
93/// to a [`Float`](crate::Float).
94///
95/// # from
96/// ```
97/// use malachite_float::Float;
98///
99/// assert_eq!(Float::from(0u32).to_string(), "0.0");
100/// assert_eq!(Float::from(123u32).to_string(), "123.0");
101/// assert_eq!(Float::from(123u32).get_prec(), Some(7));
102///
103/// assert_eq!(Float::from(0i32).to_string(), "0.0");
104/// assert_eq!(Float::from(123i32).to_string(), "123.0");
105/// assert_eq!(Float::from(123i32).get_prec(), Some(7));
106/// assert_eq!(Float::from(-123i32).to_string(), "-123.0");
107/// ```
108///
109/// # from_unsigned_prec
110/// ```
111/// use malachite_float::Float;
112/// use std::cmp::Ordering::*;
113///
114/// let (x, o) = Float::from_unsigned_prec(0u32, 10);
115/// assert_eq!(x.to_string(), "0.0");
116/// assert_eq!(o, Equal);
117///
118/// let (x, o) = Float::from_unsigned_prec(123u32, 20);
119/// assert_eq!(x.to_string(), "123.00000");
120/// assert_eq!(x.get_prec(), Some(20));
121/// assert_eq!(o, Equal);
122///
123/// let (x, o) = Float::from_unsigned_prec(123u32, 4);
124/// assert_eq!(x.to_string(), "120.0");
125/// assert_eq!(x.get_prec(), Some(4));
126/// assert_eq!(o, Less);
127/// ```
128///
129/// # from_signed_prec
130/// ```
131/// use malachite_float::Float;
132/// use std::cmp::Ordering::*;
133///
134/// let (x, o) = Float::from_signed_prec(0i32, 10);
135/// assert_eq!(x.to_string(), "0.0");
136/// assert_eq!(o, Equal);
137///
138/// let (x, o) = Float::from_signed_prec(123i32, 20);
139/// assert_eq!(x.to_string(), "123.00000");
140/// assert_eq!(x.get_prec(), Some(20));
141/// assert_eq!(o, Equal);
142///
143/// let (x, o) = Float::from_signed_prec(123i32, 4);
144/// assert_eq!(x.to_string(), "120.0");
145/// assert_eq!(x.get_prec(), Some(4));
146/// assert_eq!(o, Less);
147///
148/// let (x, o) = Float::from_signed_prec(-123i32, 20);
149/// assert_eq!(x.to_string(), "-123.00000");
150/// assert_eq!(x.get_prec(), Some(20));
151/// assert_eq!(o, Equal);
152///
153/// let (x, o) = Float::from_signed_prec(-123i32, 4);
154/// assert_eq!(x.to_string(), "-120.0");
155/// assert_eq!(x.get_prec(), Some(4));
156/// assert_eq!(o, Greater);
157/// ```
158///
159/// # from_unsigned_prec_round
160/// ```
161/// use malachite_base::rounding_modes::RoundingMode::*;
162/// use malachite_float::Float;
163/// use std::cmp::Ordering::*;
164///
165/// let (x, o) = Float::from_unsigned_prec_round(0u32, 10, Exact);
166/// assert_eq!(x.to_string(), "0.0");
167/// assert_eq!(o, Equal);
168///
169/// let (x, o) = Float::from_unsigned_prec_round(123u32, 20, Exact);
170/// assert_eq!(x.to_string(), "123.00000");
171/// assert_eq!(x.get_prec(), Some(20));
172/// assert_eq!(o, Equal);
173///
174/// let (x, o) = Float::from_unsigned_prec_round(123u32, 4, Floor);
175/// assert_eq!(x.to_string(), "120.0");
176/// assert_eq!(x.get_prec(), Some(4));
177/// assert_eq!(o, Less);
178///
179/// let (x, o) = Float::from_unsigned_prec_round(123u32, 4, Ceiling);
180/// assert_eq!(x.to_string(), "128.0");
181/// assert_eq!(x.get_prec(), Some(4));
182/// assert_eq!(o, Greater);
183/// ```
184///
185/// # from_signed_prec_round
186/// ```
187/// use malachite_base::rounding_modes::RoundingMode::*;
188/// use malachite_float::Float;
189/// use std::cmp::Ordering::*;
190///
191/// let (x, o) = Float::from_signed_prec_round(0i32, 10, Exact);
192/// assert_eq!(x.to_string(), "0.0");
193/// assert_eq!(o, Equal);
194///
195/// let (x, o) = Float::from_signed_prec_round(123i32, 20, Exact);
196/// assert_eq!(x.to_string(), "123.00000");
197/// assert_eq!(x.get_prec(), Some(20));
198/// assert_eq!(o, Equal);
199///
200/// let (x, o) = Float::from_signed_prec_round(123i32, 4, Floor);
201/// assert_eq!(x.to_string(), "120.0");
202/// assert_eq!(x.get_prec(), Some(4));
203/// assert_eq!(o, Less);
204///
205/// let (x, o) = Float::from_signed_prec_round(123i32, 4, Ceiling);
206/// assert_eq!(x.to_string(), "128.0");
207/// assert_eq!(x.get_prec(), Some(4));
208/// assert_eq!(o, Greater);
209///
210/// let (x, o) = Float::from_signed_prec_round(-123i32, 20, Exact);
211/// assert_eq!(x.to_string(), "-123.00000");
212/// assert_eq!(x.get_prec(), Some(20));
213/// assert_eq!(o, Equal);
214///
215/// let (x, o) = Float::from_signed_prec_round(-123i32, 4, Floor);
216/// assert_eq!(x.to_string(), "-128.0");
217/// assert_eq!(x.get_prec(), Some(4));
218/// assert_eq!(o, Less);
219///
220/// let (x, o) = Float::from_signed_prec_round(-123i32, 4, Ceiling);
221/// assert_eq!(x.to_string(), "-120.0");
222/// assert_eq!(x.get_prec(), Some(4));
223/// assert_eq!(o, Greater);
224/// ```
225pub mod from_primitive_int;
226/// Implementations of the [`From`] trait for converting a [`Rational`](malachite_q::Rational) to a
227/// [`Float`](crate::Float).
228pub mod from_rational;
229/// Implementations of traits for converting a [`Float`](crate::Float) to a
230/// [`GaussianInteger`](malachite_nz::gaussian_integer::GaussianInteger).
231///
232/// The traits are [`TryFrom`] and
233/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom).
234pub mod gaussian_integer_from_float;
235/// Implementations of traits for converting a [`Float`](crate::Float) to a
236/// [`GaussianRational`](malachite_q::gaussian_rational::GaussianRational).
237///
238/// The traits are [`TryFrom`] and
239/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom).
240pub mod gaussian_rational_from_float;
241/// Implementations of traits for converting a [`Float`](crate::Float) to an
242/// [`Integer`](malachite_nz::integer::Integer).
243///
244/// The traits are [`TryFrom`],
245/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom), and
246/// [`RoundingFrom`](malachite_base::num::conversion::traits::RoundingFrom).
247pub mod integer_from_float;
248/// An implementation of
249/// [`IsGaussianInteger`](malachite_base::num::conversion::traits::IsGaussianInteger), a trait for
250/// determining whether a number is a Gaussian integer.
251pub mod is_gaussian_integer;
252/// An implementation of [`IsInteger`](malachite_base::num::conversion::traits::IsInteger), a trait
253/// for determining whether a number is an integer.
254pub mod is_integer;
255/// An implementation of [`IsReal`](malachite_base::num::conversion::traits::IsReal), a trait for
256/// determining whether a number is a real number.
257///
258/// A [`Float`](crate::Float) is a real number unless it is `NaN` or infinite.
259pub mod is_real;
260/// Implementations of traits for converting [`Float`](crate::Float)s to and from
261/// mantissa-and-exponent representations.
262///
263/// The traits are
264/// [`RawMantissaAndExponent`](malachite_base::num::conversion::traits::RawMantissaAndExponent),
265/// [`IntegerMantissaAndExponent`](malachite_base::num::conversion::traits::IntegerMantissaAndExponent),
266/// and [`SciMantissaAndExponent`](malachite_base::num::conversion::traits::SciMantissaAndExponent).
267///
268/// Here are some examples of the macro-generated functions:
269///
270/// # sci_mantissa_and_exponent
271/// ```
272/// use malachite_base::num::arithmetic::traits::Pow;
273/// use malachite_base::num::basic::traits::One;
274/// use malachite_base::num::conversion::traits::{ExactFrom, SciMantissaAndExponent};
275/// use malachite_base::num::float::NiceFloat;
276/// use malachite_float::Float;
277/// use malachite_nz::natural::Natural;
278/// use malachite_q::Rational;
279///
280/// let (m, e): (f64, i32) = (&Float::ONE).sci_mantissa_and_exponent();
281/// assert_eq!(NiceFloat(m), NiceFloat(1.0));
282/// assert_eq!(e, 0);
283///
284/// let (m, e): (f64, i32) = (&Float::from(std::f64::consts::PI)).sci_mantissa_and_exponent();
285/// assert_eq!(NiceFloat(m), NiceFloat(std::f64::consts::FRAC_PI_2));
286/// assert_eq!(e, 1);
287///
288/// let (m, e): (f64, i32) =
289///     (&Float::exact_from(Natural::from(3u32).pow(50u64))).sci_mantissa_and_exponent();
290/// assert_eq!(NiceFloat(m), NiceFloat(1.187662594419065));
291/// assert_eq!(e, 79);
292///
293/// let (m, e): (f64, i32) = (&Float::from_rational_prec(Rational::from(3u32).pow(-50i64), 100).0)
294///     .sci_mantissa_and_exponent();
295/// assert_eq!(NiceFloat(m), NiceFloat(1.6839799530592128));
296/// assert_eq!(e, -80);
297/// ```
298///
299/// # from_sci_mantissa_and_exponent
300/// ```
301/// use malachite_base::num::conversion::traits::SciMantissaAndExponent;
302/// use malachite_float::Float;
303///
304/// assert_eq!(
305///     <&Float as SciMantissaAndExponent<f64, _, _>>::from_sci_mantissa_and_exponent(1.0, 0)
306///         .unwrap()
307///         .to_string(),
308///     "1.0"
309/// );
310/// assert_eq!(
311///     <&Float as SciMantissaAndExponent<f64, _, _>>::from_sci_mantissa_and_exponent(
312///         std::f64::consts::FRAC_PI_2,
313///         1
314///     )
315///     .unwrap()
316///     .to_string(),
317///     "3.1415926535897931"
318/// );
319/// assert_eq!(
320///     <&Float as SciMantissaAndExponent<f64, _, _>>::from_sci_mantissa_and_exponent(
321///         1.187662594419065,
322///         79
323///     )
324///     .unwrap()
325///     .to_string(),
326///     "7.1789798769185258e23"
327/// );
328/// assert_eq!(
329///     <&Float as SciMantissaAndExponent<f64, _, _>>::from_sci_mantissa_and_exponent(
330///         1.6839799530592128,
331///         -80
332///     )
333///     .unwrap()
334///     .to_string(),
335///     "1.3929555690985384e-24"
336/// );
337/// ```
338#[cfg_attr(dylint_lib = "malachite_lints", expect(long_lines))]
339pub mod mantissa_and_exponent;
340/// Implementations of traits for converting a [`Float`](crate::Float) to a
341/// [`Natural`](malachite_nz::natural::Natural).
342///
343/// The traits are [`TryFrom`],
344/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom), and
345/// [`RoundingFrom`](malachite_base::num::conversion::traits::RoundingFrom).
346pub mod natural_from_float;
347/// Functions and implementations of traits for converting a [`Float`](crate::Float) to a primitive
348/// float.
349///
350/// The traits are [`TryFrom`],
351/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom), and
352/// [`RoundingFrom`](malachite_base::num::conversion::traits::RoundingFrom).
353///
354/// # convertible_from
355/// ```
356/// use malachite_base::num::arithmetic::traits::PowerOf2;
357/// use malachite_base::num::basic::traits::{Infinity, NaN, Zero};
358/// use malachite_base::num::conversion::traits::ConvertibleFrom;
359/// use malachite_float::Float;
360///
361/// assert_eq!(f32::convertible_from(&Float::NAN), true);
362/// assert_eq!(f32::convertible_from(&Float::INFINITY), true);
363/// assert_eq!(f32::convertible_from(&Float::ZERO), true);
364/// assert_eq!(f32::convertible_from(&Float::from(1.5)), true);
365/// assert_eq!(f32::convertible_from(&Float::from(-1.5)), true);
366/// assert_eq!(f32::convertible_from(&Float::from(123.0)), true);
367/// assert_eq!(f32::convertible_from(&Float::from(-123.0)), true);
368///
369/// // Even though precision is high, the value is just 1.0 and can be converted
370/// assert_eq!(f32::convertible_from(&Float::one_prec(100)), true);
371///
372/// let mut x = Float::one_prec(40);
373/// x.increment();
374///
375/// // precision too high for f32
376/// assert_eq!(f32::convertible_from(&x), false);
377///
378/// // but not for f64
379/// assert_eq!(f64::convertible_from(&x), true);
380///
381/// assert_eq!(f32::convertible_from(&Float::power_of_2(100u64)), true);
382/// assert_eq!(f32::convertible_from(&Float::power_of_2(1000u64)), false);
383/// assert_eq!(f64::convertible_from(&Float::power_of_2(1000u64)), true);
384/// assert_eq!(f64::convertible_from(&Float::power_of_2(10000u64)), false);
385/// ```
386///
387/// # try_from
388/// ```
389/// use malachite_base::num::arithmetic::traits::PowerOf2;
390/// use malachite_base::num::basic::traits::{Infinity, NaN, Zero};
391/// use malachite_base::num::float::NiceFloat;
392/// use malachite_float::Float;
393/// use malachite_float::float::conversion::primitive_float_from_float::FloatFromFloatError;
394///
395/// assert_eq!(
396///     NiceFloat(f32::try_from(Float::NAN).unwrap()),
397///     NiceFloat(f32::NAN)
398/// );
399/// assert_eq!(f32::try_from(Float::INFINITY), Ok(f32::INFINITY));
400/// assert_eq!(f32::try_from(Float::ZERO), Ok(0.0));
401/// assert_eq!(f32::try_from(Float::from(1.5)), Ok(1.5));
402/// assert_eq!(f32::try_from(Float::from(-1.5)), Ok(-1.5));
403/// assert_eq!(f32::try_from(Float::from(123.0)), Ok(123.0));
404/// assert_eq!(f32::try_from(Float::from(-123.0)), Ok(-123.0));
405///
406/// // Even though precision is high, the value is just 1.0 and can be converted
407/// assert_eq!(f32::try_from(Float::one_prec(100)), Ok(1.0));
408///
409/// let mut x = Float::one_prec(40);
410/// x.increment();
411///
412/// // precision too high for f32
413/// assert_eq!(f32::try_from(x.clone()), Err(FloatFromFloatError::Inexact));
414///
415/// // but not for f64
416/// assert_eq!(
417///     NiceFloat(f64::try_from(x).unwrap()),
418///     NiceFloat(1.000000000001819)
419/// );
420///
421/// assert_eq!(
422///     NiceFloat(f32::try_from(Float::power_of_2(100u64)).unwrap()),
423///     NiceFloat(1.2676506e30)
424/// );
425/// assert_eq!(
426///     f32::try_from(Float::power_of_2(1000u64)),
427///     Err(FloatFromFloatError::Overflow)
428/// );
429/// assert_eq!(
430///     NiceFloat(f64::try_from(Float::power_of_2(1000u64)).unwrap()),
431///     NiceFloat(1.0715086071862673e301)
432/// );
433/// assert_eq!(
434///     f64::try_from(Float::power_of_2(10000u64)),
435///     Err(FloatFromFloatError::Overflow)
436/// );
437///
438/// assert_eq!(
439///     NiceFloat(f32::try_from(&Float::NAN).unwrap()),
440///     NiceFloat(f32::NAN)
441/// );
442/// assert_eq!(f32::try_from(&Float::INFINITY), Ok(f32::INFINITY));
443/// assert_eq!(f32::try_from(&Float::ZERO), Ok(0.0));
444/// assert_eq!(f32::try_from(&Float::from(1.5)), Ok(1.5));
445/// assert_eq!(f32::try_from(&Float::from(-1.5)), Ok(-1.5));
446/// assert_eq!(f32::try_from(&Float::from(123.0)), Ok(123.0));
447/// assert_eq!(f32::try_from(&Float::from(-123.0)), Ok(-123.0));
448///
449/// // Even though precision is high, the value is just 1.0 and can be converted
450/// assert_eq!(f32::try_from(&Float::one_prec(100)), Ok(1.0));
451///
452/// let mut x = Float::one_prec(40);
453/// x.increment();
454///
455/// // precision too high for f32
456/// assert_eq!(f32::try_from(&x), Err(FloatFromFloatError::Inexact));
457///
458/// // but not for f64
459/// assert_eq!(
460///     NiceFloat(f64::try_from(&x).unwrap()),
461///     NiceFloat(1.000000000001819)
462/// );
463///
464/// assert_eq!(
465///     NiceFloat(f32::try_from(&Float::power_of_2(100u64)).unwrap()),
466///     NiceFloat(1.2676506e30)
467/// );
468/// assert_eq!(
469///     f32::try_from(&Float::power_of_2(1000u64)),
470///     Err(FloatFromFloatError::Overflow)
471/// );
472/// assert_eq!(
473///     NiceFloat(f64::try_from(&Float::power_of_2(1000u64)).unwrap()),
474///     NiceFloat(1.0715086071862673e301)
475/// );
476/// assert_eq!(
477///     f64::try_from(&Float::power_of_2(10000u64)),
478///     Err(FloatFromFloatError::Overflow)
479/// );
480/// ```
481///
482/// # rounding_from
483/// ```
484/// use malachite_base::num::conversion::traits::RoundingFrom;
485/// use malachite_base::num::float::NiceFloat;
486/// use malachite_base::rounding_modes::RoundingMode::*;
487/// use malachite_float::Float;
488/// use malachite_q::Rational;
489/// use std::cmp::Ordering::*;
490///
491/// let f = Float::from_rational_prec(Rational::from_signeds(1, 3), 100).0;
492///
493/// let (x, o) = f32::rounding_from(f.clone(), Floor);
494/// assert_eq!(NiceFloat(x), NiceFloat(0.3333333));
495/// assert_eq!(o, Less);
496///
497/// let (x, o) = f32::rounding_from(f.clone(), Ceiling);
498/// assert_eq!(NiceFloat(x), NiceFloat(0.33333334));
499/// assert_eq!(o, Greater);
500///
501/// let (x, o) = f32::rounding_from(f.clone(), Nearest);
502/// assert_eq!(NiceFloat(x), NiceFloat(0.33333334));
503/// assert_eq!(o, Greater);
504///
505/// let (x, o) = f32::rounding_from(&f, Floor);
506/// assert_eq!(NiceFloat(x), NiceFloat(0.3333333));
507/// assert_eq!(o, Less);
508///
509/// let (x, o) = f32::rounding_from(&f, Ceiling);
510/// assert_eq!(NiceFloat(x), NiceFloat(0.33333334));
511/// assert_eq!(o, Greater);
512///
513/// let (x, o) = f32::rounding_from(&f, Nearest);
514/// assert_eq!(NiceFloat(x), NiceFloat(0.33333334));
515/// assert_eq!(o, Greater);
516/// ```
517pub mod primitive_float_from_float;
518/// Functions and implementations of traits for converting a [`Float`](crate::Float) to a primitive
519/// integer.
520///
521/// The traits are [`TryFrom`],
522/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom), and
523/// [`RoundingFrom`](malachite_base::num::conversion::traits::RoundingFrom).
524///
525/// # rounding_from
526/// ```
527/// use malachite_base::num::conversion::traits::RoundingFrom;
528/// use malachite_base::rounding_modes::RoundingMode::*;
529/// use malachite_float::Float;
530/// use std::cmp::Ordering::*;
531///
532/// assert_eq!(u8::rounding_from(Float::from(1.5), Floor), (1, Less));
533/// assert_eq!(u8::rounding_from(Float::from(1.5), Ceiling), (2, Greater));
534/// assert_eq!(u8::rounding_from(Float::from(1.5), Nearest), (2, Greater));
535///
536/// assert_eq!(u8::rounding_from(Float::from(256.0), Down), (255, Less));
537/// assert_eq!(u8::rounding_from(Float::from(256.0), Floor), (255, Less));
538/// assert_eq!(u8::rounding_from(Float::from(256.0), Nearest), (255, Less));
539///
540/// assert_eq!(u8::rounding_from(Float::from(-123.0), Down), (0, Greater));
541/// assert_eq!(
542///     u8::rounding_from(Float::from(-123.0), Ceiling),
543///     (0, Greater)
544/// );
545/// assert_eq!(
546///     u8::rounding_from(Float::from(-123.0), Nearest),
547///     (0, Greater)
548/// );
549///
550/// assert_eq!(i8::rounding_from(Float::from(1.5), Floor), (1, Less));
551/// assert_eq!(i8::rounding_from(Float::from(1.5), Ceiling), (2, Greater));
552/// assert_eq!(i8::rounding_from(Float::from(1.5), Nearest), (2, Greater));
553///
554/// assert_eq!(i8::rounding_from(Float::from(-1.5), Floor), (-2, Less));
555/// assert_eq!(i8::rounding_from(Float::from(-1.5), Ceiling), (-1, Greater));
556/// assert_eq!(i8::rounding_from(Float::from(-1.5), Nearest), (-2, Less));
557///
558/// assert_eq!(i8::rounding_from(Float::from(128.0), Down), (127, Less));
559/// assert_eq!(i8::rounding_from(Float::from(128.0), Floor), (127, Less));
560/// assert_eq!(i8::rounding_from(Float::from(128.0), Nearest), (127, Less));
561///
562/// assert_eq!(
563///     i8::rounding_from(Float::from(-129.0), Down),
564///     (-128, Greater)
565/// );
566/// assert_eq!(
567///     i8::rounding_from(Float::from(-129.0), Ceiling),
568///     (-128, Greater)
569/// );
570/// assert_eq!(
571///     i8::rounding_from(Float::from(-129.0), Nearest),
572///     (-128, Greater)
573/// );
574///
575/// assert_eq!(u8::rounding_from(&Float::from(1.5), Floor), (1, Less));
576/// assert_eq!(u8::rounding_from(&Float::from(1.5), Ceiling), (2, Greater));
577/// assert_eq!(u8::rounding_from(&Float::from(1.5), Nearest), (2, Greater));
578///
579/// assert_eq!(u8::rounding_from(&Float::from(256.0), Down), (255, Less));
580/// assert_eq!(u8::rounding_from(&Float::from(256.0), Floor), (255, Less));
581/// assert_eq!(u8::rounding_from(&Float::from(256.0), Nearest), (255, Less));
582///
583/// assert_eq!(u8::rounding_from(&Float::from(-123.0), Down), (0, Greater));
584/// assert_eq!(
585///     u8::rounding_from(&Float::from(-123.0), Ceiling),
586///     (0, Greater)
587/// );
588/// assert_eq!(
589///     u8::rounding_from(&Float::from(-123.0), Nearest),
590///     (0, Greater)
591/// );
592///
593/// assert_eq!(i8::rounding_from(&Float::from(1.5), Floor), (1, Less));
594/// assert_eq!(i8::rounding_from(&Float::from(1.5), Ceiling), (2, Greater));
595/// assert_eq!(i8::rounding_from(&Float::from(1.5), Nearest), (2, Greater));
596///
597/// assert_eq!(i8::rounding_from(&Float::from(-1.5), Floor), (-2, Less));
598/// assert_eq!(
599///     i8::rounding_from(&Float::from(-1.5), Ceiling),
600///     (-1, Greater)
601/// );
602/// assert_eq!(i8::rounding_from(&Float::from(-1.5), Nearest), (-2, Less));
603///
604/// assert_eq!(i8::rounding_from(&Float::from(128.0), Down), (127, Less));
605/// assert_eq!(i8::rounding_from(&Float::from(128.0), Floor), (127, Less));
606/// assert_eq!(i8::rounding_from(&Float::from(128.0), Nearest), (127, Less));
607///
608/// assert_eq!(
609///     i8::rounding_from(&Float::from(-129.0), Down),
610///     (-128, Greater)
611/// );
612/// assert_eq!(
613///     i8::rounding_from(&Float::from(-129.0), Ceiling),
614///     (-128, Greater)
615/// );
616/// assert_eq!(
617///     i8::rounding_from(&Float::from(-129.0), Nearest),
618///     (-128, Greater)
619/// );
620/// ```
621///
622/// # try_from
623/// ```
624/// use malachite_base::num::basic::traits::{Infinity, NaN, Zero};
625/// use malachite_base::num::conversion::from::{SignedFromFloatError, UnsignedFromFloatError};
626/// use malachite_float::Float;
627///
628/// assert_eq!(u8::try_from(Float::ZERO).unwrap(), 0);
629/// assert_eq!(u8::try_from(Float::from(123.0)).unwrap(), 123);
630///
631/// assert_eq!(
632///     u8::try_from(Float::from(-123.0)),
633///     Err(UnsignedFromFloatError::FloatNegative)
634/// );
635/// assert_eq!(
636///     u8::try_from(Float::from(256.0)),
637///     Err(UnsignedFromFloatError::FloatNonIntegerOrOutOfRange)
638/// );
639/// assert_eq!(
640///     u8::try_from(Float::from(1.5)),
641///     Err(UnsignedFromFloatError::FloatNonIntegerOrOutOfRange)
642/// );
643/// assert_eq!(
644///     u8::try_from(Float::INFINITY),
645///     Err(UnsignedFromFloatError::FloatInfiniteOrNan)
646/// );
647/// assert_eq!(
648///     u8::try_from(Float::NAN),
649///     Err(UnsignedFromFloatError::FloatInfiniteOrNan)
650/// );
651///
652/// assert_eq!(i8::try_from(Float::ZERO).unwrap(), 0);
653/// assert_eq!(i8::try_from(Float::from(123.0)).unwrap(), 123);
654/// assert_eq!(i8::try_from(Float::from(-123.0)).unwrap(), -123);
655///
656/// assert_eq!(
657///     i8::try_from(Float::from(128.0)),
658///     Err(SignedFromFloatError::FloatNonIntegerOrOutOfRange)
659/// );
660/// assert_eq!(
661///     i8::try_from(Float::from(-129.0)),
662///     Err(SignedFromFloatError::FloatNonIntegerOrOutOfRange)
663/// );
664/// assert_eq!(
665///     i8::try_from(Float::from(1.5)),
666///     Err(SignedFromFloatError::FloatNonIntegerOrOutOfRange)
667/// );
668/// assert_eq!(
669///     i8::try_from(Float::INFINITY),
670///     Err(SignedFromFloatError::FloatInfiniteOrNan)
671/// );
672/// assert_eq!(
673///     i8::try_from(Float::NAN),
674///     Err(SignedFromFloatError::FloatInfiniteOrNan)
675/// );
676///
677/// assert_eq!(u8::try_from(&Float::ZERO).unwrap(), 0);
678/// assert_eq!(u8::try_from(&Float::from(123.0)).unwrap(), 123);
679///
680/// assert_eq!(
681///     u8::try_from(&Float::from(-123.0)),
682///     Err(UnsignedFromFloatError::FloatNegative)
683/// );
684/// assert_eq!(
685///     u8::try_from(&Float::from(256.0)),
686///     Err(UnsignedFromFloatError::FloatNonIntegerOrOutOfRange)
687/// );
688/// assert_eq!(
689///     u8::try_from(&Float::from(1.5)),
690///     Err(UnsignedFromFloatError::FloatNonIntegerOrOutOfRange)
691/// );
692/// assert_eq!(
693///     u8::try_from(&Float::INFINITY),
694///     Err(UnsignedFromFloatError::FloatInfiniteOrNan)
695/// );
696/// assert_eq!(
697///     u8::try_from(&Float::NAN),
698///     Err(UnsignedFromFloatError::FloatInfiniteOrNan)
699/// );
700///
701/// assert_eq!(i8::try_from(&Float::ZERO).unwrap(), 0);
702/// assert_eq!(i8::try_from(&Float::from(123.0)).unwrap(), 123);
703/// assert_eq!(i8::try_from(&Float::from(-123.0)).unwrap(), -123);
704///
705/// assert_eq!(
706///     i8::try_from(&Float::from(128.0)),
707///     Err(SignedFromFloatError::FloatNonIntegerOrOutOfRange)
708/// );
709/// assert_eq!(
710///     i8::try_from(&Float::from(-129.0)),
711///     Err(SignedFromFloatError::FloatNonIntegerOrOutOfRange)
712/// );
713/// assert_eq!(
714///     i8::try_from(&Float::from(1.5)),
715///     Err(SignedFromFloatError::FloatNonIntegerOrOutOfRange)
716/// );
717/// assert_eq!(
718///     i8::try_from(&Float::INFINITY),
719///     Err(SignedFromFloatError::FloatInfiniteOrNan)
720/// );
721/// assert_eq!(
722///     i8::try_from(&Float::NAN),
723///     Err(SignedFromFloatError::FloatInfiniteOrNan)
724/// );
725/// ```
726///
727/// # convertible_from
728/// ```
729/// use malachite_base::num::basic::traits::{Infinity, NaN, Zero};
730/// use malachite_base::num::conversion::traits::ConvertibleFrom;
731/// use malachite_float::Float;
732///
733/// assert_eq!(u8::convertible_from(&Float::ZERO), true);
734/// assert_eq!(u8::convertible_from(&Float::from(123.0)), true);
735///
736/// assert_eq!(u8::convertible_from(&Float::from(256.0)), false);
737/// assert_eq!(u8::convertible_from(&Float::from(-123.0)), false);
738/// assert_eq!(u8::convertible_from(&Float::from(1.5)), false);
739/// assert_eq!(u8::convertible_from(&Float::INFINITY), false);
740/// assert_eq!(u8::convertible_from(&Float::NAN), false);
741///
742/// assert_eq!(i8::convertible_from(&Float::ZERO), true);
743/// assert_eq!(i8::convertible_from(&Float::from(123.0)), true);
744/// assert_eq!(i8::convertible_from(&Float::from(-123.0)), true);
745///
746/// assert_eq!(i8::convertible_from(&Float::from(128.0)), false);
747/// assert_eq!(i8::convertible_from(&Float::from(-129.0)), false);
748/// assert_eq!(i8::convertible_from(&Float::from(1.5)), false);
749/// assert_eq!(i8::convertible_from(&Float::INFINITY), false);
750/// assert_eq!(i8::convertible_from(&Float::NAN), false);
751/// ```
752pub mod primitive_int_from_float;
753/// Implementations of traits for converting a [`Float`](crate::Float) to a
754/// [`Rational`](malachite_q::Rational).
755///
756/// The traits are [`TryFrom`],
757/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom), and
758/// [`RoundingFrom`](malachite_base::num::conversion::traits::RoundingFrom).
759pub mod rational_from_float;
760/// Implementations of traits for serialization and deserialization using
761/// [serde](https://serde.rs/).
762pub mod serde;
763/// Implementations of traits for converting [`Float`](crate::Float)s to and from [`String`]s.
764pub mod string;