Skip to content

Commit

Permalink
Merge branch 'fix_compare_bug' into 'main'
Browse files Browse the repository at this point in the history
Fix bug: compare with zero

See merge request codbase/decimal-rs!9
  • Loading branch information
davidli2010 committed May 31, 2021
2 parents 6ac567e + 76655f1 commit a727d72
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ fn decimal_hash(bench: &mut Bencher) {
})
}

fn decimal_cmp(bench: &mut Bencher) {
let x = parse("12345678901.23456789");
let y = parse("12345.67890123456789");
bench.iter(|| {
let _n = black_box(x > y);
})
}

benchmark_group!(
decimal_benches,
decimal_parse,
Expand All @@ -171,6 +179,7 @@ benchmark_group!(
decimal_decode,
decimal_normalize,
decimal_hash,
decimal_cmp,
);

benchmark_main!(decimal_benches);
18 changes: 18 additions & 0 deletions src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,16 @@ impl Ord for Decimal {
(self, other)
};

if left.is_zero() {
return if right.is_zero() {
Ordering::Equal
} else {
Ordering::Less
};
} else if right.is_zero() {
return Ordering::Greater;
}

if left.scale == right.scale {
// fast path for same scale
return left.int_val.cmp(&right.int_val);
Expand Down Expand Up @@ -967,6 +977,14 @@ mod tests {
assert_cmp!(
"-9.9999999999999999999999999999999999999", <, "1"
);
assert_cmp!("4703178999618078116505370421100e39", >, "0");
assert_cmp!("4703178999618078116505370421100e-39", >, "0");
assert_cmp!("-4703178999618078116505370421100e39", <, "0");
assert_cmp!("-4703178999618078116505370421100e-39", <, "0");
assert_cmp!("0", <, "4703178999618078116505370421100e39");
assert_cmp!("0", <, "4703178999618078116505370421100e-39");
assert_cmp!("0", >, "-4703178999618078116505370421100e39");
assert_cmp!("0", >, "-4703178999618078116505370421100e-39");
}

#[test]
Expand Down

0 comments on commit a727d72

Please sign in to comment.