Expand description

Implementations of traits for converting an Integer to a primitive float.

The traits are From, CheckedFrom, ConvertibleFrom, and RoundingFrom.

rounding_from

extern crate malachite_base;

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
);

from

use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(f32::from(&Integer::from_str("123").unwrap()), 123.0);
assert_eq!(f32::from(&Integer::from_str("-1000000001").unwrap()), -1.0e9);
assert_eq!(
    f32::from(
        &Integer::from_str("10000000000000000000000000000000000000000000000000000").unwrap()
    ),
    3.4028235e38
);

checked_from

extern crate malachite_base;

use malachite_base::num::conversion::traits::CheckedFrom;
use malachite_nz::integer::Integer;
use std::str::FromStr;

assert_eq!(f32::checked_from(&Integer::from_str("123").unwrap()), Some(123.0));
assert_eq!(f32::checked_from(&Integer::from_str("-1000000000").unwrap()), Some(-1.0e9));
assert_eq!(f32::checked_from(&Integer::from_str("1000000001").unwrap()), None);
assert_eq!(
    f32::checked_from(
        &Integer::from_str("-10000000000000000000000000000000000000000000000000000").unwrap()
    ),
    None
);

convertible_from

extern crate malachite_base;

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
);