Simplify some Ratio methods

This commit is contained in:
Josh Stone 2016-02-22 18:09:21 -08:00
parent 5b2cb8df97
commit 8be7e7bab5
1 changed files with 13 additions and 9 deletions

View File

@ -361,7 +361,9 @@ impl<T> Neg for Ratio<T>
type Output = Ratio<T>; type Output = Ratio<T>;
#[inline] #[inline]
fn neg(self) -> Ratio<T> { -&self } fn neg(self) -> Ratio<T> {
Ratio::new_raw(-self.numer, self.denom)
}
} }
impl<'a, T> Neg for &'a Ratio<T> impl<'a, T> Neg for &'a Ratio<T>
@ -371,7 +373,7 @@ impl<'a, T> Neg for &'a Ratio<T>
#[inline] #[inline]
fn neg(self) -> Ratio<T> { fn neg(self) -> Ratio<T> {
Ratio::new_raw(-self.numer.clone(), self.denom.clone()) -self.clone()
} }
} }
@ -385,7 +387,7 @@ impl<T: Clone + Integer>
#[inline] #[inline]
fn is_zero(&self) -> bool { fn is_zero(&self) -> bool {
*self == Zero::zero() self.numer.is_zero()
} }
} }
@ -436,20 +438,22 @@ impl<T: Clone + Integer + Signed> Signed for Ratio<T> {
#[inline] #[inline]
fn signum(&self) -> Ratio<T> { fn signum(&self) -> Ratio<T> {
if *self > Zero::zero() { if self.is_positive() {
One::one() Self::one()
} else if self.is_zero() { } else if self.is_zero() {
Zero::zero() Self::zero()
} else { } else {
- ::one::<Ratio<T>>() - Self::one()
} }
} }
#[inline] #[inline]
fn is_positive(&self) -> bool { *self > Zero::zero() } fn is_positive(&self) -> bool { !self.is_negative() }
#[inline] #[inline]
fn is_negative(&self) -> bool { *self < Zero::zero() } fn is_negative(&self) -> bool {
self.numer.is_negative() ^ self.denom.is_negative()
}
} }
/* String conversions */ /* String conversions */