mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-01 10:52:18 +00:00
Merge pull request #7255 from JRI98/fix_dec_zero_div_zero
Fix division of zero by zero for Dec
This commit is contained in:
commit
46067cf28c
2 changed files with 12 additions and 5 deletions
|
@ -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).
|
||||
|
|
|
@ -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!"#)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue