Skip to content

Commit d28b12f

Browse files
bors[bot]maxbla
andcommittedJul 18, 2019
Merge #51
51: Fix #49 r=cuviper a=maxbla fixed issue #49 added a test case for it Side note: `cmp` is a bit scary. Do we know what it's amortized runtime is? It seems to me that the worst case (having to compare the reciprocals many times) could be pretty bad. Is there any way to have a separate `impl` for `T: Clone + Integer + CheckedMul`? As the comments note, CheckedMul would make the implementation faster. I messed around, but I can't find a way to have two implementations -- one for checked and one for no checked. I found an [this](rust-lang/rfcs#586) RFC for negative trait bounds though (spoiler: negative trait bounds are not happening soon). Co-authored-by: Max Blachman <[email protected]>
2 parents c53f2d7 + f38ff9c commit d28b12f

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed
 

‎src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ impl<T: Clone + Integer> Ord for Ratio<T> {
400400

401401
// With equal numerators, the denominators can be inversely compared
402402
if self.numer == other.numer {
403+
if self.numer.is_zero() {
404+
return cmp::Ordering::Equal;
405+
}
403406
let ord = self.denom.cmp(&other.denom);
404407
return if self.numer < T::zero() {
405408
ord
@@ -1473,6 +1476,9 @@ mod test {
14731476

14741477
assert!(_0 >= _0 && _1 >= _1);
14751478
assert!(_1 >= _0 && !(_0 >= _1));
1479+
1480+
let _0_2: Rational = Ratio::new_raw(0, 2);
1481+
assert_eq!(_0, _0_2);
14761482
}
14771483

14781484
#[test]

0 commit comments

Comments
 (0)
Please sign in to comment.