Merge pull request #7255 from JRI98/fix_dec_zero_div_zero

Fix division of zero by zero for Dec
This commit is contained in:
Sam Mohr 2024-11-28 15:06:18 -05:00 committed by GitHub
commit 46067cf28c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 5 deletions

View file

@ -437,16 +437,16 @@ pub const RocDec = extern struct {
const numerator_i128 = self.num;
const denominator_i128 = other.num;
// (0 / n) is always 0
if (numerator_i128 == 0) {
return RocDec{ .num = 0 };
}
// (n / 0) is an error
if (denominator_i128 == 0) {
roc_panic("Decimal division by 0!", 0);
}
// (0 / n) is always 0
if (numerator_i128 == 0) {
return RocDec{ .num = 0 };
}
// If they're both negative, or if neither is negative, the final answer
// is positive or zero. If one is negative and the denominator isn't, the
// final answer is negative (or zero, in which case final sign won't matter).

View file

@ -790,6 +790,13 @@ fn gen_div_checked_by_zero_dec() {
);
}
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))]
#[should_panic(expected = r#"Roc failed with message: "Decimal division by 0!"#)]
fn gen_div_dec_zero_by_zero() {
assert_evals_to!("0dec / 0", RocDec::from_str("-1").unwrap(), RocDec);
}
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm", feature = "gen-dev"))]
#[should_panic(expected = r#"Roc failed with message: "Decimal division by 0!"#)]