diff --git a/src/public/py_number.c b/src/public/py_number.c index 0d15781f..554e783e 100644 --- a/src/public/py_number.c +++ b/src/public/py_number.c @@ -70,6 +70,7 @@ static bool int__truediv__(int argc, py_Ref argv) { py_i64 lhs = py_toint(&argv[0]); py_f64 rhs; if(try_castfloat(&argv[1], &rhs)) { + if(rhs == 0.0) return ZeroDivisionError("float division by zero"); py_newfloat(py_retval(), lhs / rhs); } else { py_newnotimplemented(py_retval()); @@ -82,6 +83,7 @@ static bool float__truediv__(int argc, py_Ref argv) { py_f64 lhs = py_tofloat(&argv[0]); py_f64 rhs; if(try_castfloat(&argv[1], &rhs)) { + if(rhs == 0.0) return ZeroDivisionError("float division by zero"); py_newfloat(py_retval(), lhs / rhs); } else { py_newnotimplemented(py_retval()); diff --git a/tests/02_float.py b/tests/02_float.py index e1967f3d..06aadc6a 100644 --- a/tests/02_float.py +++ b/tests/02_float.py @@ -54,8 +54,19 @@ assert eq(float("123.456"), 123.456) inf = float("inf") -assert 1/0 == inf -assert -1/0 == -inf + +try: + assert 1/0 == inf + exit(1) +except ZeroDivisionError: + pass + +try: + assert -1/0 == -inf + exit(1) +except ZeroDivisionError: + pass + assert 1/inf == 0 assert -1/inf == 0