Expand description
Implementations of traits for converting an Integer 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::integer::Integer;
use std::str::FromStr;
assert_eq!(
f32::rounding_from(&Integer::from_str("123").unwrap(), RoundingMode::Exact),
123.0
);
assert_eq!(
f32::rounding_from(&Integer::from_str("1000000001").unwrap(), RoundingMode::Floor),
1.0e9
);
assert_eq!(
f32::rounding_from(&Integer::from_str("1000000001").unwrap(), RoundingMode::Ceiling),
1.00000006e9
);
assert_eq!(
f32::rounding_from(&Integer::from_str("-1000000001").unwrap(), RoundingMode::Floor),
-1.00000006e9
);
assert_eq!(
f32::rounding_from(&Integer::from_str("-1000000001").unwrap(), RoundingMode::Ceiling),
-1.0e9
);
assert_eq!(
f32::rounding_from(
&Integer::from_str("10000000000000000000000000000000000000000000000000000").unwrap(),
RoundingMode::Nearest
),
3.4028235e38
);try_from
use malachite_nz::integer::conversion::primitive_float_from_integer::*;
use malachite_nz::integer::Integer;
use std::str::FromStr;
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 malachite_base::num::conversion::traits::ConvertibleFrom;
use malachite_nz::integer::Integer;
use std::str::FromStr;
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
);