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