summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-07-04 02:15:11 +1000
committerDamien George <damien.p.george@gmail.com>2017-07-04 02:15:11 +1000
commit6b8b56f8596d3dde544a72d037e9feddd9f70938 (patch)
tree2f38350492210f01e6347e3e9332f8f37e4be3b9
parent9ed5e80eea71c8392b7c178d2e2b69e2428850d8 (diff)
py/modmath: Check for zero division in log with 2 args.
-rw-r--r--py/modmath.c2
-rw-r--r--tests/float/math_fun.py6
2 files changed, 5 insertions, 3 deletions
diff --git a/py/modmath.c b/py/modmath.c
index 10713234c..d5d135fc1 100644
--- a/py/modmath.c
+++ b/py/modmath.c
@@ -168,6 +168,8 @@ STATIC mp_obj_t mp_math_log(size_t n_args, const mp_obj_t *args) {
mp_float_t base = mp_obj_get_float(args[1]);
if (base <= (mp_float_t)0.0) {
math_error();
+ } else if (base == (mp_float_t)1.0) {
+ mp_raise_msg(&mp_type_ZeroDivisionError, "division by zero");
}
return mp_obj_new_float(l / MICROPY_FLOAT_C_FUN(log)(base));
}
diff --git a/tests/float/math_fun.py b/tests/float/math_fun.py
index 80d20bd8a..2835b9bfb 100644
--- a/tests/float/math_fun.py
+++ b/tests/float/math_fun.py
@@ -51,7 +51,7 @@ binary_functions = [('copysign', copysign, [(23., 42.), (-23., 42.), (23., -42.)
('atan2', atan2, ((1., 0.), (0., 1.), (2., 0.5), (-3., 5.), (-3., -4.),)),
('fmod', fmod, ((1., 1.), (0., 1.), (2., 0.5), (-3., 5.), (-3., -4.),)),
('ldexp', ldexp, ((1., 0), (0., 1), (2., 2), (3., -2), (-3., -4),)),
- ('log', log, ((2., 2.), (3., 2.), (4., 5.), (0., 1.), (1., 0.), (-1., 1.), (1., -1.))),
+ ('log', log, ((2., 2.), (3., 2.), (4., 5.), (0., 1.), (1., 0.), (-1., 1.), (1., -1.), (2., 1.))),
]
for function_name, function, test_vals in binary_functions:
@@ -59,5 +59,5 @@ for function_name, function, test_vals in binary_functions:
for value1, value2 in test_vals:
try:
print("{:.5g}".format(function(value1, value2)))
- except ValueError as e:
- print(str(e))
+ except (ValueError, ZeroDivisionError) as e:
+ print(type(e))