py/modmath: Add domain error checking to sqrt, log, log2, log10.

These functions will raise 'ValueError: math domain error' on invalid
input.
This commit is contained in:
Michael Buesch
2015-12-10 13:28:37 +01:00
committed by Damien George
parent f7c4f9a640
commit 17298af61e
2 changed files with 41 additions and 14 deletions

View File

@@ -9,15 +9,14 @@ except ImportError:
test_values = [-100., -1.23456, -1, -0.5, 0.0, 0.5, 1.23456, 100.]
test_values_small = [-10., -1.23456, -1, -0.5, 0.0, 0.5, 1.23456, 10.] # so we don't overflow 32-bit precision
p_test_values = [0.1, 0.5, 1.23456]
unit_range_test_values = [-1., -0.75, -0.5, -0.25, 0., 0.25, 0.5, 0.75, 1.]
functions = [('sqrt', sqrt, p_test_values),
functions = [('sqrt', sqrt, test_values),
('exp', exp, test_values_small),
('expm1', expm1, test_values_small),
('log', log, p_test_values),
('log2', log2, p_test_values),
('log10', log10, p_test_values),
('log', log, test_values),
('log2', log2, test_values),
('log10', log10, test_values),
('cosh', cosh, test_values_small),
('sinh', sinh, test_values_small),
('tanh', tanh, test_values_small),
@@ -41,7 +40,10 @@ functions = [('sqrt', sqrt, p_test_values),
for function_name, function, test_vals in functions:
print(function_name)
for value in test_vals:
print("{:.5g}".format(function(value)))
try:
print("{:.5g}".format(function(value)))
except ValueError as e:
print(str(e))
tuple_functions = [('frexp', frexp, test_values),
('modf', modf, test_values),
@@ -59,10 +61,13 @@ 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.))),
('log', log, ((2., 2.), (3., 2.), (4., 5.), (0., 1.), (1., 0.), (-1., 1.), (1., -1.))),
]
for function_name, function, test_vals in binary_functions:
print(function_name)
for value1, value2 in test_vals:
print("{:.5g}".format(function(value1, value2)))
try:
print("{:.5g}".format(function(value1, value2)))
except ValueError as e:
print(str(e))