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