Skip to content

Commit 6b8b56f

Browse files
committed
py/modmath: Check for zero division in log with 2 args.
1 parent 9ed5e80 commit 6b8b56f

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

py/modmath.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ STATIC mp_obj_t mp_math_log(size_t n_args, const mp_obj_t *args) {
168168
mp_float_t base = mp_obj_get_float(args[1]);
169169
if (base <= (mp_float_t)0.0) {
170170
math_error();
171+
} else if (base == (mp_float_t)1.0) {
172+
mp_raise_msg(&mp_type_ZeroDivisionError, "division by zero");
171173
}
172174
return mp_obj_new_float(l / MICROPY_FLOAT_C_FUN(log)(base));
173175
}

tests/float/math_fun.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@
5151
('atan2', atan2, ((1., 0.), (0., 1.), (2., 0.5), (-3., 5.), (-3., -4.),)),
5252
('fmod', fmod, ((1., 1.), (0., 1.), (2., 0.5), (-3., 5.), (-3., -4.),)),
5353
('ldexp', ldexp, ((1., 0), (0., 1), (2., 2), (3., -2), (-3., -4),)),
54-
('log', log, ((2., 2.), (3., 2.), (4., 5.), (0., 1.), (1., 0.), (-1., 1.), (1., -1.))),
54+
('log', log, ((2., 2.), (3., 2.), (4., 5.), (0., 1.), (1., 0.), (-1., 1.), (1., -1.), (2., 1.))),
5555
]
5656

5757
for function_name, function, test_vals in binary_functions:
5858
print(function_name)
5959
for value1, value2 in test_vals:
6060
try:
6161
print("{:.5g}".format(function(value1, value2)))
62-
except ValueError as e:
63-
print(str(e))
62+
except (ValueError, ZeroDivisionError) as e:
63+
print(type(e))

0 commit comments

Comments
 (0)