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
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
use malachite_base::num::conversion::traits::RoundingFrom;
use malachite_base::rounding_modes::RoundingMode;
use malachite_q::Rational;
use std::cmp::Ordering;
assert_eq!(
u32::rounding_from(&Rational::from(123), RoundingMode::Exact),
(123, Ordering::Equal)
);
assert_eq!(
u32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Floor),
(3, Ordering::Less)
);
assert_eq!(
u32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Down),
(3, Ordering::Less)
);
assert_eq!(
u32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Ceiling),
(4, Ordering::Greater)
);
assert_eq!(
u32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Up),
(4, Ordering::Greater)
);
assert_eq!(
u32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Nearest),
(3, Ordering::Less)
);
assert_eq!(
u32::rounding_from(&Rational::from(-123), RoundingMode::Down),
(0, Ordering::Greater)
);
assert_eq!(
u32::rounding_from(&Rational::from(-123), RoundingMode::Ceiling),
(0, Ordering::Greater)
);
assert_eq!(
u32::rounding_from(&Rational::from(-123), RoundingMode::Nearest),
(0, Ordering::Greater)
);
assert_eq!(
u8::rounding_from(&Rational::from(1000), RoundingMode::Down),
(255, Ordering::Less)
);
assert_eq!(
u8::rounding_from(&Rational::from(1000), RoundingMode::Floor),
(255, Ordering::Less)
);
assert_eq!(
u8::rounding_from(&Rational::from(1000), RoundingMode::Nearest),
(255, Ordering::Less)
);
assert_eq!(
i32::rounding_from(&Rational::from(-123), RoundingMode::Exact),
(-123, Ordering::Equal)
);
assert_eq!(
i32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Floor),
(3, Ordering::Less)
);
assert_eq!(
i32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Down),
(3, Ordering::Less)
);
assert_eq!(
i32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Ceiling),
(4, Ordering::Greater)
);
assert_eq!(
i32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Up),
(4, Ordering::Greater))
;
assert_eq!(
i32::rounding_from(&Rational::from_signeds(22, 7), RoundingMode::Nearest),
(3, Ordering::Less)
);
assert_eq!(
i32::rounding_from(&Rational::from_signeds(-22, 7), RoundingMode::Floor),
(-4, Ordering::Less)
);
assert_eq!(
i32::rounding_from(&Rational::from_signeds(-22, 7), RoundingMode::Down),
(-3, Ordering::Greater)
);
assert_eq!(
i32::rounding_from(&Rational::from_signeds(-22, 7), RoundingMode::Ceiling),
(-3, Ordering::Greater)
);
assert_eq!(
i32::rounding_from(&Rational::from_signeds(-22, 7), RoundingMode::Up),
(-4, Ordering::Less)
);
assert_eq!(
i32::rounding_from(&Rational::from_signeds(-22, 7), RoundingMode::Nearest),
(-3, Ordering::Greater)
);
assert_eq!(
i8::rounding_from(&Rational::from(-1000), RoundingMode::Down),
(-128, Ordering::Greater)
);
assert_eq!(
i8::rounding_from(&Rational::from(-1000), RoundingMode::Ceiling),
(-128, Ordering::Greater)
);
assert_eq!(
i8::rounding_from(&Rational::from(-1000), RoundingMode::Nearest),
(-128, Ordering::Greater)
);
assert_eq!(
i8::rounding_from(&Rational::from(1000), RoundingMode::Down),
(127, Ordering::Less)
);
assert_eq!(
i8::rounding_from(&Rational::from(1000), RoundingMode::Floor),
(127, Ordering::Less)
);
assert_eq!(
i8::rounding_from(&Rational::from(1000), RoundingMode::Nearest),
(127, Ordering::Less)
);