forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_fun.py
More file actions
131 lines (123 loc) · 2.86 KB
/
math_fun.py
File metadata and controls
131 lines (123 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Tests the functions imported from math
try:
from math import *
except ImportError:
print("SKIP")
raise SystemExit
test_values = [-100.0, -1.23456, -1, -0.5, 0.0, 0.5, 1.23456, 100.0]
test_values_small = [
-10.0,
-1.23456,
-1,
-0.5,
0.0,
0.5,
1.23456,
10.0,
] # so we don't overflow 32-bit precision
unit_range_test_values = [-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75, 1.0]
functions = [
("sqrt", sqrt, test_values),
("exp", exp, test_values_small),
("log", log, test_values),
("cos", cos, test_values),
("sin", sin, test_values),
("tan", tan, test_values),
("acos", acos, unit_range_test_values),
("asin", asin, unit_range_test_values),
("atan", atan, test_values),
("ceil", ceil, test_values),
("fabs", fabs, test_values),
("floor", floor, test_values),
("trunc", trunc, test_values),
("radians", radians, test_values),
("degrees", degrees, test_values),
]
for function_name, function, test_vals in functions:
print(function_name)
for value in test_vals:
try:
print("{:.5g}".format(function(value)))
except ValueError as e:
print(str(e))
tuple_functions = [
("frexp", frexp, test_values),
("modf", modf, test_values),
]
for function_name, function, test_vals in tuple_functions:
print(function_name)
for value in test_vals:
x, y = function(value)
print("{:.5g} {:.5g}".format(x, y))
binary_functions = [
(
"copysign",
copysign,
[(23.0, 42.0), (-23.0, 42.0), (23.0, -42.0), (-23.0, -42.0), (1.0, 0.0), (1.0, -0.0)],
),
(
"pow",
pow,
(
(1.0, 0.0),
(0.0, 1.0),
(2.0, 0.5),
(-3.0, 5.0),
(-3.0, -4.0),
),
),
(
"atan2",
atan2,
(
(1.0, 0.0),
(0.0, 1.0),
(2.0, 0.5),
(-3.0, 5.0),
(-3.0, -4.0),
),
),
(
"fmod",
fmod,
(
(1.0, 1.0),
(0.0, 1.0),
(2.0, 0.5),
(-3.0, 5.0),
(-3.0, -4.0),
),
),
(
"ldexp",
ldexp,
(
(1.0, 0),
(0.0, 1),
(2.0, 2),
(3.0, -2),
(-3.0, -4),
),
),
(
"log",
log,
(
(2.0, 2.0),
(3.0, 2.0),
(4.0, 5.0),
(0.0, 1.0),
(1.0, 0.0),
(-1.0, 1.0),
(1.0, -1.0),
(2.0, 1.0),
),
),
]
for function_name, function, test_vals in binary_functions:
print(function_name)
for value1, value2 in test_vals:
try:
print("{:.5g}".format(function(value1, value2)))
except (ValueError, ZeroDivisionError) as e:
print(type(e))