Expand description
Implementations of traits for converting a Rational
to a primitive
integer.
The traits are CheckedFrom
,
ConvertibleFrom
, and
RoundingFrom
.
checked_from
extern crate malachite_base;
use malachite_base::num::conversion::traits::CheckedFrom;
use malachite_q::Rational;
use std::str::FromStr;
assert_eq!(u32::checked_from(&Rational::from(123)).unwrap(), 123);
assert_eq!(u32::checked_from(&Rational::from(-123)), None);
assert_eq!(u32::checked_from(&Rational::from_str("1000000000000").unwrap()), None);
assert_eq!(u32::checked_from(&Rational::from_signeds(22, 7)), None);
assert_eq!(i32::checked_from(&Rational::from(123)).unwrap(), 123);
assert_eq!(i32::checked_from(&Rational::from(-123)).unwrap(), -123);
assert_eq!(i32::checked_from(&Rational::from_str("-1000000000000").unwrap()), None);
assert_eq!(i32::checked_from(&Rational::from_str("1000000000000").unwrap()), None);
assert_eq!(i32::checked_from(&Rational::from_signeds(22, 7)), None);
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);