Expand description
Implementations of traits for converting a Natural
to a primitive
float.
The traits are TryFrom
,
ConvertibleFrom
, and
RoundingFrom
.
§rounding_from
use malachite_base::num::conversion::traits::RoundingFrom;
use malachite_base::rounding_modes::RoundingMode;
use malachite_nz::natural::Natural;
use core::cmp::Ordering;
use core::str::FromStr;
assert_eq!(
f32::rounding_from(&Natural::from_str("123").unwrap(), RoundingMode::Exact),
(123.0, Ordering::Equal)
);
assert_eq!(
f32::rounding_from(&Natural::from_str("1000000001").unwrap(), RoundingMode::Floor),
(1.0e9, Ordering::Less)
);
assert_eq!(
f32::rounding_from(&Natural::from_str("1000000001").unwrap(), RoundingMode::Ceiling),
(1.00000006e9, Ordering::Greater)
);
assert_eq!(
f32::rounding_from(
&Natural::from_str("10000000000000000000000000000000000000000000000000000").unwrap(),
RoundingMode::Nearest
),
(3.4028235e38, Ordering::Less)
);
§try_from
use malachite_nz::natural::conversion::primitive_float_from_natural::*;
use malachite_nz::natural::Natural;
use core::str::FromStr;
assert_eq!(f32::try_from(&Natural::from_str("123").unwrap()), Ok(123.0));
assert_eq!(f32::try_from(&Natural::from_str("1000000000").unwrap()), Ok(1.0e9));
assert_eq!(
f32::try_from(&Natural::from_str("1000000001").unwrap()),
Err(PrimitiveFloatFromNaturalError)
);
assert_eq!(
f32::try_from(
&Natural::from_str("10000000000000000000000000000000000000000000000000000").unwrap()
),
Err(PrimitiveFloatFromNaturalError)
);
§convertible_from
use malachite_base::num::conversion::traits::ConvertibleFrom;
use malachite_nz::natural::Natural;
use core::str::FromStr;
assert_eq!(f32::convertible_from(&Natural::from_str("123").unwrap()), true);
assert_eq!(f32::convertible_from(&Natural::from_str("1000000000").unwrap()), true);
assert_eq!(f32::convertible_from(&Natural::from_str("1000000001").unwrap()), false);
assert_eq!(
f32::convertible_from(
&Natural::from_str("10000000000000000000000000000000000000000000000000000").unwrap()
),
false
);