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