forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_math.py
More file actions
175 lines (131 loc) · 2.77 KB
/
Copy pathtest_math.py
File metadata and controls
175 lines (131 loc) · 2.77 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from math import (factorial, isqrt, perm, comb, degrees, radians, exp, pow,
ldexp, fabs, gcd, lcm, floor, ceil, remainder, expm1, fmod, log1p, trunc)
from ltypes import i32, f64, i64
eps: f64
eps = 1e-12
def test_factorial_1():
i: i32
i = factorial(10)
assert i == 3628800
def test_comb():
i: i32
i = comb(10, 2)
assert i == 45
def test_perm():
i: i32
i = perm(5, 2)
assert i == 20
def test_isqrt():
i: i32
i = isqrt(15)
assert i == 3
def test_degrees():
i: f64
# Check for integer
i = degrees(32)
assert abs(i - 1833.4649444186343) < eps
# Check for float
i = degrees(32.2)
assert abs(i - 1844.924100321251) < eps
def test_radians():
i: f64
# Check for integer
i = radians(100)
assert abs(i - 1.7453292519943295) < eps
# Check for float
i = radians(100.1)
assert abs(i - 1.7470745812463238) < eps
def test_exp():
i: f64
i = exp(2.34)
assert abs(i - 10.381236562731843) < eps
def test_pow():
i: f64
i = pow(2.4, 4.3)
assert abs(i - 43.14280115650323) < eps
def test_ldexp():
i: f64
i = ldexp(23.3, 2)
assert abs(i - 93.2) < eps
def test_fabs():
i: f64
j: f64
eps: f64
eps = 1e-12
i = fabs(10.3)
j = fabs(-10.3)
assert abs(i - j) < eps
def test_gcd():
i: i32
i = gcd(10, 4)
assert i == 2
i = gcd(21, 14)
assert i == 7
i = gcd(21, -12)
assert i == 3
def test_lcm():
i: i32
i = lcm(10, 4)
assert i == 20
i = lcm(21, 14)
assert i == 42
i = lcm(21, -12)
assert i == 84
def test_floor():
i: i64
i = floor(10.02)
assert i == 10
i = floor(-13)
assert i == -13
i = floor(-13.31)
assert i == -14
def test_ceil():
i: i64
i = ceil(10.02)
assert i == 11
i = ceil(-13)
assert i == -13
i = ceil(-13.31)
assert i == -13
def test_remainder():
assert remainder(9.0, 3.0) == 0.0
assert remainder(12.0, 5.0) == 2.0
assert remainder(13.0, 5.0) == -2.0
def test_fmod():
assert fmod(20.5, 2.5) == 0.5
assert fmod(-20.5, 2.5) == -0.5
def test_log1p():
assert log1p(1.0) - 0.69314718055994529 < eps
def test_expm1():
assert expm1(1.0) - 1.71828182845904509 < eps
def test_trunc():
i: i64
i = trunc(3.5)
assert i == 3
i = trunc(-4.5)
assert i == -4
i = trunc(5.5)
assert i == 5
i = trunc(-4.5)
assert i == -4
def check():
test_factorial_1()
test_comb()
test_isqrt()
test_perm()
test_degrees()
test_radians()
test_exp()
test_pow()
test_fabs()
test_ldexp()
test_gcd()
test_lcm()
test_floor()
test_ceil()
test_remainder()
test_fmod()
test_expm1()
test_log1p()
test_trunc()
check()