1# Tests the functions imported from math
2
3try:
4    from math import *
5except ImportError:
6    print("SKIP")
7    raise SystemExit
8
9test_values = [-100.0, -1.23456, -1, -0.5, 0.0, 0.5, 1.23456, 100.0]
10test_values_small = [
11    -10.0,
12    -1.23456,
13    -1,
14    -0.5,
15    0.0,
16    0.5,
17    1.23456,
18    10.0,
19]  # so we don't overflow 32-bit precision
20unit_range_test_values = [-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75, 1.0]
21
22functions = [
23    ("sqrt", sqrt, test_values),
24    ("exp", exp, test_values_small),
25    ("log", log, test_values),
26    ("cos", cos, test_values),
27    ("sin", sin, test_values),
28    ("tan", tan, test_values),
29    ("acos", acos, unit_range_test_values),
30    ("asin", asin, unit_range_test_values),
31    ("atan", atan, test_values),
32    ("ceil", ceil, test_values),
33    ("fabs", fabs, test_values),
34    ("floor", floor, test_values),
35    ("trunc", trunc, test_values),
36    ("radians", radians, test_values),
37    ("degrees", degrees, test_values),
38]
39
40for function_name, function, test_vals in functions:
41    print(function_name)
42    for value in test_vals:
43        try:
44            print("{:.5g}".format(function(value)))
45        except ValueError as e:
46            print(str(e))
47
48tuple_functions = [
49    ("frexp", frexp, test_values),
50    ("modf", modf, test_values),
51]
52
53for function_name, function, test_vals in tuple_functions:
54    print(function_name)
55    for value in test_vals:
56        x, y = function(value)
57        print("{:.5g} {:.5g}".format(x, y))
58
59binary_functions = [
60    (
61        "copysign",
62        copysign,
63        [(23.0, 42.0), (-23.0, 42.0), (23.0, -42.0), (-23.0, -42.0), (1.0, 0.0), (1.0, -0.0)],
64    ),
65    ("pow", pow, ((1.0, 0.0), (0.0, 1.0), (2.0, 0.5), (-3.0, 5.0), (-3.0, -4.0))),
66    ("atan2", atan2, ((1.0, 0.0), (0.0, 1.0), (2.0, 0.5), (-3.0, 5.0), (-3.0, -4.0))),
67    ("fmod", fmod, ((1.0, 1.0), (0.0, 1.0), (2.0, 0.5), (-3.0, 5.0), (-3.0, -4.0))),
68    ("ldexp", ldexp, ((1.0, 0), (0.0, 1), (2.0, 2), (3.0, -2), (-3.0, -4))),
69    (
70        "log",
71        log,
72        (
73            (2.0, 2.0),
74            (3.0, 2.0),
75            (4.0, 5.0),
76            (0.0, 1.0),
77            (1.0, 0.0),
78            (-1.0, 1.0),
79            (1.0, -1.0),
80            (2.0, 1.0),
81        ),
82    ),
83]
84
85for function_name, function, test_vals in binary_functions:
86    print(function_name)
87    for value1, value2 in test_vals:
88        try:
89            print("{:.5g}".format(function(value1, value2)))
90        except (ValueError, ZeroDivisionError) as e:
91            print(type(e))
92