Expand description

Functions and implementations of traits for converting a Float to a primitive float.

The traits are TryFrom, ConvertibleFrom, and RoundingFrom.

§convertible_from

use malachite_base::num::arithmetic::traits::PowerOf2;
use malachite_base::num::basic::traits::{Infinity, NaN, Zero};
use malachite_base::num::conversion::traits::ConvertibleFrom;
use malachite_float::Float;

assert_eq!(f32::convertible_from(&Float::NAN), true);
assert_eq!(f32::convertible_from(&Float::INFINITY), true);
assert_eq!(f32::convertible_from(&Float::ZERO), true);
assert_eq!(f32::convertible_from(&Float::from(1.5)), true);
assert_eq!(f32::convertible_from(&Float::from(-1.5)), true);
assert_eq!(f32::convertible_from(&Float::from(123.0)), true);
assert_eq!(f32::convertible_from(&Float::from(-123.0)), true);

// Even though precision is high, the value is just 1.0 and can be converted
assert_eq!(f32::convertible_from(&Float::one_prec(100)), true);

let mut x = Float::one_prec(40);
x.increment();

// precision too high for f32
assert_eq!(f32::convertible_from(&x), false);

// but not for f64
assert_eq!(f64::convertible_from(&x), true);

assert_eq!(f32::convertible_from(&Float::power_of_2(100u64)), true);
assert_eq!(f32::convertible_from(&Float::power_of_2(1000u64)), false);
assert_eq!(f64::convertible_from(&Float::power_of_2(1000u64)), true);
assert_eq!(f64::convertible_from(&Float::power_of_2(10000u64)), false);

§try_from

use malachite_base::num::arithmetic::traits::PowerOf2;
use malachite_base::num::basic::traits::{Infinity, NaN, Zero};
use malachite_base::num::float::NiceFloat;
use malachite_float::conversion::primitive_float_from_float::FloatFromFloatError;
use malachite_float::Float;

assert_eq!(
    NiceFloat(f32::try_from(Float::NAN).unwrap()),
    NiceFloat(f32::NAN)
);
assert_eq!(f32::try_from(Float::INFINITY), Ok(f32::INFINITY));
assert_eq!(f32::try_from(Float::ZERO), Ok(0.0));
assert_eq!(f32::try_from(Float::from(1.5)), Ok(1.5));
assert_eq!(f32::try_from(Float::from(-1.5)), Ok(-1.5));
assert_eq!(f32::try_from(Float::from(123.0)), Ok(123.0));
assert_eq!(f32::try_from(Float::from(-123.0)), Ok(-123.0));

// Even though precision is high, the value is just 1.0 and can be converted
assert_eq!(f32::try_from(Float::one_prec(100)), Ok(1.0));

let mut x = Float::one_prec(40);
x.increment();

// precision too high for f32
assert_eq!(f32::try_from(x.clone()), Err(FloatFromFloatError::Inexact));

// but not for f64
assert_eq!(
    NiceFloat(f64::try_from(x).unwrap()),
    NiceFloat(1.000000000001819)
);

assert_eq!(
    NiceFloat(f32::try_from(Float::power_of_2(100u64)).unwrap()),
    NiceFloat(1.2676506e30)
);
assert_eq!(
    f32::try_from(Float::power_of_2(1000u64)),
    Err(FloatFromFloatError::Overflow)
);
assert_eq!(
    NiceFloat(f64::try_from(Float::power_of_2(1000u64)).unwrap()),
    NiceFloat(1.0715086071862673e301)
);
assert_eq!(
    f64::try_from(Float::power_of_2(10000u64)),
    Err(FloatFromFloatError::Overflow)
);

assert_eq!(
    NiceFloat(f32::try_from(&Float::NAN).unwrap()),
    NiceFloat(f32::NAN)
);
assert_eq!(f32::try_from(&Float::INFINITY), Ok(f32::INFINITY));
assert_eq!(f32::try_from(&Float::ZERO), Ok(0.0));
assert_eq!(f32::try_from(&Float::from(1.5)), Ok(1.5));
assert_eq!(f32::try_from(&Float::from(-1.5)), Ok(-1.5));
assert_eq!(f32::try_from(&Float::from(123.0)), Ok(123.0));
assert_eq!(f32::try_from(&Float::from(-123.0)), Ok(-123.0));

// Even though precision is high, the value is just 1.0 and can be converted
assert_eq!(f32::try_from(&Float::one_prec(100)), Ok(1.0));

let mut x = Float::one_prec(40);
x.increment();

// precision too high for f32
assert_eq!(f32::try_from(&x), Err(FloatFromFloatError::Inexact));

// but not for f64
assert_eq!(
    NiceFloat(f64::try_from(&x).unwrap()),
    NiceFloat(1.000000000001819)
);

assert_eq!(
    NiceFloat(f32::try_from(&Float::power_of_2(100u64)).unwrap()),
    NiceFloat(1.2676506e30)
);
assert_eq!(
    f32::try_from(&Float::power_of_2(1000u64)),
    Err(FloatFromFloatError::Overflow)
);
assert_eq!(
    NiceFloat(f64::try_from(&Float::power_of_2(1000u64)).unwrap()),
    NiceFloat(1.0715086071862673e301)
);
assert_eq!(
    f64::try_from(&Float::power_of_2(10000u64)),
    Err(FloatFromFloatError::Overflow)
);

§rounding_from

use malachite_base::num::conversion::traits::RoundingFrom;
use malachite_base::num::float::NiceFloat;
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_float::Float;
use malachite_q::Rational;
use std::cmp::Ordering::*;

let f = Float::from_rational_prec(Rational::from_signeds(1, 3), 100).0;

let (x, o) = f32::rounding_from(f.clone(), Floor);
assert_eq!(NiceFloat(x), NiceFloat(0.3333333));
assert_eq!(o, Less);

let (x, o) = f32::rounding_from(f.clone(), Ceiling);
assert_eq!(NiceFloat(x), NiceFloat(0.33333334));
assert_eq!(o, Greater);

let (x, o) = f32::rounding_from(f.clone(), Nearest);
assert_eq!(NiceFloat(x), NiceFloat(0.33333334));
assert_eq!(o, Greater);

let (x, o) = f32::rounding_from(&f, Floor);
assert_eq!(NiceFloat(x), NiceFloat(0.3333333));
assert_eq!(o, Less);

let (x, o) = f32::rounding_from(&f, Ceiling);
assert_eq!(NiceFloat(x), NiceFloat(0.33333334));
assert_eq!(o, Greater);

let (x, o) = f32::rounding_from(&f, Nearest);
assert_eq!(NiceFloat(x), NiceFloat(0.33333334));
assert_eq!(o, Greater);

Enums§