Expand description

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

The traits are TryFrom, ConvertibleFrom, and RoundingFrom.

§rounding_from

use malachite_base::num::conversion::traits::RoundingFrom;
use malachite_base::rounding_modes::RoundingMode;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

assert_eq!(Integer::rounding_from(0.0, RoundingMode::Exact).to_debug_string(), "(0, Equal)");
assert_eq!(Integer::rounding_from(-0.0, RoundingMode::Exact).to_debug_string(), "(0, Equal)");
assert_eq!(
    Integer::rounding_from(123.0, RoundingMode::Exact).to_debug_string(),
    "(123, Equal)"
);
assert_eq!(
    Integer::rounding_from(1.0e9, RoundingMode::Exact).to_debug_string(),
    "(1000000000, Equal)"
);
assert_eq!(
    Integer::rounding_from(1.0e9, RoundingMode::Exact).to_debug_string(),
    "(1000000000, Equal)"
);
assert_eq!(
    Integer::rounding_from(4294967295.0, RoundingMode::Exact).to_debug_string(),
    "(4294967295, Equal)"
);
assert_eq!(
    Integer::rounding_from(4294967296.0, RoundingMode::Exact).to_debug_string(),
    "(4294967296, Equal)"
);
assert_eq!(
    Integer::rounding_from(1.0e100, RoundingMode::Exact).to_debug_string(),
    "(1000000000000000015902891109759918046836080856394528138978132755774783877217038106081346\
    9985856815104, Equal)"
);
assert_eq!(
    Integer::rounding_from(123.1, RoundingMode::Floor).to_debug_string(),
    "(123, Less)"
);
assert_eq!(
    Integer::rounding_from(123.1, RoundingMode::Ceiling).to_debug_string(),
    "(124, Greater)"
);
assert_eq!(
    Integer::rounding_from(123.1, RoundingMode::Nearest).to_debug_string(),
    "(123, Less)"
);
assert_eq!(
    Integer::rounding_from(123.9, RoundingMode::Floor).to_debug_string(),
    "(123, Less)"
);
assert_eq!(
    Integer::rounding_from(123.9, RoundingMode::Ceiling).to_debug_string(),
    "(124, Greater)"
);
assert_eq!(
    Integer::rounding_from(123.9, RoundingMode::Nearest).to_debug_string(),
    "(124, Greater)"
);
assert_eq!(
    Integer::rounding_from(123.5, RoundingMode::Nearest).to_debug_string(),
    "(124, Greater)"
);
assert_eq!(
    Integer::rounding_from(124.5, RoundingMode::Nearest).to_debug_string(),
    "(124, Less)"
);
assert_eq!(
    Integer::rounding_from(-0.99, RoundingMode::Ceiling).to_debug_string(),
    "(0, Greater)"
);
assert_eq!(
    Integer::rounding_from(-0.499, RoundingMode::Nearest).to_debug_string(),
    "(0, Greater)"
);
assert_eq!(
    Integer::rounding_from(-0.5, RoundingMode::Nearest).to_debug_string(),
    "(0, Greater)"
);

§try_from

use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::basic::traits::NegativeInfinity;
use malachite_base::strings::ToDebugString;
use malachite_nz::integer::Integer;

assert_eq!(Integer::try_from(f64::NAN).to_debug_string(), "Err(FloatInfiniteOrNan)");
assert_eq!(Integer::try_from(f64::INFINITY).to_debug_string(), "Err(FloatInfiniteOrNan)");
assert_eq!(
    Integer::try_from(f64::NEGATIVE_INFINITY).to_debug_string(),
    "Err(FloatInfiniteOrNan)"
);
assert_eq!(Integer::try_from(0.0).to_debug_string(), "Ok(0)");
assert_eq!(Integer::try_from(-0.0).to_debug_string(), "Ok(0)");
assert_eq!(Integer::try_from(123.0).to_debug_string(), "Ok(123)");
assert_eq!(Integer::try_from(-123.0).to_debug_string(), "Ok(-123)");
assert_eq!(Integer::try_from(1.0e9).to_debug_string(), "Ok(1000000000)");
assert_eq!(Integer::try_from(4294967295.0).to_debug_string(), "Ok(4294967295)");
assert_eq!(Integer::try_from(4294967296.0).to_debug_string(), "Ok(4294967296)");
assert_eq!(
    Integer::try_from(1.0e100).to_debug_string(),
    "Ok(10000000000000000159028911097599180468360808563945281389781327557747838772170381060813\
    469985856815104)"
);
assert_eq!(Integer::try_from(123.1).to_debug_string(), "Err(FloatNonIntegerOrOutOfRange)");
assert_eq!(Integer::try_from(123.9).to_debug_string(), "Err(FloatNonIntegerOrOutOfRange)");
assert_eq!(Integer::try_from(123.5).to_debug_string(), "Err(FloatNonIntegerOrOutOfRange)");
assert_eq!(Integer::try_from(124.5).to_debug_string(), "Err(FloatNonIntegerOrOutOfRange)");
assert_eq!(Integer::try_from(-0.499).to_debug_string(), "Err(FloatNonIntegerOrOutOfRange)");
assert_eq!(Integer::try_from(-0.5).to_debug_string(), "Err(FloatNonIntegerOrOutOfRange)");

§convertible_from

use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::basic::traits::NegativeInfinity;
use malachite_base::num::conversion::traits::ConvertibleFrom;
use malachite_nz::integer::Integer;

assert_eq!(Integer::convertible_from(f64::NAN), false);
assert_eq!(Integer::convertible_from(f64::INFINITY), false);
assert_eq!(Integer::convertible_from(f64::NEGATIVE_INFINITY), false);
assert_eq!(Integer::convertible_from(0.0), true);
assert_eq!(Integer::convertible_from(-0.0), true);
assert_eq!(Integer::convertible_from(123.0), true);
assert_eq!(Integer::convertible_from(-123.0), true);
assert_eq!(Integer::convertible_from(1.0e9), true);
assert_eq!(Integer::convertible_from(4294967295.0), true);
assert_eq!(Integer::convertible_from(4294967296.0), true);
assert_eq!(Integer::convertible_from(1.0e100), true);
assert_eq!(Integer::convertible_from(123.1), false);
assert_eq!(Integer::convertible_from(123.9), false);
assert_eq!(Integer::convertible_from(123.5), false);
assert_eq!(Integer::convertible_from(124.5), false);
assert_eq!(Integer::convertible_from(-0.499), false);
assert_eq!(Integer::convertible_from(-0.5), false);