malachite_base/num/conversion/mod.rs
1// Copyright © 2025 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/// Traits for working with the digits of numbers.
10pub mod digits;
11/// Traits for converting between different number types. The traits are
12/// [`WrappingFrom`](traits::WrappingFrom), [`SaturatingFrom`](traits::SaturatingFrom),
13/// [`OverflowingFrom`](traits::OverflowingFrom), [`ConvertibleFrom`](traits::ConvertibleFrom), and
14/// [`RoundingFrom`](traits::RoundingFrom).
15///
16/// # try_from
17/// ```
18/// use malachite_base::num::conversion::from::{
19/// PrimitiveFloatFromSignedError, PrimitiveFloatFromUnsignedError, SignedFromFloatError,
20/// UnsignedFromFloatError,
21/// };
22/// use malachite_base::num::float::NiceFloat;
23///
24/// assert_eq!(NiceFloat::<f32>::try_from(100u8), Ok(NiceFloat(100.0)));
25/// assert_eq!(
26/// NiceFloat::<f32>::try_from(u32::MAX),
27/// Err(PrimitiveFloatFromUnsignedError)
28/// );
29/// assert_eq!(NiceFloat::<f32>::try_from(100i8), Ok(NiceFloat(100.0)));
30/// assert_eq!(
31/// NiceFloat::<f32>::try_from(i32::MAX),
32/// Err(PrimitiveFloatFromSignedError)
33/// );
34///
35/// assert_eq!(u8::try_from(NiceFloat(100.0f32)), Ok(100));
36/// assert_eq!(
37/// u8::try_from(NiceFloat(100.1f32)),
38/// Err(UnsignedFromFloatError::FloatNonIntegerOrOutOfRange)
39/// );
40/// assert_eq!(
41/// u8::try_from(NiceFloat(300.0f32)),
42/// Err(UnsignedFromFloatError::FloatNonIntegerOrOutOfRange)
43/// );
44/// assert_eq!(
45/// u8::try_from(NiceFloat(-100.0f32)),
46/// Err(UnsignedFromFloatError::FloatNegative)
47/// );
48/// assert_eq!(i8::try_from(NiceFloat(-100.0f32)), Ok(-100));
49/// assert_eq!(
50/// i8::try_from(NiceFloat(-200.0f32)),
51/// Err(SignedFromFloatError::FloatNonIntegerOrOutOfRange)
52/// );
53/// ```
54///
55/// # wrapping_from
56/// ```
57/// use malachite_base::num::conversion::traits::WrappingFrom;
58///
59/// assert_eq!(u8::wrapping_from(123u8), 123);
60/// assert_eq!(i32::wrapping_from(-5i32), -5);
61///
62/// assert_eq!(u16::wrapping_from(123u8), 123);
63/// assert_eq!(i64::wrapping_from(-5i32), -5);
64/// assert_eq!(u32::wrapping_from(5u64), 5);
65///
66/// assert_eq!(u8::wrapping_from(1000u16), 232);
67/// assert_eq!(u32::wrapping_from(-5i32), 4294967291);
68/// assert_eq!(i32::wrapping_from(3000000000u32), -1294967296);
69/// assert_eq!(i8::wrapping_from(-1000i16), 24);
70/// ```
71///
72/// # saturating_from
73/// ```
74/// use malachite_base::num::conversion::traits::SaturatingFrom;
75///
76/// assert_eq!(u8::saturating_from(123u8), 123);
77/// assert_eq!(i32::saturating_from(-5i32), -5);
78///
79/// assert_eq!(u16::saturating_from(123u8), 123);
80/// assert_eq!(i64::saturating_from(-5i32), -5);
81/// assert_eq!(u32::saturating_from(5u64), 5);
82///
83/// assert_eq!(u8::saturating_from(1000u16), 255);
84/// assert_eq!(u32::saturating_from(-5i32), 0);
85/// assert_eq!(i32::saturating_from(3000000000u32), 2147483647);
86/// assert_eq!(i8::saturating_from(-1000i16), -128);
87/// ```
88///
89/// # overflowing_from
90/// ```
91/// use malachite_base::num::conversion::traits::OverflowingFrom;
92///
93/// assert_eq!(u8::overflowing_from(123u8), (123, false));
94/// assert_eq!(i32::overflowing_from(-5i32), (-5, false));
95///
96/// assert_eq!(u16::overflowing_from(123u8), (123, false));
97/// assert_eq!(i64::overflowing_from(-5i32), (-5, false));
98/// assert_eq!(u32::overflowing_from(5u64), (5, false));
99///
100/// assert_eq!(u8::overflowing_from(1000u16), (232, true));
101/// assert_eq!(u32::overflowing_from(-5i32), (4294967291, true));
102/// assert_eq!(i32::overflowing_from(3000000000u32), (-1294967296, true));
103/// assert_eq!(i8::overflowing_from(-1000i16), (24, true));
104/// ```
105///
106/// # convertible_from
107/// ```
108/// use malachite_base::num::conversion::traits::ConvertibleFrom;
109///
110/// assert_eq!(u8::convertible_from(123u8), true);
111/// assert_eq!(i32::convertible_from(-5i32), true);
112///
113/// assert_eq!(u16::convertible_from(123u8), true);
114/// assert_eq!(i64::convertible_from(-5i32), true);
115/// assert_eq!(u32::convertible_from(5u64), true);
116///
117/// assert_eq!(u8::convertible_from(1000u16), false);
118/// assert_eq!(u32::convertible_from(-5i32), false);
119/// assert_eq!(i32::convertible_from(3000000000u32), false);
120/// assert_eq!(i8::convertible_from(-1000i16), false);
121///
122/// assert_eq!(f32::convertible_from(100u8), true);
123/// assert_eq!(f32::convertible_from(u32::MAX), false);
124///
125/// assert_eq!(u8::convertible_from(100.0f32), true);
126/// assert_eq!(u8::convertible_from(100.1f32), false);
127/// assert_eq!(u8::convertible_from(300.0f32), false);
128/// assert_eq!(u8::convertible_from(-100.0f32), false);
129/// ```
130///
131/// # rounding_from
132/// ```
133/// use malachite_base::num::conversion::traits::RoundingFrom;
134/// use malachite_base::rounding_modes::RoundingMode::*;
135/// use std::cmp::Ordering::*;
136///
137/// assert_eq!(f32::rounding_from(100, Floor), (100.0, Equal));
138/// assert_eq!(f32::rounding_from(100, Down), (100.0, Equal));
139/// assert_eq!(f32::rounding_from(100, Ceiling), (100.0, Equal));
140/// assert_eq!(f32::rounding_from(100, Up), (100.0, Equal));
141/// assert_eq!(f32::rounding_from(100, Nearest), (100.0, Equal));
142/// assert_eq!(f32::rounding_from(100, Exact), (100.0, Equal));
143///
144/// assert_eq!(f32::rounding_from(i32::MAX, Floor), (2147483500.0, Less));
145/// assert_eq!(f32::rounding_from(i32::MAX, Down), (2147483500.0, Less));
146/// assert_eq!(
147/// f32::rounding_from(i32::MAX, Ceiling),
148/// (2147483600.0, Greater)
149/// );
150/// assert_eq!(f32::rounding_from(i32::MAX, Up), (2147483600.0, Greater));
151/// assert_eq!(
152/// f32::rounding_from(i32::MAX, Nearest),
153/// (2147483600.0, Greater)
154/// );
155///
156/// assert_eq!(u32::rounding_from(100.0f32, Floor), (100, Equal));
157/// assert_eq!(u32::rounding_from(100.0f32, Down), (100, Equal));
158/// assert_eq!(u32::rounding_from(100.0f32, Ceiling), (100, Equal));
159/// assert_eq!(u32::rounding_from(100.0f32, Up), (100, Equal));
160/// assert_eq!(u32::rounding_from(100.0f32, Nearest), (100, Equal));
161/// assert_eq!(u32::rounding_from(100.0f32, Exact), (100, Equal));
162///
163/// assert_eq!(u32::rounding_from(100.5f32, Floor), (100, Less));
164/// assert_eq!(u32::rounding_from(100.5f32, Down), (100, Less));
165/// assert_eq!(u32::rounding_from(100.5f32, Ceiling), (101, Greater));
166/// assert_eq!(u32::rounding_from(100.5f32, Up), (101, Greater));
167/// assert_eq!(u32::rounding_from(100.5f32, Nearest), (100, Less));
168/// ```
169pub mod from;
170/// [`JoinHalves`](traits::JoinHalves) and [`SplitInHalf`](traits::SplitInHalf), traits for joining
171/// the bits of two numbers or for splitting a number in half.
172///
173/// # join_halves
174/// ```
175/// use malachite_base::num::conversion::traits::JoinHalves;
176///
177/// assert_eq!(u16::join_halves(1, 2), 258);
178/// assert_eq!(u32::join_halves(0xabcd, 0x1234), 0xabcd1234);
179/// ```
180///
181/// # split_in_half
182/// ```
183/// use malachite_base::num::conversion::traits::SplitInHalf;
184///
185/// assert_eq!(258u16.split_in_half(), (1, 2));
186/// assert_eq!(0xabcd1234u32.split_in_half(), (0xabcd, 0x1234));
187/// ```
188///
189/// # lower_half
190/// ```
191/// use malachite_base::num::conversion::traits::SplitInHalf;
192///
193/// assert_eq!(258u16.lower_half(), 2);
194/// assert_eq!(0xabcd1234u32.lower_half(), 0x1234);
195/// ```
196///
197/// # upper_half
198/// ```
199/// use malachite_base::num::conversion::traits::SplitInHalf;
200///
201/// assert_eq!(258u16.upper_half(), 1);
202/// assert_eq!(0xabcd1234u32.upper_half(), 0xabcd);
203/// ```
204pub mod half;
205/// [`IsInteger`](traits::IsInteger), a trait for determining whether a value is an integer.
206///
207/// # is_integer
208/// ```
209/// use malachite_base::num::basic::traits::NegativeInfinity;
210/// use malachite_base::num::conversion::traits::IsInteger;
211///
212/// assert_eq!(0.is_integer(), true);
213/// assert_eq!(1.is_integer(), true);
214/// assert_eq!(100.is_integer(), true);
215/// assert_eq!((-1).is_integer(), true);
216/// assert_eq!((-100).is_integer(), true);
217///
218/// assert_eq!(0.0.is_integer(), true);
219/// assert_eq!(1.0.is_integer(), true);
220/// assert_eq!(100.0.is_integer(), true);
221/// assert_eq!((-1.0).is_integer(), true);
222/// assert_eq!((-100.0).is_integer(), true);
223///
224/// assert_eq!(0.1.is_integer(), false);
225/// assert_eq!(100.1.is_integer(), false);
226/// assert_eq!(f32::NAN.is_integer(), false);
227/// assert_eq!(f32::INFINITY.is_integer(), false);
228/// assert_eq!(f32::NEGATIVE_INFINITY.is_integer(), false);
229/// ```
230pub mod is_integer;
231/// Traits for converting numbers to and from mantissa and exponent representations.
232///
233/// See [`PrimitiveFloat`](super::basic::floats::PrimitiveFloat) for a description of the different
234/// types of mantissas and exponents. The traits are
235/// [`RawMantissaAndExponent`](traits::RawMantissaAndExponent),
236/// [`IntegerMantissaAndExponent`](traits::IntegerMantissaAndExponent), and
237/// [`SciMantissaAndExponent`](traits::SciMantissaAndExponent).
238///
239/// # raw_mantissa_and_exponent
240/// ```
241/// use malachite_base::num::basic::traits::NegativeInfinity;
242/// use malachite_base::num::conversion::traits::RawMantissaAndExponent;
243///
244/// assert_eq!(0.0f32.raw_mantissa_and_exponent(), (0, 0));
245/// assert_eq!((-0.0f32).raw_mantissa_and_exponent(), (0, 0));
246/// assert_eq!(f32::NAN.raw_mantissa_and_exponent(), (0x400000, 255));
247/// assert_eq!(f32::INFINITY.raw_mantissa_and_exponent(), (0, 255));
248/// assert_eq!(f32::NEGATIVE_INFINITY.raw_mantissa_and_exponent(), (0, 255));
249/// assert_eq!(1.0f32.raw_mantissa_and_exponent(), (0, 127));
250/// assert_eq!(
251/// core::f32::consts::PI.raw_mantissa_and_exponent(),
252/// (4788187, 128)
253/// );
254/// assert_eq!(0.1f32.raw_mantissa_and_exponent(), (5033165, 123));
255/// ```
256///
257/// # raw_mantissa
258/// ```
259/// use malachite_base::num::basic::traits::NegativeInfinity;
260/// use malachite_base::num::conversion::traits::RawMantissaAndExponent;
261///
262/// assert_eq!(0.0f32.raw_mantissa(), 0);
263/// assert_eq!((-0.0f32).raw_mantissa(), 0);
264/// assert_eq!(f32::NAN.raw_mantissa(), 0x400000);
265/// assert_eq!(f32::INFINITY.raw_mantissa(), 0);
266/// assert_eq!(f32::NEGATIVE_INFINITY.raw_mantissa(), 0);
267/// assert_eq!(1.0f32.raw_mantissa(), 0);
268/// assert_eq!(core::f32::consts::PI.raw_mantissa(), 4788187);
269/// assert_eq!(0.1f32.raw_mantissa(), 5033165);
270/// ```
271///
272/// # raw_exponent
273///
274/// ```
275/// use malachite_base::num::basic::traits::NegativeInfinity;
276/// use malachite_base::num::conversion::traits::RawMantissaAndExponent;
277///
278/// assert_eq!(0.0f32.raw_exponent(), 0);
279/// assert_eq!((-0.0f32).raw_exponent(), 0);
280/// assert_eq!(f32::NAN.raw_exponent(), 255);
281/// assert_eq!(f32::INFINITY.raw_exponent(), 255);
282/// assert_eq!(f32::NEGATIVE_INFINITY.raw_exponent(), 255);
283/// assert_eq!(1.0f32.raw_exponent(), 127);
284/// assert_eq!(core::f32::consts::PI.raw_exponent(), 128);
285/// assert_eq!(0.1f32.raw_exponent(), 123);
286/// ```
287///
288/// # from_raw_mantissa_and_exponent
289/// ```
290/// use malachite_base::num::conversion::traits::RawMantissaAndExponent;
291/// use malachite_base::num::float::NiceFloat;
292///
293/// assert_eq!(
294/// NiceFloat(f32::from_raw_mantissa_and_exponent(0, 0)),
295/// NiceFloat(0.0)
296/// );
297/// assert_eq!(
298/// NiceFloat(f32::from_raw_mantissa_and_exponent(0x400000, 255)),
299/// NiceFloat(f32::NAN)
300/// );
301/// assert_eq!(
302/// NiceFloat(f32::from_raw_mantissa_and_exponent(0, 255)),
303/// NiceFloat(f32::INFINITY)
304/// );
305/// assert_eq!(
306/// NiceFloat(f32::from_raw_mantissa_and_exponent(0, 127)),
307/// NiceFloat(1.0)
308/// );
309/// assert_eq!(
310/// NiceFloat(f32::from_raw_mantissa_and_exponent(4788187, 128)),
311/// NiceFloat(core::f32::consts::PI)
312/// );
313/// assert_eq!(
314/// NiceFloat(f32::from_raw_mantissa_and_exponent(5033165, 123)),
315/// NiceFloat(0.1)
316/// );
317/// assert_eq!(
318/// NiceFloat(f32::from_raw_mantissa_and_exponent(2097152, 130)),
319/// NiceFloat(10.0)
320/// );
321/// ```
322///
323/// # integer_mantissa_and_exponent
324/// ```
325/// use malachite_base::num::basic::floats::PrimitiveFloat;
326/// use malachite_base::num::conversion::traits::IntegerMantissaAndExponent;
327///
328/// assert_eq!(1u8.integer_mantissa_and_exponent(), (1, 0));
329/// assert_eq!(2u8.integer_mantissa_and_exponent(), (1, 1));
330/// assert_eq!(3u8.integer_mantissa_and_exponent(), (3, 0));
331/// assert_eq!(100u8.integer_mantissa_and_exponent(), (25, 2));
332///
333/// assert_eq!(
334/// core::f32::consts::PI.integer_mantissa_and_exponent(),
335/// (13176795, -22)
336/// );
337/// assert_eq!(0.1f32.integer_mantissa_and_exponent(), (13421773, -27));
338/// assert_eq!(10.0f32.integer_mantissa_and_exponent(), (5, 1));
339/// assert_eq!(
340/// f32::MIN_POSITIVE_SUBNORMAL.integer_mantissa_and_exponent(),
341/// (1, -149)
342/// );
343/// assert_eq!(
344/// f32::MAX_SUBNORMAL.integer_mantissa_and_exponent(),
345/// (0x7fffff, -149)
346/// );
347/// assert_eq!(
348/// f32::MIN_POSITIVE_NORMAL.integer_mantissa_and_exponent(),
349/// (1, -126)
350/// );
351/// assert_eq!(
352/// f32::MAX_FINITE.integer_mantissa_and_exponent(),
353/// (0xffffff, 104)
354/// );
355/// ```
356///
357/// # integer_mantissa
358/// ```
359/// use malachite_base::num::basic::floats::PrimitiveFloat;
360/// use malachite_base::num::conversion::traits::IntegerMantissaAndExponent;
361///
362/// assert_eq!(1u8.integer_mantissa(), 1);
363/// assert_eq!(2u8.integer_mantissa(), 1);
364/// assert_eq!(3u8.integer_mantissa(), 3);
365/// assert_eq!(100u8.integer_mantissa(), 25);
366///
367/// assert_eq!(1.0f32.integer_mantissa(), 1);
368/// assert_eq!(core::f32::consts::PI.integer_mantissa(), 13176795);
369/// assert_eq!(0.1f32.integer_mantissa(), 13421773);
370/// assert_eq!(10.0f32.integer_mantissa(), 5);
371/// assert_eq!(f32::MIN_POSITIVE_SUBNORMAL.integer_mantissa(), 1);
372/// assert_eq!(f32::MAX_SUBNORMAL.integer_mantissa(), 0x7fffff);
373/// assert_eq!(f32::MIN_POSITIVE_NORMAL.integer_mantissa(), 1);
374/// assert_eq!(f32::MAX_FINITE.integer_mantissa(), 0xffffff);
375/// ```
376///
377/// # integer_exponent
378/// ```
379/// use malachite_base::num::basic::floats::PrimitiveFloat;
380/// use malachite_base::num::conversion::traits::IntegerMantissaAndExponent;
381///
382/// assert_eq!(1u8.integer_exponent(), 0);
383/// assert_eq!(2u8.integer_exponent(), 1);
384/// assert_eq!(3u8.integer_exponent(), 0);
385/// assert_eq!(100u8.integer_exponent(), 2);
386///
387/// assert_eq!(1.0f32.integer_exponent(), 0);
388/// assert_eq!(core::f32::consts::PI.integer_exponent(), -22);
389/// assert_eq!(0.1f32.integer_exponent(), -27);
390/// assert_eq!(10.0f32.integer_exponent(), 1);
391/// assert_eq!(f32::MIN_POSITIVE_SUBNORMAL.integer_exponent(), -149);
392/// assert_eq!(f32::MAX_SUBNORMAL.integer_exponent(), -149);
393/// assert_eq!(f32::MIN_POSITIVE_NORMAL.integer_exponent(), -126);
394/// assert_eq!(f32::MAX_FINITE.integer_exponent(), 104);
395/// ```
396///
397/// # from_integer_mantissa_and_exponent;
398/// ```
399/// use malachite_base::num::conversion::traits::IntegerMantissaAndExponent;
400/// use malachite_base::num::float::NiceFloat;
401///
402/// assert_eq!(u8::from_integer_mantissa_and_exponent(0, 1), Some(0));
403/// assert_eq!(u8::from_integer_mantissa_and_exponent(1, 0), Some(1));
404/// assert_eq!(u8::from_integer_mantissa_and_exponent(1, 1), Some(2));
405/// assert_eq!(u8::from_integer_mantissa_and_exponent(3, 0), Some(3));
406/// assert_eq!(u8::from_integer_mantissa_and_exponent(25, 2), Some(100));
407///
408/// assert_eq!(
409/// f32::from_integer_mantissa_and_exponent(0, 5).map(NiceFloat),
410/// Some(NiceFloat(0.0))
411/// );
412/// assert_eq!(
413/// f32::from_integer_mantissa_and_exponent(1, 0).map(NiceFloat),
414/// Some(NiceFloat(1.0))
415/// );
416/// assert_eq!(
417/// f32::from_integer_mantissa_and_exponent(4, -2).map(NiceFloat),
418/// Some(NiceFloat(1.0))
419/// );
420/// assert_eq!(
421/// f32::from_integer_mantissa_and_exponent(13176795, -22).map(NiceFloat),
422/// Some(NiceFloat(core::f32::consts::PI))
423/// );
424/// assert_eq!(
425/// f32::from_integer_mantissa_and_exponent(13421773, -27).map(NiceFloat),
426/// Some(NiceFloat(0.1))
427/// );
428/// assert_eq!(
429/// f32::from_integer_mantissa_and_exponent(5, 1).map(NiceFloat),
430/// Some(NiceFloat(10.0))
431/// );
432///
433/// assert_eq!(f32::from_integer_mantissa_and_exponent(5, 10000), None);
434/// assert_eq!(f32::from_integer_mantissa_and_exponent(5, -10000), None);
435/// // In the next 3 examples, the precision is too high.
436/// assert_eq!(f32::from_integer_mantissa_and_exponent(u64::MAX, -32), None);
437/// assert_eq!(f32::from_integer_mantissa_and_exponent(3, -150), None);
438/// assert_eq!(f32::from_integer_mantissa_and_exponent(1, 128), None);
439/// ```
440///
441/// # sci_mantissa_and_exponent
442/// ```
443/// use malachite_base::num::basic::floats::PrimitiveFloat;
444/// use malachite_base::num::conversion::traits::SciMantissaAndExponent;
445/// use malachite_base::num::float::NiceFloat;
446///
447/// let test = |n: u32, mantissa: f32, exponent: u64| {
448/// let (m, e) = n.sci_mantissa_and_exponent();
449/// assert_eq!(NiceFloat(m), NiceFloat(mantissa));
450/// assert_eq!(e, exponent);
451/// };
452/// test(3, 1.5, 1);
453/// test(123, 1.921875, 6);
454/// test(1000000000, 1.8626451, 29);
455///
456/// let test = |x: f32, mantissa: f32, exponent: i64| {
457/// let (actual_mantissa, actual_exponent) = x.sci_mantissa_and_exponent();
458/// assert_eq!(NiceFloat(actual_mantissa), NiceFloat(mantissa));
459/// assert_eq!(actual_exponent, exponent);
460/// };
461/// test(1.0, 1.0, 0);
462/// test(core::f32::consts::PI, 1.5707964, 1);
463/// test(0.1, 1.6, -4);
464/// test(10.0, 1.25, 3);
465/// test(f32::MIN_POSITIVE_SUBNORMAL, 1.0, -149);
466/// test(f32::MAX_SUBNORMAL, 1.9999998, -127);
467/// test(f32::MIN_POSITIVE_NORMAL, 1.0, -126);
468/// test(f32::MAX_FINITE, 1.9999999, 127);
469/// ```
470///
471/// # sci_mantissa
472/// ```
473/// use malachite_base::num::basic::floats::PrimitiveFloat;
474/// use malachite_base::num::conversion::traits::SciMantissaAndExponent;
475/// use malachite_base::num::float::NiceFloat;
476///
477/// assert_eq!(NiceFloat(1.0f32.sci_mantissa()), NiceFloat(1.0));
478/// assert_eq!(
479/// NiceFloat(core::f32::consts::PI.sci_mantissa()),
480/// NiceFloat(1.5707964)
481/// );
482/// assert_eq!(NiceFloat(0.1f32.sci_mantissa()), NiceFloat(1.6));
483/// assert_eq!(NiceFloat(10.0f32.sci_mantissa()), NiceFloat(1.25));
484/// assert_eq!(
485/// NiceFloat(f32::MIN_POSITIVE_SUBNORMAL.sci_mantissa()),
486/// NiceFloat(1.0)
487/// );
488/// assert_eq!(
489/// NiceFloat(f32::MAX_SUBNORMAL.sci_mantissa()),
490/// NiceFloat(1.9999998)
491/// );
492/// assert_eq!(
493/// NiceFloat(f32::MIN_POSITIVE_NORMAL.sci_mantissa()),
494/// NiceFloat(1.0)
495/// );
496/// assert_eq!(
497/// NiceFloat(f32::MAX_FINITE.sci_mantissa()),
498/// NiceFloat(1.9999999)
499/// );
500/// ```
501///
502/// # sci_exponent
503/// ```
504/// use malachite_base::num::basic::floats::PrimitiveFloat;
505/// use malachite_base::num::conversion::traits::SciMantissaAndExponent;
506///
507/// assert_eq!(1.0f32.sci_exponent(), 0);
508/// assert_eq!(core::f32::consts::PI.sci_exponent(), 1);
509/// assert_eq!(0.1f32.sci_exponent(), -4);
510/// assert_eq!(10.0f32.sci_exponent(), 3);
511/// assert_eq!(f32::MIN_POSITIVE_SUBNORMAL.sci_exponent(), -149);
512/// assert_eq!(f32::MAX_SUBNORMAL.sci_exponent(), -127);
513/// assert_eq!(f32::MIN_POSITIVE_NORMAL.sci_exponent(), -126);
514/// assert_eq!(f32::MAX_FINITE.sci_exponent(), 127);
515/// ```
516///
517/// # from_sci_mantissa_and_exponent;
518/// ```
519/// use malachite_base::num::basic::floats::PrimitiveFloat;
520/// use malachite_base::num::conversion::traits::SciMantissaAndExponent;
521/// use malachite_base::num::float::NiceFloat;
522///
523/// assert_eq!(u32::from_sci_mantissa_and_exponent(1.5, 1u64), Some(3u32));
524/// assert_eq!(u32::from_sci_mantissa_and_exponent(1.51, 1u64), Some(3u32));
525/// assert_eq!(
526/// u32::from_sci_mantissa_and_exponent(1.921875, 6u64),
527/// Some(123u32)
528/// );
529/// assert_eq!(u32::from_sci_mantissa_and_exponent(1.5, 1u64), Some(3u32));
530///
531/// assert_eq!(
532/// f32::from_sci_mantissa_and_exponent(1.0, 0).map(NiceFloat),
533/// Some(NiceFloat(1.0))
534/// );
535/// assert_eq!(
536/// f32::from_sci_mantissa_and_exponent(1.5707964, 1).map(NiceFloat),
537/// Some(NiceFloat(core::f32::consts::PI))
538/// );
539/// assert_eq!(
540/// f32::from_sci_mantissa_and_exponent(1.6, -4).map(NiceFloat),
541/// Some(NiceFloat(0.1))
542/// );
543/// assert_eq!(
544/// f32::from_sci_mantissa_and_exponent(1.25, 3).map(NiceFloat),
545/// Some(NiceFloat(10.0))
546/// );
547/// assert_eq!(
548/// f32::from_sci_mantissa_and_exponent(1.0, -149).map(NiceFloat),
549/// Some(NiceFloat(f32::MIN_POSITIVE_SUBNORMAL))
550/// );
551/// assert_eq!(
552/// f32::from_sci_mantissa_and_exponent(1.9999998, -127).map(NiceFloat),
553/// Some(NiceFloat(f32::MAX_SUBNORMAL))
554/// );
555/// assert_eq!(
556/// f32::from_sci_mantissa_and_exponent(1.0, -126).map(NiceFloat),
557/// Some(NiceFloat(f32::MIN_POSITIVE_NORMAL))
558/// );
559/// assert_eq!(
560/// f32::from_sci_mantissa_and_exponent(1.9999999, 127).map(NiceFloat),
561/// Some(NiceFloat(f32::MAX_FINITE))
562/// );
563///
564/// assert_eq!(f32::from_sci_mantissa_and_exponent(2.0, 1), None);
565/// assert_eq!(f32::from_sci_mantissa_and_exponent(1.1, -2000), None);
566/// assert_eq!(f32::from_sci_mantissa_and_exponent(1.1, 2000), None);
567/// assert_eq!(f32::from_sci_mantissa_and_exponent(1.999, -149), None);
568/// ```
569pub mod mantissa_and_exponent;
570/// Traits for converting slices to numbers, slices to [`Vec`]s, or numbers to [`Vec`]s. The traits
571/// are [`FromOtherTypeSlice`](traits::FromOtherTypeSlice),
572/// [`VecFromOtherTypeSlice`](traits::VecFromOtherTypeSlice), and
573/// [`VecFromOtherType`](traits::VecFromOtherType).
574///
575/// # from_other_type_slice
576/// ```
577/// use malachite_base::num::conversion::traits::FromOtherTypeSlice;
578///
579/// let xs: &[u32] = &[];
580/// assert_eq!(u32::from_other_type_slice(xs), 0);
581/// assert_eq!(u32::from_other_type_slice(&[123u32, 456]), 123);
582///
583/// assert_eq!(u8::from_other_type_slice(&[0xabcdu16, 0xef01]), 0xcd);
584///
585/// assert_eq!(u16::from_other_type_slice(&[0xabu8, 0xcd, 0xef]), 0xcdab);
586/// assert_eq!(u64::from_other_type_slice(&[0xabu8, 0xcd, 0xef]), 0xefcdab);
587/// ```
588///
589/// # vec_from_other_type_slice
590/// ```
591/// use malachite_base::num::conversion::traits::VecFromOtherTypeSlice;
592///
593/// assert_eq!(u32::vec_from_other_type_slice(&[123u32, 456]), &[123, 456]);
594/// assert_eq!(
595/// u8::vec_from_other_type_slice(&[0xcdabu16, 0x01ef, 0x4523, 0x8967]),
596/// &[0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89]
597/// );
598/// assert_eq!(
599/// u16::vec_from_other_type_slice(&[0xabu8, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67]),
600/// &[0xcdab, 0x01ef, 0x4523, 0x67]
601/// );
602/// ```
603///
604/// # vec_from_other_type
605/// ```
606/// use malachite_base::num::conversion::traits::VecFromOtherType;
607///
608/// assert_eq!(u32::vec_from_other_type(123u32), &[123]);
609/// assert_eq!(u8::vec_from_other_type(0xcdabu16), &[0xab, 0xcd]);
610/// assert_eq!(u16::vec_from_other_type(0xabu8), &[0xab]);
611/// ```
612pub mod slice;
613/// Traits for converting numbers to and from [`String`]s.
614pub mod string;
615/// Various traits for converting numbers.
616pub mod traits;