throws subtraction overflow error for floats

This commit is contained in:
Celso Bonutti Filho 2020-12-22 23:26:50 -03:00
parent 59b850ba50
commit 5d2abc2bd2
2 changed files with 50 additions and 14 deletions

View file

@ -3424,7 +3424,29 @@ fn build_float_binop<'a, 'ctx, 'env>(
struct_value.into()
}
NumAddWrap => unreachable!("wrapping addition is not defined on floats"),
NumSub => bd.build_float_sub(lhs, rhs, "sub_float").into(),
NumSub => {
let builder = env.builder;
let context = env.context;
let result = bd.build_float_sub(lhs, rhs, "sub_float");
let is_finite =
call_bitcode_fn(env, &[result.into()], &bitcode::NUM_IS_FINITE).into_int_value();
let then_block = context.append_basic_block(parent, "then_block");
let throw_block = context.append_basic_block(parent, "throw_block");
builder.build_conditional_branch(is_finite, then_block, throw_block);
builder.position_at_end(throw_block);
throw_exception(env, "float subtraction overflowed!");
builder.position_at_end(then_block);
result.into()
}
NumSubWrap => unreachable!("wrapping subtraction is not defined on floats"),
NumMul => bd.build_float_mul(lhs, rhs, "mul_float").into(),
NumGt => bd.build_float_compare(OGT, lhs, rhs, "float_gt").into(),
NumGte => bd.build_float_compare(OGE, lhs, rhs, "float_gte").into(),

View file

@ -704,19 +704,19 @@ mod gen_num {
);
}
// #[test]
// #[should_panic(expected = r#"Roc failed with message: "float addition overflowed!"#)]
// fn float_overflow() {
// assert_evals_to!(
// indoc!(
// r#"
// 1.7976931348623157e308 + 1.7976931348623157e308
// "#
// ),
// 0.0,
// f64
// );
// }
#[test]
#[should_panic(expected = r#"Roc failed with message: "float addition overflowed!"#)]
fn float_add_overflow() {
assert_evals_to!(
indoc!(
r#"
1.7976931348623157e308 + 1.7976931348623157e308
"#
),
0.0,
f64
);
}
#[test]
fn num_max_int() {
@ -770,4 +770,18 @@ mod gen_num {
i64
);
}
#[test]
#[should_panic(expected = r#"Roc failed with message: "float subtraction overflowed!"#)]
fn float_sub_overflow() {
assert_evals_to!(
indoc!(
r#"
-1.7976931348623157e308 - 1.7976931348623157e308
"#
),
0.0,
f64
);
}
}