pub trait Ring: Clone + PartialEq + core::fmt::Debug {
fn zero() -> Self;
fn one() -> Self;
fn is_zero(&self) -> bool;
fn add_assign(&mut self, other: &Self);
fn mul(&self, other: &Self) -> Self;
fn neg(&self) -> Self;
fn from_i64(n: i64) -> Self;
fn from_u128(n: u128) -> Self {
Self::try_from_u128(n).unwrap_or_else(|| {
panic!(
"the structure constant {n} does not fit {}; use the bignum ring",
core::any::type_name::<Self>()
)
})
}
fn try_from_u128(n: u128) -> Option<Self> {
i64::try_from(n).ok().map(Self::from_i64)
}
fn as_ratio(&self) -> Option<(i128, i128)> {
None
}
fn from_ratio(_num: i128, _den: i128) -> Option<Self> {
None
}
fn from_i128(n: i128) -> Self {
Self::try_from_i128(n).unwrap_or_else(|| {
panic!(
"the character value {n} does not fit {}; use the bignum ring",
core::any::type_name::<Self>()
)
})
}
fn try_from_i128(n: i128) -> Option<Self> {
i64::try_from(n).ok().map(Self::from_i64)
}
fn sub_assign(&mut self, other: &Self) {
let neg = other.neg();
self.add_assign(&neg);
}
fn div_exact(&self, _other: &Self) -> Option<Self> {
None
}
}
pub trait Field: Ring {
fn inv(&self) -> Self;
fn div(&self, other: &Self) -> Self {
self.mul(&other.inv())
}
}
pub trait QAlgebra: Ring {
fn div_u128(&self, n: u128) -> Self;
}
pub trait Integral: Ring {
fn gcd(&self, other: &Self) -> Self;
fn is_negative(&self) -> bool;
}
pub trait Plethystic: QAlgebra {
fn frobenius(&self, n: u32) -> Self;
}
macro_rules! impl_ring_for_int {
($($t:ty => $div_exact:ident),*) => {$(
impl Ring for $t {
#[inline] fn zero() -> Self { 0 }
#[inline] fn one() -> Self { 1 }
#[inline] fn is_zero(&self) -> bool { *self == 0 }
#[inline] fn add_assign(&mut self, other: &Self) { *self += *other; }
#[inline] fn mul(&self, other: &Self) -> Self { *self * *other }
#[inline] fn neg(&self) -> Self { -*self }
#[inline] fn from_i64(n: i64) -> Self { n as $t }
#[inline] fn try_from_u128(n: u128) -> Option<Self> {
<$t>::try_from(n).ok()
}
#[inline] fn try_from_i128(n: i128) -> Option<Self> {
<$t>::try_from(n).ok()
}
#[inline] fn div_exact(&self, other: &Self) -> Option<Self> {
$div_exact(*self, *other)
}
}
)*};
}
impl_ring_for_int!(i64 => div_exact_i64, i128 => div_exact_i128);
macro_rules! impl_integral_for_int {
($($t:ty),*) => {$(
impl Integral for $t {
#[inline]
fn gcd(&self, other: &Self) -> Self {
let g = gcd_u128(self.unsigned_abs() as u128, other.unsigned_abs() as u128);
<$t>::try_from(g).unwrap_or_else(|_| {
panic!("the gcd {g} does not fit {}", stringify!($t))
})
}
#[inline]
fn is_negative(&self) -> bool {
*self < 0
}
}
)*};
}
impl_integral_for_int!(i64, i128);
#[inline]
fn div_exact_i64(a: i64, b: i64) -> Option<i64> {
(b != 0 && a % b == 0).then(|| a / b)
}
#[inline]
pub(crate) fn div_exact_i128(a: i128, b: i128) -> Option<i128> {
if b == 0 {
return None;
}
if let (Ok(a64), Ok(b64)) = (i64::try_from(a), i64::try_from(b)) {
if b64 == -1 {
return Some(-a);
}
return (a64 % b64 == 0).then(|| i128::from(a64 / b64));
}
(a % b == 0).then(|| a / b)
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Rational {
num: i128,
den: i128,
}
const NO_NEGATION: &str =
"a Rational part of i128::MIN has no negation in i128; use the bignum ring";
pub(crate) fn gcd_u128(a: u128, b: u128) -> u128 {
if (a | b) >> 64 == 0 {
#[allow(clippy::cast_possible_truncation)]
let (mut a, mut b) = (a as u64, b as u64);
while b != 0 {
let t = a % b;
a = b;
b = t;
}
return u128::from(a);
}
gcd_u128_wide(a, b)
}
fn gcd_u128_wide(mut a: u128, mut b: u128) -> u128 {
if a == 0 {
return b;
}
if b == 0 {
return a;
}
let shift = (a | b).trailing_zeros();
a >>= a.trailing_zeros();
loop {
b >>= b.trailing_zeros();
if a > b {
core::mem::swap(&mut a, &mut b);
}
b -= a;
if b == 0 {
break;
}
}
a << shift
}
#[inline]
pub(crate) fn quo(a: i128, g: i128) -> i128 {
if g == 1 {
return a;
}
if let (Ok(a64), Ok(g64)) = (i64::try_from(a), i64::try_from(g)) {
return i128::from(a64 / g64);
}
a / g
}
pub(crate) trait Overflow {
fn add(a: i128, b: i128) -> i128;
fn mul(a: i128, b: i128) -> i128;
}
pub(crate) struct Panics;
impl Overflow for Panics {
#[inline]
fn add(a: i128, b: i128) -> i128 {
a + b
}
#[inline]
fn mul(a: i128, b: i128) -> i128 {
a * b
}
}
#[inline]
#[allow(clippy::cast_possible_wrap)]
pub(crate) fn gcd_i128(a: i128, b: i128) -> i128 {
gcd_u128(a.unsigned_abs(), b.unsigned_abs()) as i128
}
#[inline]
pub(crate) fn rat_normalize(num: i128, den: i128) -> (i128, i128) {
let (mut n, mut d) = (num, den);
if d < 0 {
n = -n;
d = -d;
}
if n == 0 {
return (0, 1);
}
if d == 1 {
return (n, 1);
}
let g = gcd_i128(n, d);
(quo(n, g), quo(d, g))
}
#[inline]
pub(crate) fn rat_add<P: Overflow>(a: i128, b: i128, c: i128, d: i128) -> (i128, i128) {
if b == 1 && d == 1 {
return (P::add(a, c), 1);
}
if b == d {
let t = P::add(a, c);
if t == 0 {
return (0, 1);
}
let g = gcd_i128(t, b);
return (quo(t, g), quo(b, g));
}
let g = gcd_i128(b, d);
if g == 1 {
return (P::add(P::mul(a, d), P::mul(c, b)), P::mul(b, d));
}
let (bg, dg) = (quo(b, g), quo(d, g));
let t = P::add(P::mul(a, dg), P::mul(c, bg));
if t == 0 {
return (0, 1);
}
let g2 = gcd_i128(t, g);
(quo(t, g2), P::mul(bg, quo(d, g2)))
}
#[inline]
pub(crate) fn rat_mul<P: Overflow>(a: i128, b: i128, c: i128, d: i128) -> (i128, i128) {
if b == 1 && d == 1 {
return (P::mul(a, c), 1);
}
if a == 0 || c == 0 {
return (0, 1);
}
let g1 = gcd_i128(a, d);
let g2 = gcd_i128(c, b);
(
P::mul(quo(a, g1), quo(c, g2)),
P::mul(quo(b, g2), quo(d, g1)),
)
}
#[inline]
pub(crate) fn rat_div<P: Overflow>(a: i128, b: i128, n: i128) -> (i128, i128) {
let g = gcd_i128(a, n);
(quo(a, g), P::mul(b, quo(n, g)))
}
impl Rational {
pub fn new(num: i128, den: i128) -> Self {
assert!(den != 0, "Rational with zero denominator");
assert!(num != i128::MIN && den != i128::MIN, "{NO_NEGATION}");
let (num, den) = rat_normalize(num, den);
Rational { num, den }
}
pub fn from_int(n: i128) -> Self {
Rational { num: n, den: 1 }
}
pub fn numer(&self) -> i128 {
self.num
}
pub fn denom(&self) -> i128 {
self.den
}
}
impl core::fmt::Debug for Rational {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if self.den == 1 {
write!(f, "{}", self.num)
} else {
write!(f, "{}/{}", self.num, self.den)
}
}
}
macro_rules! impl_ring_ops {
([$($g:tt)*] $t:ty) => {
impl<$($g)*> core::ops::Add<&$t> for $t {
type Output = $t;
fn add(mut self, rhs: &$t) -> $t {
$crate::coeff::Ring::add_assign(&mut self, rhs);
self
}
}
impl<$($g)*> core::ops::Add<$t> for $t {
type Output = $t;
fn add(self, rhs: $t) -> $t {
self + &rhs
}
}
impl<$($g)*> core::ops::Add<&$t> for &$t {
type Output = $t;
fn add(self, rhs: &$t) -> $t {
self.clone() + rhs
}
}
impl<$($g)*> core::ops::Add<$t> for &$t {
type Output = $t;
fn add(self, rhs: $t) -> $t {
rhs + self
}
}
impl<$($g)*> core::ops::Sub<&$t> for $t {
type Output = $t;
fn sub(mut self, rhs: &$t) -> $t {
$crate::coeff::Ring::sub_assign(&mut self, rhs);
self
}
}
impl<$($g)*> core::ops::Sub<$t> for $t {
type Output = $t;
fn sub(self, rhs: $t) -> $t {
self - &rhs
}
}
impl<$($g)*> core::ops::Sub<&$t> for &$t {
type Output = $t;
fn sub(self, rhs: &$t) -> $t {
self.clone() - rhs
}
}
impl<$($g)*> core::ops::Sub<$t> for &$t {
type Output = $t;
fn sub(self, rhs: $t) -> $t {
let mut d = $crate::coeff::Ring::neg(&rhs);
$crate::coeff::Ring::add_assign(&mut d, self);
d
}
}
impl<$($g)*> core::ops::Neg for $t {
type Output = $t;
fn neg(self) -> $t {
$crate::coeff::Ring::neg(&self)
}
}
impl<$($g)*> core::ops::Neg for &$t {
type Output = $t;
fn neg(self) -> $t {
$crate::coeff::Ring::neg(self)
}
}
impl<$($g)*> core::ops::Mul<&$t> for $t {
type Output = $t;
fn mul(self, rhs: &$t) -> $t {
$crate::coeff::Ring::mul(&self, rhs)
}
}
impl<$($g)*> core::ops::Mul<$t> for $t {
type Output = $t;
fn mul(self, rhs: $t) -> $t {
$crate::coeff::Ring::mul(&self, &rhs)
}
}
impl<$($g)*> core::ops::Mul<&$t> for &$t {
type Output = $t;
fn mul(self, rhs: &$t) -> $t {
$crate::coeff::Ring::mul(self, rhs)
}
}
impl<$($g)*> core::ops::Mul<$t> for &$t {
type Output = $t;
fn mul(self, rhs: $t) -> $t {
$crate::coeff::Ring::mul(self, &rhs)
}
}
impl<$($g)*> core::ops::AddAssign<&$t> for $t {
fn add_assign(&mut self, rhs: &$t) {
$crate::coeff::Ring::add_assign(self, rhs);
}
}
impl<$($g)*> core::ops::AddAssign<$t> for $t {
fn add_assign(&mut self, rhs: $t) {
$crate::coeff::Ring::add_assign(self, &rhs);
}
}
impl<$($g)*> core::ops::SubAssign<&$t> for $t {
fn sub_assign(&mut self, rhs: &$t) {
$crate::coeff::Ring::sub_assign(self, rhs);
}
}
impl<$($g)*> core::ops::SubAssign<$t> for $t {
fn sub_assign(&mut self, rhs: $t) {
$crate::coeff::Ring::sub_assign(self, &rhs);
}
}
impl<$($g)*> core::ops::MulAssign<&$t> for $t {
fn mul_assign(&mut self, rhs: &$t) {
*self = $crate::coeff::Ring::mul(self, rhs);
}
}
impl<$($g)*> core::ops::MulAssign<$t> for $t {
fn mul_assign(&mut self, rhs: $t) {
*self = $crate::coeff::Ring::mul(self, &rhs);
}
}
};
}
macro_rules! impl_field_ops {
([$($g:tt)*] $t:ty) => {
impl<$($g)*> core::ops::Div<&$t> for $t {
type Output = $t;
fn div(self, rhs: &$t) -> $t {
$crate::coeff::Field::div(&self, rhs)
}
}
impl<$($g)*> core::ops::Div<$t> for $t {
type Output = $t;
fn div(self, rhs: $t) -> $t {
$crate::coeff::Field::div(&self, &rhs)
}
}
impl<$($g)*> core::ops::Div<&$t> for &$t {
type Output = $t;
fn div(self, rhs: &$t) -> $t {
$crate::coeff::Field::div(self, rhs)
}
}
impl<$($g)*> core::ops::Div<$t> for &$t {
type Output = $t;
fn div(self, rhs: $t) -> $t {
$crate::coeff::Field::div(self, &rhs)
}
}
impl<$($g)*> core::ops::DivAssign<&$t> for $t {
fn div_assign(&mut self, rhs: &$t) {
*self = $crate::coeff::Field::div(self, rhs);
}
}
impl<$($g)*> core::ops::DivAssign<$t> for $t {
fn div_assign(&mut self, rhs: $t) {
*self = $crate::coeff::Field::div(self, &rhs);
}
}
};
}
pub(crate) use impl_ring_ops;
impl_ring_ops!([] Rational);
impl_field_ops!([] Rational);
impl Ring for Rational {
fn zero() -> Self {
Rational { num: 0, den: 1 }
}
fn one() -> Self {
Rational { num: 1, den: 1 }
}
fn is_zero(&self) -> bool {
self.num == 0
}
fn add_assign(&mut self, other: &Self) {
let (num, den) = rat_add::<Panics>(self.num, self.den, other.num, other.den);
*self = Rational { num, den };
}
fn mul(&self, other: &Self) -> Self {
let (num, den) = rat_mul::<Panics>(self.num, self.den, other.num, other.den);
Rational { num, den }
}
fn neg(&self) -> Self {
assert!(self.num != i128::MIN, "{NO_NEGATION}");
Rational {
num: -self.num,
den: self.den,
}
}
fn from_i64(n: i64) -> Self {
Rational::from_int(n as i128)
}
fn try_from_u128(n: u128) -> Option<Self> {
i128::try_from(n).ok().map(Rational::from_int)
}
fn try_from_i128(n: i128) -> Option<Self> {
Some(Rational::from_int(n))
}
fn as_ratio(&self) -> Option<(i128, i128)> {
Some((self.num, self.den))
}
fn from_ratio(num: i128, den: i128) -> Option<Self> {
(den != 0).then(|| Rational::new(num, den))
}
fn div_exact(&self, other: &Self) -> Option<Self> {
(!other.is_zero()).then(|| self.mul(&other.inv()))
}
}
impl Integral for Rational {
fn gcd(&self, other: &Self) -> Self {
if self.is_zero() && other.is_zero() {
<Self as Ring>::zero()
} else {
<Self as Ring>::one()
}
}
fn is_negative(&self) -> bool {
self.num < 0
}
}
impl Field for Rational {
fn inv(&self) -> Self {
assert!(self.num != 0, "inverse of zero Rational");
Rational::new(self.den, self.num)
}
}
impl Plethystic for Rational {
fn frobenius(&self, _n: u32) -> Self {
*self
}
}
impl QAlgebra for Rational {
fn div_u128(&self, n: u128) -> Self {
assert!(n != 0, "division of Rational by zero");
let n = i128::try_from(n)
.unwrap_or_else(|_| panic!("a divisor of {n} is past i128::MAX; use the bignum ring"));
let (num, den) = rat_div::<Panics>(self.num, self.den, n);
Rational { num, den }
}
}
#[cfg(feature = "bignum")]
mod bignum_impls {
use super::{Field, Integral, Plethystic, QAlgebra, Ring};
use num_bigint::BigInt;
use num_rational::BigRational;
use num_traits::{One, Signed, Zero};
impl Ring for BigInt {
fn zero() -> Self {
<BigInt as Zero>::zero()
}
fn one() -> Self {
<BigInt as One>::one()
}
fn is_zero(&self) -> bool {
Zero::is_zero(self)
}
fn add_assign(&mut self, other: &Self) {
*self += other;
}
fn mul(&self, other: &Self) -> Self {
self * other
}
fn neg(&self) -> Self {
-self
}
fn from_i64(n: i64) -> Self {
BigInt::from(n)
}
fn try_from_u128(n: u128) -> Option<Self> {
Some(BigInt::from(n)) }
fn try_from_i128(n: i128) -> Option<Self> {
Some(BigInt::from(n)) }
fn div_exact(&self, other: &Self) -> Option<Self> {
(!Zero::is_zero(other) && Zero::is_zero(&(self % other))).then(|| self / other)
}
}
impl Integral for BigInt {
fn gcd(&self, other: &Self) -> Self {
let (mut a, mut b) = (self.abs(), other.abs());
while !Zero::is_zero(&b) {
let t = a % &b;
a = b;
b = t;
}
a
}
fn is_negative(&self) -> bool {
Signed::is_negative(self)
}
}
impl Ring for BigRational {
fn zero() -> Self {
<BigRational as Zero>::zero()
}
fn one() -> Self {
<BigRational as One>::one()
}
fn is_zero(&self) -> bool {
Zero::is_zero(self)
}
fn add_assign(&mut self, other: &Self) {
*self += other;
}
fn mul(&self, other: &Self) -> Self {
self * other
}
fn neg(&self) -> Self {
-self
}
fn from_i64(n: i64) -> Self {
BigRational::from(BigInt::from(n))
}
fn try_from_u128(n: u128) -> Option<Self> {
Some(BigRational::from(BigInt::from(n))) }
fn try_from_i128(n: i128) -> Option<Self> {
Some(BigRational::from(BigInt::from(n))) }
fn div_exact(&self, other: &Self) -> Option<Self> {
(!Zero::is_zero(other)).then(|| self / other)
}
}
impl Integral for BigRational {
fn gcd(&self, other: &Self) -> Self {
if Zero::is_zero(self) && Zero::is_zero(other) {
<Self as Ring>::zero()
} else {
<Self as Ring>::one()
}
}
fn is_negative(&self) -> bool {
Signed::is_negative(self)
}
}
impl Field for BigRational {
fn inv(&self) -> Self {
assert!(!Zero::is_zero(self), "inverse of zero BigRational");
self.recip()
}
}
impl QAlgebra for BigRational {
fn div_u128(&self, n: u128) -> Self {
assert!(n != 0, "division of BigRational by zero");
self / BigRational::from(BigInt::from(n))
}
}
impl Plethystic for BigRational {
fn frobenius(&self, _n: u32) -> Self {
self.clone()
}
}
pub fn _assert_bounds() {
fn ring<T: Ring>() {}
fn qalg<T: QAlgebra>() {}
ring::<BigInt>();
ring::<BigRational>();
qalg::<BigRational>();
}
pub fn fits_i128(v: &BigInt) -> bool {
v.abs() <= BigInt::from(i128::MAX)
}
}
#[cfg(feature = "bignum")]
pub use bignum_impls::fits_i128;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fixed_width_injections_refuse_rather_than_truncate() {
assert_eq!(
<i64 as Ring>::from_u128(u64::MAX as u128 / 2),
9223372036854775807
);
assert_eq!(
<i128 as Ring>::from_u128(u128::MAX / 2),
170141183460469231731687303715884105727
);
assert_eq!(
<Rational as Ring>::from_u128(1 << 100),
Rational::from_int(1 << 100)
);
for (name, f) in [
(
"i64 from_u128",
(|| {
<i64 as Ring>::from_u128(u64::MAX as u128 + 1);
}) as fn(),
),
("i64 from_i128", || {
<i64 as Ring>::from_i128(i64::MAX as i128 + 1);
}),
("i128 from_u128", || {
<i128 as Ring>::from_u128(i128::MAX as u128 + 1);
}),
("Rational from_u128", || {
<Rational as Ring>::from_u128(i128::MAX as u128 + 1);
}),
] {
let out = std::panic::catch_unwind(f);
assert!(out.is_err(), "{name} must refuse a constant past its width");
}
}
#[test]
#[should_panic(expected = "no negation in i128")]
fn rational_refuses_the_width_minimum_as_a_numerator() {
let _ = Rational::new(i128::MIN, 3);
}
#[test]
#[should_panic(expected = "no negation in i128")]
fn negating_a_width_minimum_rational_refuses() {
let min = Rational::from_int(i128::MIN);
let _ = min.neg();
}
#[test]
#[should_panic(expected = "past i128::MAX")]
fn dividing_by_a_divisor_past_the_width_refuses() {
let _ = Rational::one().div_u128(u128::MAX);
}
#[test]
fn rational_arithmetic() {
let mut a = Rational::new(1, 2);
a.add_assign(&Rational::new(1, 3)); assert_eq!(a, Rational::new(5, 6));
assert_eq!(a.mul(&Rational::new(6, 5)), Rational::one());
assert_eq!(Rational::new(2, 4), Rational::new(1, 2)); assert_eq!(Rational::new(3, -6), Rational::new(-1, 2)); }
#[test]
fn shortcut_arithmetic_matches_reduce_after_the_fact() {
fn plain_gcd(mut a: i128, mut b: i128) -> i128 {
(a, b) = (a.abs(), b.abs());
while b != 0 {
(a, b) = (b, a % b);
}
a
}
fn reduce(n: i128, d: i128) -> (i128, i128) {
if n == 0 {
return (0, 1);
}
let g = plain_gcd(n, d);
let (n, d) = (n / g, d / g);
if d < 0 {
(-n, -d)
} else {
(n, d)
}
}
let nums = [-7i128, -6, -1, 0, 1, 2, 3, 5, 6, 12, 35, 1 << 40];
let dens = [1i128, 2, 3, 4, 6, 7, 12, 30, 1 << 40, (1 << 40) + 1];
for &a in &nums {
for &b in &dens {
for &c in &nums {
for &d in &dens {
let x = Rational::new(a, b);
let y = Rational::new(c, d);
let mut sum = x;
sum.add_assign(&y);
assert_eq!(
(sum.num, sum.den),
reduce(a * d + c * b, b * d),
"{a}/{b} + {c}/{d}"
);
let prod = x.mul(&y);
assert_eq!(
(prod.num, prod.den),
reduce(a * c, b * d),
"{a}/{b} * {c}/{d}"
);
for n in [1u128, 2, 6, 7, 1 << 40] {
let q = x.div_u128(n);
assert_eq!(
(q.num, q.den),
reduce(a, b * i128::try_from(n).unwrap()),
"{a}/{b} / {n}"
);
}
}
}
}
}
}
#[test]
fn narrowed_gcd_and_quotient_agree_with_the_wide_forms() {
fn euclid(mut a: u128, mut b: u128) -> u128 {
while b != 0 {
(a, b) = (b, a % b);
}
a
}
let p = 1_000_000_007u128;
let q = 998_244_353u128;
let r = (1u128 << 61) - 1;
let cases = [
(0u128, 0u128),
(0, 5),
(12, 18),
(u64::MAX as u128, 6),
(u64::MAX as u128 + 1, 6),
(p * q, q * r),
(p * r * 8, q * r * 12),
(r * r, r * p),
(u128::MAX / 3, u128::MAX / 5),
];
for &(a, b) in &cases {
assert_eq!(gcd_u128(a, b), euclid(a, b), "gcd({a}, {b})");
assert_eq!(gcd_u128(b, a), euclid(a, b), "gcd({b}, {a})");
}
for &(a, g) in &[
(36i128, 6i128),
(-36, 6),
(i128::from(i64::MAX) * 4, 4),
(i128::from(i64::MIN) * 3, 3),
(i128::from(i64::MIN), 1),
(i128::MAX, i128::MAX),
] {
assert_eq!(quo(a, g), a / g, "{a} / {g}");
}
assert_eq!(
div_exact_i128(i128::from(i64::MIN), -1),
Some(-i128::from(i64::MIN))
);
assert_eq!(div_exact_i128(7, 0), None);
assert_eq!(div_exact_i128(7, 2), None);
assert_eq!(div_exact_i128(i128::MAX - 1, 2), Some((i128::MAX - 1) / 2));
assert_eq!(
div_exact_i128(-(1i128 << 100), 1 << 30),
Some(-(1i128 << 70))
);
}
#[test]
fn rational_field_ops() {
let a = Rational::new(3, 7);
assert_eq!(a.mul(&a.inv()), Rational::one());
assert_eq!(
Rational::new(1, 2).div(&Rational::new(3, 4)),
Rational::new(2, 3)
);
let mut b = Rational::from_int(5);
b.sub_assign(&Rational::new(1, 2)); assert_eq!(b, Rational::new(9, 2));
}
}