Skip to content

Commit 774bf31

Browse files
committed
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 8be7e7b.
2 parents 095738e + c22e3bf commit 774bf31

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

rational/src/lib.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -526,12 +526,14 @@ impl<T: Clone + Integer + Signed> Signed for Ratio<T> {
526526

527527
#[inline]
528528
fn is_positive(&self) -> bool {
529-
!self.is_negative()
529+
(self.numer.is_positive() && self.denom.is_positive()) ||
530+
(self.numer.is_negative() && self.denom.is_negative())
530531
}
531532

532533
#[inline]
533534
fn is_negative(&self) -> bool {
534-
self.numer.is_negative() ^ self.denom.is_negative()
535+
(self.numer.is_negative() && self.denom.is_positive()) ||
536+
(self.numer.is_positive() && self.denom.is_negative())
535537
}
536538
}
537539

@@ -674,6 +676,14 @@ mod test {
674676
numer: -1,
675677
denom: 2,
676678
};
679+
pub const _1_NEG2: Rational = Ratio {
680+
numer: 1,
681+
denom: -2,
682+
};
683+
pub const _NEG1_NEG2: Rational = Ratio {
684+
numer: -1,
685+
denom: -2,
686+
};
677687
pub const _1_3: Rational = Ratio {
678688
numer: 1,
679689
denom: 3,
@@ -1081,9 +1091,17 @@ mod test {
10811091
assert_eq!(_1_2.abs_sub(&_3_2), Zero::zero());
10821092
assert_eq!(_1_2.signum(), One::one());
10831093
assert_eq!(_NEG1_2.signum(), -<Ratio<isize>>::one());
1094+
assert_eq!(_0.signum(), Zero::zero());
10841095
assert!(_NEG1_2.is_negative());
1096+
assert!(_1_NEG2.is_negative());
10851097
assert!(!_NEG1_2.is_positive());
1098+
assert!(!_1_NEG2.is_positive());
1099+
assert!(_1_2.is_positive());
1100+
assert!(_NEG1_NEG2.is_positive());
10861101
assert!(!_1_2.is_negative());
1102+
assert!(!_NEG1_NEG2.is_negative());
1103+
assert!(!_0.is_positive());
1104+
assert!(!_0.is_negative());
10871105
}
10881106

10891107
#[test]

0 commit comments

Comments
 (0)