Expand description

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

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

rounding_from

extern crate malachite_base;

use malachite_base::num::arithmetic::traits::PowerOf2;
use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::basic::traits::OneHalf;
use malachite_base::num::conversion::traits::RoundingFrom;
use malachite_base::num::float::NiceFloat;
use malachite_base::rounding_modes::RoundingMode;
use malachite_q::Rational;

let one_third = Rational::from_signeds(1i8, 3);
assert_eq!(f32::rounding_from(one_third.clone(), RoundingMode::Floor), 0.3333333);
assert_eq!(f32::rounding_from(one_third, RoundingMode::Ceiling), 0.33333334);
assert_eq!(f32::rounding_from(Rational::ONE_HALF, RoundingMode::Exact), 0.5);
let big = Rational::power_of_2(200u64);
assert_eq!(f32::rounding_from(big.clone(), RoundingMode::Down), f32::MAX_FINITE);
assert_eq!(f32::rounding_from(big, RoundingMode::Up), f32::POSITIVE_INFINITY);
let small = Rational::power_of_2(-200i64);
assert_eq!(NiceFloat(f32::rounding_from(small.clone(), RoundingMode::Down)), NiceFloat(0.0));
assert_eq!(f32::rounding_from(small.clone(), RoundingMode::Up), f32::MIN_POSITIVE_SUBNORMAL);
assert_eq!(NiceFloat(f32::rounding_from(-&small, RoundingMode::Down)), NiceFloat(-0.0));
assert_eq!(f32::rounding_from(-small, RoundingMode::Up), -f32::MIN_POSITIVE_SUBNORMAL);

let one_third = Rational::from_signeds(1i8, 3);
assert_eq!(f32::rounding_from(&one_third, RoundingMode::Floor), 0.3333333);
assert_eq!(f32::rounding_from(&one_third, RoundingMode::Ceiling), 0.33333334);
assert_eq!(f32::rounding_from(&Rational::ONE_HALF, RoundingMode::Exact), 0.5);
let big = Rational::power_of_2(200u64);
assert_eq!(f32::rounding_from(&big, RoundingMode::Down), f32::MAX_FINITE);
assert_eq!(f32::rounding_from(&big, RoundingMode::Up), f32::POSITIVE_INFINITY);
let small = Rational::power_of_2(-200i64);
assert_eq!(NiceFloat(f32::rounding_from(&small, RoundingMode::Down)), NiceFloat(0.0));
assert_eq!(f32::rounding_from(&small, RoundingMode::Up), f32::MIN_POSITIVE_SUBNORMAL);
assert_eq!(NiceFloat(f32::rounding_from(&-&small, RoundingMode::Down)), NiceFloat(-0.0));
assert_eq!(f32::rounding_from(&-small, RoundingMode::Up), -f32::MIN_POSITIVE_SUBNORMAL);

from

extern crate malachite_base;

use malachite_base::num::arithmetic::traits::PowerOf2;
use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::basic::traits::OneHalf;
use malachite_base::num::float::NiceFloat;
use malachite_q::Rational;

assert_eq!(f32::from(Rational::from_signeds(1i8, 3)), 0.33333334);
assert_eq!(f32::from(Rational::ONE_HALF), 0.5);
assert_eq!(f32::from(Rational::power_of_2(200u64)), f32::MAX_FINITE);
assert_eq!(NiceFloat(f32::from(Rational::power_of_2(-200i64))), NiceFloat(0.0));
assert_eq!(NiceFloat(f32::from(-Rational::power_of_2(-200i64))), NiceFloat(-0.0));

assert_eq!(f32::from(&Rational::from_signeds(1i8, 3)), 0.33333334);
assert_eq!(f32::from(&Rational::ONE_HALF), 0.5);
assert_eq!(f32::from(&Rational::power_of_2(200u64)), f32::MAX_FINITE);
assert_eq!(NiceFloat(f32::from(&Rational::power_of_2(-200i64))), NiceFloat(0.0));
assert_eq!(NiceFloat(f32::from(&-Rational::power_of_2(-200i64))), NiceFloat(-0.0));

checked_from

extern crate malachite_base;

use malachite_base::num::arithmetic::traits::PowerOf2;
use malachite_base::num::conversion::traits::CheckedFrom;
use malachite_base::num::basic::traits::OneHalf;
use malachite_q::Rational;

assert_eq!(f32::checked_from(Rational::from_signeds(1i8, 3)), None);
assert_eq!(f32::checked_from(Rational::ONE_HALF), Some(0.5));
assert_eq!(f32::checked_from(Rational::power_of_2(200u64)), None);
assert_eq!(f32::checked_from(Rational::power_of_2(-200i64)), None);
assert_eq!(f32::checked_from(-Rational::power_of_2(-200i64)), None);

assert_eq!(f32::checked_from(&Rational::from_signeds(1i8, 3)), None);
assert_eq!(f32::checked_from(&Rational::ONE_HALF), Some(0.5));
assert_eq!(f32::checked_from(&Rational::power_of_2(200u64)), None);
assert_eq!(f32::checked_from(&Rational::power_of_2(-200i64)), None);
assert_eq!(f32::checked_from(&-Rational::power_of_2(-200i64)), None);

convertible_from

extern crate malachite_base;

use malachite_base::num::arithmetic::traits::PowerOf2;
use malachite_base::num::conversion::traits::ConvertibleFrom;
use malachite_base::num::basic::traits::OneHalf;
use malachite_q::Rational;

assert_eq!(f32::convertible_from(Rational::from_signeds(1i8, 3)), false);
assert_eq!(f32::convertible_from(Rational::ONE_HALF), true);
assert_eq!(f32::convertible_from(Rational::power_of_2(200u64)), false);
assert_eq!(f32::convertible_from(Rational::power_of_2(-200i64)), false);
assert_eq!(f32::convertible_from(-Rational::power_of_2(-200i64)), false);

assert_eq!(f32::convertible_from(&Rational::from_signeds(1i8, 3)), false);
assert_eq!(f32::convertible_from(&Rational::ONE_HALF), true);
assert_eq!(f32::convertible_from(&Rational::power_of_2(200u64)), false);
assert_eq!(f32::convertible_from(&Rational::power_of_2(-200i64)), false);
assert_eq!(f32::convertible_from(&-Rational::power_of_2(-200i64)), false);