Skip to main content

malachite_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.34");
45/// assert_eq!(o, Greater);
46///
47/// let (x, o) = Float::from_primitive_float_prec(123.0, 4);
48/// assert_eq!(x.to_string(), "1.2e2");
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.31");
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.34");
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.34");
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.0");
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(), "1.2e2");
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.0");
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(), "1.2e2");
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.0");
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(), "-1.2e2");
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.0");
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(), "1.2e2");
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(), "1.3e2");
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.0");
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(), "1.2e2");
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(), "1.3e2");
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.0");
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(), "-1.3e2");
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(), "-1.2e2");
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.141592653589793"
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.178979876918526e23"
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.392955569098538e-24"
298/// );
299/// ```
300pub mod mantissa_and_exponent;
301/// Implementations of traits for converting a [`Float`](crate::Float) to a
302/// [`Natural`](malachite_nz::natural::Natural).
303///
304/// The traits are [`TryFrom`],
305/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom), and
306/// [`RoundingFrom`](malachite_base::num::conversion::traits::RoundingFrom).
307pub mod natural_from_float;
308/// Functions and implementations of traits for converting a [`Float`](crate::Float) to a primitive
309/// float.
310///
311/// The traits are [`TryFrom`],
312/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom), and
313/// [`RoundingFrom`](malachite_base::num::conversion::traits::RoundingFrom).
314///
315/// # convertible_from
316/// ```
317/// use malachite_base::num::arithmetic::traits::PowerOf2;
318/// use malachite_base::num::basic::traits::{Infinity, NaN, Zero};
319/// use malachite_base::num::conversion::traits::ConvertibleFrom;
320/// use malachite_float::Float;
321///
322/// assert_eq!(f32::convertible_from(&Float::NAN), true);
323/// assert_eq!(f32::convertible_from(&Float::INFINITY), true);
324/// assert_eq!(f32::convertible_from(&Float::ZERO), true);
325/// assert_eq!(f32::convertible_from(&Float::from(1.5)), true);
326/// assert_eq!(f32::convertible_from(&Float::from(-1.5)), true);
327/// assert_eq!(f32::convertible_from(&Float::from(123.0)), true);
328/// assert_eq!(f32::convertible_from(&Float::from(-123.0)), true);
329///
330/// // Even though precision is high, the value is just 1.0 and can be converted
331/// assert_eq!(f32::convertible_from(&Float::one_prec(100)), true);
332///
333/// let mut x = Float::one_prec(40);
334/// x.increment();
335///
336/// // precision too high for f32
337/// assert_eq!(f32::convertible_from(&x), false);
338///
339/// // but not for f64
340/// assert_eq!(f64::convertible_from(&x), true);
341///
342/// assert_eq!(f32::convertible_from(&Float::power_of_2(100u64)), true);
343/// assert_eq!(f32::convertible_from(&Float::power_of_2(1000u64)), false);
344/// assert_eq!(f64::convertible_from(&Float::power_of_2(1000u64)), true);
345/// assert_eq!(f64::convertible_from(&Float::power_of_2(10000u64)), false);
346/// ```
347///
348/// # try_from
349/// ```
350/// use malachite_base::num::arithmetic::traits::PowerOf2;
351/// use malachite_base::num::basic::traits::{Infinity, NaN, Zero};
352/// use malachite_base::num::float::NiceFloat;
353/// use malachite_float::conversion::primitive_float_from_float::FloatFromFloatError;
354/// use malachite_float::Float;
355///
356/// assert_eq!(
357///     NiceFloat(f32::try_from(Float::NAN).unwrap()),
358///     NiceFloat(f32::NAN)
359/// );
360/// assert_eq!(f32::try_from(Float::INFINITY), Ok(f32::INFINITY));
361/// assert_eq!(f32::try_from(Float::ZERO), Ok(0.0));
362/// assert_eq!(f32::try_from(Float::from(1.5)), Ok(1.5));
363/// assert_eq!(f32::try_from(Float::from(-1.5)), Ok(-1.5));
364/// assert_eq!(f32::try_from(Float::from(123.0)), Ok(123.0));
365/// assert_eq!(f32::try_from(Float::from(-123.0)), Ok(-123.0));
366///
367/// // Even though precision is high, the value is just 1.0 and can be converted
368/// assert_eq!(f32::try_from(Float::one_prec(100)), Ok(1.0));
369///
370/// let mut x = Float::one_prec(40);
371/// x.increment();
372///
373/// // precision too high for f32
374/// assert_eq!(f32::try_from(x.clone()), Err(FloatFromFloatError::Inexact));
375///
376/// // but not for f64
377/// assert_eq!(
378///     NiceFloat(f64::try_from(x).unwrap()),
379///     NiceFloat(1.000000000001819)
380/// );
381///
382/// assert_eq!(
383///     NiceFloat(f32::try_from(Float::power_of_2(100u64)).unwrap()),
384///     NiceFloat(1.2676506e30)
385/// );
386/// assert_eq!(
387///     f32::try_from(Float::power_of_2(1000u64)),
388///     Err(FloatFromFloatError::Overflow)
389/// );
390/// assert_eq!(
391///     NiceFloat(f64::try_from(Float::power_of_2(1000u64)).unwrap()),
392///     NiceFloat(1.0715086071862673e301)
393/// );
394/// assert_eq!(
395///     f64::try_from(Float::power_of_2(10000u64)),
396///     Err(FloatFromFloatError::Overflow)
397/// );
398///
399/// assert_eq!(
400///     NiceFloat(f32::try_from(&Float::NAN).unwrap()),
401///     NiceFloat(f32::NAN)
402/// );
403/// assert_eq!(f32::try_from(&Float::INFINITY), Ok(f32::INFINITY));
404/// assert_eq!(f32::try_from(&Float::ZERO), Ok(0.0));
405/// assert_eq!(f32::try_from(&Float::from(1.5)), Ok(1.5));
406/// assert_eq!(f32::try_from(&Float::from(-1.5)), Ok(-1.5));
407/// assert_eq!(f32::try_from(&Float::from(123.0)), Ok(123.0));
408/// assert_eq!(f32::try_from(&Float::from(-123.0)), Ok(-123.0));
409///
410/// // Even though precision is high, the value is just 1.0 and can be converted
411/// assert_eq!(f32::try_from(&Float::one_prec(100)), Ok(1.0));
412///
413/// let mut x = Float::one_prec(40);
414/// x.increment();
415///
416/// // precision too high for f32
417/// assert_eq!(f32::try_from(&x), Err(FloatFromFloatError::Inexact));
418///
419/// // but not for f64
420/// assert_eq!(
421///     NiceFloat(f64::try_from(&x).unwrap()),
422///     NiceFloat(1.000000000001819)
423/// );
424///
425/// assert_eq!(
426///     NiceFloat(f32::try_from(&Float::power_of_2(100u64)).unwrap()),
427///     NiceFloat(1.2676506e30)
428/// );
429/// assert_eq!(
430///     f32::try_from(&Float::power_of_2(1000u64)),
431///     Err(FloatFromFloatError::Overflow)
432/// );
433/// assert_eq!(
434///     NiceFloat(f64::try_from(&Float::power_of_2(1000u64)).unwrap()),
435///     NiceFloat(1.0715086071862673e301)
436/// );
437/// assert_eq!(
438///     f64::try_from(&Float::power_of_2(10000u64)),
439///     Err(FloatFromFloatError::Overflow)
440/// );
441/// ```
442///
443/// # rounding_from
444/// ```
445/// use malachite_base::num::conversion::traits::RoundingFrom;
446/// use malachite_base::num::float::NiceFloat;
447/// use malachite_base::rounding_modes::RoundingMode::*;
448/// use malachite_float::Float;
449/// use malachite_q::Rational;
450/// use std::cmp::Ordering::*;
451///
452/// let f = Float::from_rational_prec(Rational::from_signeds(1, 3), 100).0;
453///
454/// let (x, o) = f32::rounding_from(f.clone(), Floor);
455/// assert_eq!(NiceFloat(x), NiceFloat(0.3333333));
456/// assert_eq!(o, Less);
457///
458/// let (x, o) = f32::rounding_from(f.clone(), Ceiling);
459/// assert_eq!(NiceFloat(x), NiceFloat(0.33333334));
460/// assert_eq!(o, Greater);
461///
462/// let (x, o) = f32::rounding_from(f.clone(), Nearest);
463/// assert_eq!(NiceFloat(x), NiceFloat(0.33333334));
464/// assert_eq!(o, Greater);
465///
466/// let (x, o) = f32::rounding_from(&f, Floor);
467/// assert_eq!(NiceFloat(x), NiceFloat(0.3333333));
468/// assert_eq!(o, Less);
469///
470/// let (x, o) = f32::rounding_from(&f, Ceiling);
471/// assert_eq!(NiceFloat(x), NiceFloat(0.33333334));
472/// assert_eq!(o, Greater);
473///
474/// let (x, o) = f32::rounding_from(&f, Nearest);
475/// assert_eq!(NiceFloat(x), NiceFloat(0.33333334));
476/// assert_eq!(o, Greater);
477/// ```
478pub mod primitive_float_from_float;
479/// Functions and implementations of traits for converting a [`Float`](crate::Float) to a primitive
480/// integer.
481///
482/// The traits are [`TryFrom`],
483/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom), and
484/// [`RoundingFrom`](malachite_base::num::conversion::traits::RoundingFrom).
485///
486/// # rounding_from
487/// ```
488/// use malachite_base::num::conversion::traits::RoundingFrom;
489/// use malachite_base::rounding_modes::RoundingMode::*;
490/// use malachite_float::Float;
491/// use std::cmp::Ordering::*;
492///
493/// assert_eq!(u8::rounding_from(Float::from(1.5), Floor), (1, Less));
494/// assert_eq!(u8::rounding_from(Float::from(1.5), Ceiling), (2, Greater));
495/// assert_eq!(u8::rounding_from(Float::from(1.5), Nearest), (2, Greater));
496///
497/// assert_eq!(u8::rounding_from(Float::from(256.0), Down), (255, Less));
498/// assert_eq!(u8::rounding_from(Float::from(256.0), Floor), (255, Less));
499/// assert_eq!(u8::rounding_from(Float::from(256.0), Nearest), (255, Less));
500///
501/// assert_eq!(u8::rounding_from(Float::from(-123.0), Down), (0, Greater));
502/// assert_eq!(
503///     u8::rounding_from(Float::from(-123.0), Ceiling),
504///     (0, Greater)
505/// );
506/// assert_eq!(
507///     u8::rounding_from(Float::from(-123.0), Nearest),
508///     (0, Greater)
509/// );
510///
511/// assert_eq!(i8::rounding_from(Float::from(1.5), Floor), (1, Less));
512/// assert_eq!(i8::rounding_from(Float::from(1.5), Ceiling), (2, Greater));
513/// assert_eq!(i8::rounding_from(Float::from(1.5), Nearest), (2, Greater));
514///
515/// assert_eq!(i8::rounding_from(Float::from(-1.5), Floor), (-2, Less));
516/// assert_eq!(i8::rounding_from(Float::from(-1.5), Ceiling), (-1, Greater));
517/// assert_eq!(i8::rounding_from(Float::from(-1.5), Nearest), (-2, Less));
518///
519/// assert_eq!(i8::rounding_from(Float::from(128.0), Down), (127, Less));
520/// assert_eq!(i8::rounding_from(Float::from(128.0), Floor), (127, Less));
521/// assert_eq!(i8::rounding_from(Float::from(128.0), Nearest), (127, Less));
522///
523/// assert_eq!(
524///     i8::rounding_from(Float::from(-129.0), Down),
525///     (-128, Greater)
526/// );
527/// assert_eq!(
528///     i8::rounding_from(Float::from(-129.0), Ceiling),
529///     (-128, Greater)
530/// );
531/// assert_eq!(
532///     i8::rounding_from(Float::from(-129.0), Nearest),
533///     (-128, Greater)
534/// );
535///
536/// assert_eq!(u8::rounding_from(&Float::from(1.5), Floor), (1, Less));
537/// assert_eq!(u8::rounding_from(&Float::from(1.5), Ceiling), (2, Greater));
538/// assert_eq!(u8::rounding_from(&Float::from(1.5), Nearest), (2, Greater));
539///
540/// assert_eq!(u8::rounding_from(&Float::from(256.0), Down), (255, Less));
541/// assert_eq!(u8::rounding_from(&Float::from(256.0), Floor), (255, Less));
542/// assert_eq!(u8::rounding_from(&Float::from(256.0), Nearest), (255, Less));
543///
544/// assert_eq!(u8::rounding_from(&Float::from(-123.0), Down), (0, Greater));
545/// assert_eq!(
546///     u8::rounding_from(&Float::from(-123.0), Ceiling),
547///     (0, Greater)
548/// );
549/// assert_eq!(
550///     u8::rounding_from(&Float::from(-123.0), Nearest),
551///     (0, Greater)
552/// );
553///
554/// assert_eq!(i8::rounding_from(&Float::from(1.5), Floor), (1, Less));
555/// assert_eq!(i8::rounding_from(&Float::from(1.5), Ceiling), (2, Greater));
556/// assert_eq!(i8::rounding_from(&Float::from(1.5), Nearest), (2, Greater));
557///
558/// assert_eq!(i8::rounding_from(&Float::from(-1.5), Floor), (-2, Less));
559/// assert_eq!(
560///     i8::rounding_from(&Float::from(-1.5), Ceiling),
561///     (-1, Greater)
562/// );
563/// assert_eq!(i8::rounding_from(&Float::from(-1.5), Nearest), (-2, Less));
564///
565/// assert_eq!(i8::rounding_from(&Float::from(128.0), Down), (127, Less));
566/// assert_eq!(i8::rounding_from(&Float::from(128.0), Floor), (127, Less));
567/// assert_eq!(i8::rounding_from(&Float::from(128.0), Nearest), (127, Less));
568///
569/// assert_eq!(
570///     i8::rounding_from(&Float::from(-129.0), Down),
571///     (-128, Greater)
572/// );
573/// assert_eq!(
574///     i8::rounding_from(&Float::from(-129.0), Ceiling),
575///     (-128, Greater)
576/// );
577/// assert_eq!(
578///     i8::rounding_from(&Float::from(-129.0), Nearest),
579///     (-128, Greater)
580/// );
581/// ```
582///
583/// # try_from
584/// ```
585/// use malachite_base::num::basic::traits::{Infinity, NaN, Zero};
586/// use malachite_base::num::conversion::from::{SignedFromFloatError, UnsignedFromFloatError};
587/// use malachite_float::Float;
588///
589/// assert_eq!(u8::try_from(Float::ZERO).unwrap(), 0);
590/// assert_eq!(u8::try_from(Float::from(123.0)).unwrap(), 123);
591///
592/// assert_eq!(
593///     u8::try_from(Float::from(-123.0)),
594///     Err(UnsignedFromFloatError::FloatNegative)
595/// );
596/// assert_eq!(
597///     u8::try_from(Float::from(256.0)),
598///     Err(UnsignedFromFloatError::FloatNonIntegerOrOutOfRange)
599/// );
600/// assert_eq!(
601///     u8::try_from(Float::from(1.5)),
602///     Err(UnsignedFromFloatError::FloatNonIntegerOrOutOfRange)
603/// );
604/// assert_eq!(
605///     u8::try_from(Float::INFINITY),
606///     Err(UnsignedFromFloatError::FloatInfiniteOrNan)
607/// );
608/// assert_eq!(
609///     u8::try_from(Float::NAN),
610///     Err(UnsignedFromFloatError::FloatInfiniteOrNan)
611/// );
612///
613/// assert_eq!(i8::try_from(Float::ZERO).unwrap(), 0);
614/// assert_eq!(i8::try_from(Float::from(123.0)).unwrap(), 123);
615/// assert_eq!(i8::try_from(Float::from(-123.0)).unwrap(), -123);
616///
617/// assert_eq!(
618///     i8::try_from(Float::from(128.0)),
619///     Err(SignedFromFloatError::FloatNonIntegerOrOutOfRange)
620/// );
621/// assert_eq!(
622///     i8::try_from(Float::from(-129.0)),
623///     Err(SignedFromFloatError::FloatNonIntegerOrOutOfRange)
624/// );
625/// assert_eq!(
626///     i8::try_from(Float::from(1.5)),
627///     Err(SignedFromFloatError::FloatNonIntegerOrOutOfRange)
628/// );
629/// assert_eq!(
630///     i8::try_from(Float::INFINITY),
631///     Err(SignedFromFloatError::FloatInfiniteOrNan)
632/// );
633/// assert_eq!(
634///     i8::try_from(Float::NAN),
635///     Err(SignedFromFloatError::FloatInfiniteOrNan)
636/// );
637///
638/// assert_eq!(u8::try_from(&Float::ZERO).unwrap(), 0);
639/// assert_eq!(u8::try_from(&Float::from(123.0)).unwrap(), 123);
640///
641/// assert_eq!(
642///     u8::try_from(&Float::from(-123.0)),
643///     Err(UnsignedFromFloatError::FloatNegative)
644/// );
645/// assert_eq!(
646///     u8::try_from(&Float::from(256.0)),
647///     Err(UnsignedFromFloatError::FloatNonIntegerOrOutOfRange)
648/// );
649/// assert_eq!(
650///     u8::try_from(&Float::from(1.5)),
651///     Err(UnsignedFromFloatError::FloatNonIntegerOrOutOfRange)
652/// );
653/// assert_eq!(
654///     u8::try_from(&Float::INFINITY),
655///     Err(UnsignedFromFloatError::FloatInfiniteOrNan)
656/// );
657/// assert_eq!(
658///     u8::try_from(&Float::NAN),
659///     Err(UnsignedFromFloatError::FloatInfiniteOrNan)
660/// );
661///
662/// assert_eq!(i8::try_from(&Float::ZERO).unwrap(), 0);
663/// assert_eq!(i8::try_from(&Float::from(123.0)).unwrap(), 123);
664/// assert_eq!(i8::try_from(&Float::from(-123.0)).unwrap(), -123);
665///
666/// assert_eq!(
667///     i8::try_from(&Float::from(128.0)),
668///     Err(SignedFromFloatError::FloatNonIntegerOrOutOfRange)
669/// );
670/// assert_eq!(
671///     i8::try_from(&Float::from(-129.0)),
672///     Err(SignedFromFloatError::FloatNonIntegerOrOutOfRange)
673/// );
674/// assert_eq!(
675///     i8::try_from(&Float::from(1.5)),
676///     Err(SignedFromFloatError::FloatNonIntegerOrOutOfRange)
677/// );
678/// assert_eq!(
679///     i8::try_from(&Float::INFINITY),
680///     Err(SignedFromFloatError::FloatInfiniteOrNan)
681/// );
682/// assert_eq!(
683///     i8::try_from(&Float::NAN),
684///     Err(SignedFromFloatError::FloatInfiniteOrNan)
685/// );
686/// ```
687///
688/// # convertible_from
689/// ```
690/// use malachite_base::num::basic::traits::{Infinity, NaN, Zero};
691/// use malachite_base::num::conversion::traits::ConvertibleFrom;
692/// use malachite_float::Float;
693///
694/// assert_eq!(u8::convertible_from(&Float::ZERO), true);
695/// assert_eq!(u8::convertible_from(&Float::from(123.0)), true);
696///
697/// assert_eq!(u8::convertible_from(&Float::from(256.0)), false);
698/// assert_eq!(u8::convertible_from(&Float::from(-123.0)), false);
699/// assert_eq!(u8::convertible_from(&Float::from(1.5)), false);
700/// assert_eq!(u8::convertible_from(&Float::INFINITY), false);
701/// assert_eq!(u8::convertible_from(&Float::NAN), false);
702///
703/// assert_eq!(i8::convertible_from(&Float::ZERO), true);
704/// assert_eq!(i8::convertible_from(&Float::from(123.0)), true);
705/// assert_eq!(i8::convertible_from(&Float::from(-123.0)), true);
706///
707/// assert_eq!(i8::convertible_from(&Float::from(128.0)), false);
708/// assert_eq!(i8::convertible_from(&Float::from(-129.0)), false);
709/// assert_eq!(i8::convertible_from(&Float::from(1.5)), false);
710/// assert_eq!(i8::convertible_from(&Float::INFINITY), false);
711/// assert_eq!(i8::convertible_from(&Float::NAN), false);
712/// ```
713pub mod primitive_int_from_float;
714/// Implementations of traits for converting a [`Float`](crate::Float) to a
715/// [`Rational`](malachite_q::Rational).
716///
717/// The traits are [`TryFrom`],
718/// [`ConvertibleFrom`](malachite_base::num::conversion::traits::ConvertibleFrom), and
719/// [`RoundingFrom`](malachite_base::num::conversion::traits::RoundingFrom).
720pub mod rational_from_float;
721/// Implementations of traits for converting [`Float`](crate::Float)s to and from [`String`]s.
722///
723/// Warning: these implementations are unstable and will definitely be changed in the future.
724pub mod string;