From c22e3bf9a2733274a2a6d422cbd35b5a135d63ff Mon Sep 17 00:00:00 2001 From: Oliver Middleton Date: Thu, 10 Mar 2016 21:44:37 +0000 Subject: [PATCH] Correct Ratio::is_negative and Ratio::is_positive Zero is not positive or negative. This was broken in 8be7e7bab5ffc7f998e713547b67c1a424d3b9b1. --- rational/src/lib.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/rational/src/lib.rs b/rational/src/lib.rs index f6e2cbd..47bb0b0 100644 --- a/rational/src/lib.rs +++ b/rational/src/lib.rs @@ -526,12 +526,14 @@ impl Signed for Ratio { #[inline] 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] 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, 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 { numer: 1, denom: 3, @@ -1081,9 +1091,17 @@ mod test { assert_eq!(_1_2.abs_sub(&_3_2), Zero::zero()); assert_eq!(_1_2.signum(), One::one()); assert_eq!(_NEG1_2.signum(), ->::one()); + assert_eq!(_0.signum(), Zero::zero()); assert!(_NEG1_2.is_negative()); + assert!(_1_NEG2.is_negative()); 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!(!_NEG1_NEG2.is_negative()); + assert!(!_0.is_positive()); + assert!(!_0.is_negative()); } #[test]