Expand description

Implementations of traits for converting a Rational to a primitive integer.

The traits are TryFrom, ConvertibleFrom, and RoundingFrom.

try_from

use malachite_q::conversion::primitive_int_from_rational::{
    SignedFromRationalError,
    UnsignedFromRationalError
};
use malachite_q::Rational;
use std::str::FromStr;

assert_eq!(u32::try_from(&Rational::from(123)).unwrap(), 123);
assert_eq!(u32::try_from(&Rational::from(-123)), Err(UnsignedFromRationalError));
assert_eq!(
    u32::try_from(&Rational::from_str("1000000000000").unwrap()),
    Err(UnsignedFromRationalError)
);
assert_eq!(u32::try_from(&Rational::from_signeds(22, 7)), Err(UnsignedFromRationalError));

assert_eq!(i32::try_from(&Rational::from(123)).unwrap(), 123);
assert_eq!(i32::try_from(&Rational::from(-123)).unwrap(), -123);
assert_eq!(
    i32::try_from(&Rational::from_str("-1000000000000").unwrap()),
    Err(SignedFromRationalError)
);
assert_eq!(
    i32::try_from(&Rational::from_str("1000000000000").unwrap()),
    Err(SignedFromRationalError)
);
assert_eq!(i32::try_from(&Rational::from_signeds(22, 7)), Err(SignedFromRationalError));

convertible_from

extern crate malachite_base;

use malachite_base::num::conversion::traits::ConvertibleFrom;
use malachite_q::Rational;
use std::str::FromStr;

assert_eq!(u32::convertible_from(&Rational::from(123)), true);
assert_eq!(u32::convertible_from(&Rational::from(-123)), false);
assert_eq!(u32::convertible_from(&Rational::from_str("1000000000000").unwrap()), false);
assert_eq!(u32::convertible_from(&Rational::from_signeds(22, 7)), false);

assert_eq!(i32::convertible_from(&Rational::from(123)), true);
assert_eq!(i32::convertible_from(&Rational::from(-123)), true);
assert_eq!(i32::convertible_from(&Rational::from_str("-1000000000000").unwrap()), false);
assert_eq!(i32::convertible_from(&Rational::from_str("1000000000000").unwrap()), false);
assert_eq!(i32::convertible_from(&Rational::from_signeds(22, 7)), false);

rounding_from

extern crate malachite_base;

use malachite_base::num::conversion::traits::RoundingFrom;
use malachite_base::rounding_modes::RoundingMode;
use malachite_q::Rational;

assert_eq!(u32::rounding_from(&Rational::from(123), RoundingMode::Exact), 123);

assert_eq!(u32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Floor), 3);
assert_eq!(u32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Down), 3);
assert_eq!(u32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Ceiling), 4);
assert_eq!(u32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Up), 4);
assert_eq!(u32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Nearest), 3);

assert_eq!(u32::rounding_from(&Rational::from(-123), RoundingMode::Down), 0);
assert_eq!(u32::rounding_from(&Rational::from(-123), RoundingMode::Ceiling), 0);
assert_eq!(u32::rounding_from(&Rational::from(-123), RoundingMode::Nearest), 0);

assert_eq!(u8::rounding_from(&Rational::from(1000), RoundingMode::Down), 255);
assert_eq!(u8::rounding_from(&Rational::from(1000), RoundingMode::Floor), 255);
assert_eq!(u8::rounding_from(&Rational::from(1000), RoundingMode::Nearest), 255);

assert_eq!(i32::rounding_from(&Rational::from(-123), RoundingMode::Exact), -123);

assert_eq!(i32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Floor), 3);
assert_eq!(i32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Down), 3);
assert_eq!(i32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Ceiling), 4);
assert_eq!(i32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Up), 4);
assert_eq!(i32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Nearest), 3);

assert_eq!(i32::rounding_from(&Rational::from_signeds(-22, 7), RoundingMode::Floor), -4);
assert_eq!(i32::rounding_from(&Rational::from_signeds(-22, 7), RoundingMode::Down), -3);
assert_eq!(i32::rounding_from(&Rational::from_signeds(-22, 7), RoundingMode::Ceiling), -3);
assert_eq!(i32::rounding_from(&Rational::from_signeds(-22, 7), RoundingMode::Up), -4);
assert_eq!(i32::rounding_from(&Rational::from_signeds(-22, 7), RoundingMode::Nearest), -3);

assert_eq!(i8::rounding_from(&Rational::from(-1000), RoundingMode::Down), -128);
assert_eq!(i8::rounding_from(&Rational::from(-1000), RoundingMode::Ceiling), -128);
assert_eq!(i8::rounding_from(&Rational::from(-1000), RoundingMode::Nearest), -128);

assert_eq!(i8::rounding_from(&Rational::from(1000), RoundingMode::Down), 127);
assert_eq!(i8::rounding_from(&Rational::from(1000), RoundingMode::Floor), 127);
assert_eq!(i8::rounding_from(&Rational::from(1000), RoundingMode::Nearest), 127);

Structs