use super::{Rational, RationalScalar};
use core::cmp::Ordering;
impl<T: RationalScalar> PartialEq for Rational<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.numer() == other.numer() && self.denom() == other.denom()
}
}
impl<T: RationalScalar> Eq for Rational<T> {}
impl<T: RationalScalar> PartialOrd for Rational<T> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<T: RationalScalar> Ord for Rational<T> {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
if self.den == other.den {
return self.num.cmp(&other.num);
}
let (mut a, mut b) = (self.num, self.den);
let (mut c, mut d) = (other.num, other.den);
loop {
let q_lhs = a.div_euclid(b);
let q_rhs = c.div_euclid(d);
if q_lhs != q_rhs {
return q_lhs.cmp(&q_rhs);
}
let r_lhs = a.rem_euclid(b);
let r_rhs = c.rem_euclid(d);
if r_lhs.is_zero() {
return Ordering::Less;
}
if r_rhs.is_zero() {
return Ordering::Greater;
}
let (next_a, next_b, next_c, next_d) = (d, r_rhs, b, r_lhs);
a = next_a;
b = next_b;
c = next_c;
d = next_d;
}
}
}