Expand description

Implementations of traits for converting a primitive float to a Natural.

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::natural::Natural;

assert_eq!(Natural::rounding_from(0.0, RoundingMode::Exact), 0);
assert_eq!(Natural::rounding_from(-0.0, RoundingMode::Exact), 0);
assert_eq!(Natural::rounding_from(123.0, RoundingMode::Exact), 123);
assert_eq!(Natural::rounding_from(1.0e9, RoundingMode::Exact), 1000000000);
assert_eq!(Natural::rounding_from(1.0e9, RoundingMode::Exact), 1000000000);
assert_eq!(Natural::rounding_from(4294967295.0, RoundingMode::Exact), 4294967295u32);
assert_eq!(Natural::rounding_from(4294967296.0, RoundingMode::Exact), 4294967296u64);
assert_eq!(
    Natural::rounding_from(1.0e100, RoundingMode::Exact).to_string(),
    "10000000000000000159028911097599180468360808563945281389781327557747838772170381060813469\
    985856815104"
);
assert_eq!(Natural::rounding_from(123.1, RoundingMode::Floor), 123);
assert_eq!(Natural::rounding_from(123.1, RoundingMode::Ceiling), 124);
assert_eq!(Natural::rounding_from(123.1, RoundingMode::Nearest), 123);
assert_eq!(Natural::rounding_from(123.9, RoundingMode::Floor), 123);
assert_eq!(Natural::rounding_from(123.9, RoundingMode::Ceiling), 124);
assert_eq!(Natural::rounding_from(123.9, RoundingMode::Nearest), 124);
assert_eq!(Natural::rounding_from(123.5, RoundingMode::Nearest), 124);
assert_eq!(Natural::rounding_from(124.5, RoundingMode::Nearest), 124);
assert_eq!(Natural::rounding_from(-0.99, RoundingMode::Ceiling), 0);
assert_eq!(Natural::rounding_from(-0.499, RoundingMode::Nearest), 0);
assert_eq!(Natural::rounding_from(-0.5, RoundingMode::Nearest), 0);

from

use malachite_nz::natural::Natural;

assert_eq!(Natural::from(0.0), 0);
assert_eq!(Natural::from(-0.0), 0);
assert_eq!(Natural::from(123.0), 123);
assert_eq!(Natural::from(1.0e9), 1000000000);
assert_eq!(Natural::from(4294967295.0), 4294967295u32);
assert_eq!(Natural::from(4294967296.0), 4294967296u64);
assert_eq!(
    Natural::from(1.0e100).to_string(),
    "10000000000000000159028911097599180468360808563945281389781327557747838772170381060813469\
    985856815104"
);
assert_eq!(Natural::from(123.1), 123);
assert_eq!(Natural::from(123.9), 124);
assert_eq!(Natural::from(123.5), 124);
assert_eq!(Natural::from(124.5), 124);
assert_eq!(Natural::from(-0.499), 0);
assert_eq!(Natural::from(-0.5), 0);

checked_from

extern crate malachite_base;

use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::conversion::traits::CheckedFrom;
use malachite_base::strings::ToDebugString;
use malachite_nz::natural::Natural;

assert_eq!(Natural::checked_from(f64::NAN).to_debug_string(), "None");
assert_eq!(Natural::checked_from(f64::POSITIVE_INFINITY).to_debug_string(), "None");
assert_eq!(Natural::checked_from(f64::NEGATIVE_INFINITY).to_debug_string(), "None");
assert_eq!(Natural::checked_from(0.0).to_debug_string(), "Some(0)");
assert_eq!(Natural::checked_from(-0.0).to_debug_string(), "Some(0)");
assert_eq!(Natural::checked_from(123.0).to_debug_string(), "Some(123)");
assert_eq!(Natural::checked_from(1.0e9).to_debug_string(), "Some(1000000000)");
assert_eq!(Natural::checked_from(4294967295.0).to_debug_string(), "Some(4294967295)");
assert_eq!(Natural::checked_from(4294967296.0).to_debug_string(), "Some(4294967296)");
assert_eq!(
    Natural::checked_from(1.0e100).to_debug_string(),
    "Some(100000000000000001590289110975991804683608085639452813897813275577478387721703810608\
    13469985856815104)"
);
assert_eq!(Natural::checked_from(123.1).to_debug_string(), "None");
assert_eq!(Natural::checked_from(123.9).to_debug_string(), "None");
assert_eq!(Natural::checked_from(123.5).to_debug_string(), "None");
assert_eq!(Natural::checked_from(124.5).to_debug_string(), "None");
assert_eq!(Natural::checked_from(-0.499).to_debug_string(), "None");
assert_eq!(Natural::checked_from(-0.5).to_debug_string(), "None");
assert_eq!(Natural::checked_from(-123.0).to_debug_string(), "None");

convertible_from

extern crate malachite_base;

use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::conversion::traits::ConvertibleFrom;
use malachite_nz::natural::Natural;

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