Auto merge of #182 - ollie27:ratio_posneg, r=cuviper

Correct Ratio::is_negative and Ratio::is_positive

Zero is not positive or negative.

This was broken in 8be7e7bab5.
This commit is contained in:
Homu 2016-04-14 07:05:42 +09:00
commit 774bf31ec6
1 changed files with 20 additions and 2 deletions

View File

@ -526,12 +526,14 @@ impl<T: Clone + Integer + Signed> Signed for Ratio<T> {
#[inline] #[inline]
fn is_positive(&self) -> bool { fn is_positive(&self) -> bool {
!self.is_negative() (self.numer.is_positive() && self.denom.is_positive()) ||
(self.numer.is_negative() && self.denom.is_negative())
} }
#[inline] #[inline]
fn is_negative(&self) -> bool { fn is_negative(&self) -> bool {
self.numer.is_negative() ^ self.denom.is_negative() (self.numer.is_negative() && self.denom.is_positive()) ||
(self.numer.is_positive() && self.denom.is_negative())
} }
} }
@ -674,6 +676,14 @@ mod test {
numer: -1, numer: -1,
denom: 2, denom: 2,
}; };
pub const _1_NEG2: Rational = Ratio {
numer: 1,
denom: -2,
};
pub const _NEG1_NEG2: Rational = Ratio {
numer: -1,
denom: -2,
};
pub const _1_3: Rational = Ratio { pub const _1_3: Rational = Ratio {
numer: 1, numer: 1,
denom: 3, denom: 3,
@ -1081,9 +1091,17 @@ mod test {
assert_eq!(_1_2.abs_sub(&_3_2), Zero::zero()); assert_eq!(_1_2.abs_sub(&_3_2), Zero::zero());
assert_eq!(_1_2.signum(), One::one()); assert_eq!(_1_2.signum(), One::one());
assert_eq!(_NEG1_2.signum(), -<Ratio<isize>>::one()); assert_eq!(_NEG1_2.signum(), -<Ratio<isize>>::one());
assert_eq!(_0.signum(), Zero::zero());
assert!(_NEG1_2.is_negative()); assert!(_NEG1_2.is_negative());
assert!(_1_NEG2.is_negative());
assert!(!_NEG1_2.is_positive()); assert!(!_NEG1_2.is_positive());
assert!(!_1_NEG2.is_positive());
assert!(_1_2.is_positive());
assert!(_NEG1_NEG2.is_positive());
assert!(!_1_2.is_negative()); assert!(!_1_2.is_negative());
assert!(!_NEG1_NEG2.is_negative());
assert!(!_0.is_positive());
assert!(!_0.is_negative());
} }
#[test] #[test]