1# Tests of math module.
2
3load('math.star', 'math')
4load('assert.star', 'assert')
5
6def near(got, want, threshold):
7 return math.fabs(got-want) < threshold
8
9inf, nan = float("inf"), float("nan")
10
11# ceil
12assert.eq(math.ceil(0.0), 0.0)
13assert.eq(math.ceil(0.4), 1.0)
14assert.eq(math.ceil(0.5), 1.0)
15assert.eq(math.ceil(1.0), 1.0)
16assert.eq(math.ceil(10.0), 10.0)
17assert.eq(math.ceil(0), 0.0)
18assert.eq(math.ceil(1), 1.0)
19assert.eq(math.ceil(10), 10.0)
20assert.eq(math.ceil(-0.0), 0.0)
21assert.eq(math.ceil(-0.4), 0.0)
22assert.eq(math.ceil(-0.5), 0.0)
23assert.eq(math.ceil(-1.0), -1.0)
24assert.eq(math.ceil(-10.0), -10.0)
25assert.eq(math.ceil(-1), -1.0)
26assert.eq(math.ceil(-10), -10.0)
27assert.eq(type(math.ceil(0)), "int")
28assert.eq(type(math.ceil(0.4)), "int")
29assert.eq(type(math.ceil(10)), "int")
30assert.eq(type(math.ceil(-10.0)), "int")
31assert.eq(type(math.ceil(-0.5)), "int")
32assert.eq(math.ceil((1<<63) + 0.5), int(float((1<<63) + 1)))
33assert.fails(
34 lambda: math.ceil(inf), "cannot convert float infinity to integer")
35assert.fails(
36 lambda: math.ceil(-inf), "cannot convert float infinity to integer")
37assert.fails(
38 lambda: math.ceil(nan), "cannot convert float NaN to integer")
39assert.fails(lambda: math.ceil("0"), "got string, want float or int")
40# fabs
41assert.eq(math.fabs(2.0), 2.0)
42assert.eq(math.fabs(0.0), 0.0)
43assert.eq(math.fabs(-2.0), 2.0)
44assert.eq(math.fabs(2), 2)
45assert.eq(math.fabs(0), 0)
46assert.eq(math.fabs(-2), 2)
47assert.eq(math.fabs(inf), inf)
48assert.eq(math.fabs(-inf), inf)
49assert.eq(math.fabs(nan), nan)
50assert.fails(lambda: math.fabs("0"), "got string, want float or int")
51# floor
52assert.eq(math.floor(0.0), 0.0)
53assert.eq(math.floor(0.4), 0.0)
54assert.eq(math.floor(0.5), 0.0)
55assert.eq(math.floor(1.0), 1.0)
56assert.eq(math.floor(10.0), 10.0)
57assert.eq(math.floor(-0.0), 0.0)
58assert.eq(math.floor(-0.4), -1.0)
59assert.eq(math.floor(-0.5), -1.0)
60assert.eq(math.floor(-1.0), -1.0)
61assert.eq(math.floor(-10.0), -10.0)
62assert.eq(type(math.floor(0)), "int")
63assert.eq(type(math.floor(0.4)), "int")
64assert.eq(type(math.floor(10)), "int")
65assert.eq(type(math.floor(-10.0)), "int")
66assert.eq(type(math.floor(-0.5)), "int")
67assert.eq(math.floor((1<<63) + 0.5), int(float(1<<63)))
68assert.fails(
69 lambda: math.floor(inf), "cannot convert float infinity to integer")
70assert.fails(
71 lambda: math.floor(-inf), "cannot convert float infinity to integer")
72assert.fails(
73 lambda: math.floor(nan), "cannot convert float NaN to integer")
74assert.fails(lambda: math.floor("0"), "got string, want float or int")
75# mod
76assert.eq(math.mod(5, 3), 2)
77assert.eq(math.mod(inf, 1), nan)
78assert.eq(math.mod(-inf, 1.0), nan)
79assert.eq(math.mod(nan, 1.0), nan)
80assert.eq(math.mod(1.0, 0.0), nan)
81assert.eq(math.mod(1.0, inf), 1)
82assert.eq(math.mod(1.0, -inf), 1)
83assert.eq(math.mod(1.0, nan), nan)
84assert.fails(lambda: math.mod("0", 1.0), "got string, want float or int")
85assert.fails(lambda: math.mod(1.0, "0"), "got string, want float or int")
86# pow
87assert.eq(math.pow(5, 3), 125)
88assert.eq(math.pow(5, 0), 1)
89assert.eq(math.pow(5, 1), 5)
90assert.eq(math.pow(1, 5), 1)
91assert.eq(math.pow(inf, 1), inf)
92assert.eq(math.pow(-inf, 1.0), -inf)
93assert.eq(math.pow(nan, 1.0), nan)
94assert.eq(math.pow(1.1, inf), inf)
95assert.eq(math.pow(1.1, -inf), 0)
96assert.eq(math.pow(2.0, nan), nan)
97assert.fails(lambda: math.pow("0", 1.0), "got string, want float or int")
98assert.fails(lambda: math.pow(1.0, "0"), "got string, want float or int")
99# copysign
100assert.eq(math.copysign(3.2, -1), -3.2)
101assert.eq(math.copysign(inf, -1.0),-inf)
102assert.eq(math.copysign(-inf, -1), -inf)
103assert.eq(math.copysign(nan, -1), nan)
104assert.eq(math.copysign(-1, nan), 1)
105assert.fails(lambda: math.copysign("0", 1.0), "got string, want float or int")
106assert.fails(lambda: math.copysign(1.0, "0"), "got string, want float or int")
107# remainder
108assert.eq(math.remainder(3, 5), -2)
109assert.eq(math.remainder(1, 0), nan)
110assert.eq(math.remainder(2, inf), 2)
111assert.eq(math.remainder(2, -inf), 2)
112assert.eq(math.remainder(inf, -1.0), nan)
113assert.eq(math.remainder(-inf, -1), nan)
114assert.eq(math.remainder(nan, -1), nan)
115assert.eq(math.remainder(-1, nan), nan)
116assert.fails(lambda: math.remainder("0", 1.0), "got string, want float or int")
117assert.fails(lambda: math.remainder(1.0, "0"), "got string, want float or int")
118# round
119assert.eq(math.round(0.0), 0.0)
120assert.eq(math.round(0.4), 0.0)
121assert.eq(math.round(0.5), 1.0)
122assert.eq(math.round(0.6), 1.0)
123assert.eq(math.round(1.0), 1.0)
124assert.eq(math.round(10.0), 10.0)
125assert.eq(math.round(inf), inf)
126assert.eq(math.round(nan), nan)
127assert.eq(math.round(-0.4), 0.0)
128assert.eq(math.round(-0.5), -1.0)
129assert.eq(math.round(-0.6), -1.0)
130assert.eq(math.round(-1.0), -1.0)
131assert.eq(math.round(-10.0), -10.0)
132assert.eq(math.round(-inf), -inf)
133assert.fails(lambda: math.round("0"), "got string, want float or int")
134# exp
135assert.eq(math.exp(0.0), 1)
136assert.eq(math.exp(1.0), math.e)
137assert.true(near(math.exp(2.0), math.e * math.e, 0.00000000000001))
138assert.eq(math.exp(-1.0), 1 / math.e)
139assert.eq(math.exp(0), 1)
140assert.eq(math.exp(1), math.e)
141assert.true(near(math.exp(2), math.e * math.e, 0.00000000000001))
142assert.eq(math.exp(-1), 1 / math.e)
143assert.eq(math.exp(inf), inf)
144assert.eq(math.exp(-inf), 0)
145assert.eq(math.exp(nan), nan)
146assert.fails(lambda: math.exp("0"), "got string, want float or int")
147# sqrt
148assert.eq(math.sqrt(0.0), 0.0)
149assert.eq(math.sqrt(4.0), 2.0)
150assert.eq(math.sqrt(-4.0), nan)
151assert.eq(math.sqrt(0), 0)
152assert.eq(math.sqrt(4), 2)
153assert.eq(math.sqrt(-4), nan)
154assert.eq(math.sqrt(nan), nan)
155assert.eq(math.sqrt(inf), inf)
156assert.eq(math.sqrt(-inf), nan)
157assert.fails(lambda: math.sqrt("0"), "got string, want float or int")
158# acos
159assert.eq(math.acos(1.0), 0)
160assert.eq(math.acos(1), 0)
161assert.eq(math.acos(0.0), math.pi / 2)
162assert.eq(math.acos(0), math.pi / 2)
163assert.eq(math.acos(-1.0), math.pi)
164assert.eq(math.acos(-1), math.pi)
165assert.eq(math.acos(1.01), nan)
166assert.eq(math.acos(-1.01), nan)
167assert.eq(math.acos(inf), nan)
168assert.eq(math.acos(-inf), nan)
169assert.eq(math.acos(nan), nan)
170assert.fails(lambda: math.acos("0"), "got string, want float or int")
171# asin
172assert.eq(math.asin(0.0), 0)
173assert.eq(math.asin(1.0), math.pi / 2)
174assert.eq(math.asin(-1.0), -math.pi / 2)
175assert.eq(math.asin(0), 0)
176assert.eq(math.asin(1), math.pi / 2)
177assert.eq(math.asin(-1), -math.pi / 2)
178assert.eq(math.asin(1.01), nan)
179assert.eq(math.asin(-1.01), nan)
180assert.eq(math.asin(inf), nan)
181assert.eq(math.asin(-inf), nan)
182assert.eq(math.asin(nan), nan)
183assert.fails(lambda: math.asin("0"), "got string, want float or int")
184# atan
185assert.eq(math.atan(0.0), 0)
186assert.eq(math.atan(1.0), math.pi / 4)
187assert.eq(math.atan(-1.0), -math.pi / 4)
188assert.eq(math.atan(1), math.pi / 4)
189assert.eq(math.atan(-1), -math.pi / 4)
190assert.eq(math.atan(inf), math.pi / 2)
191assert.eq(math.atan(-inf), -math.pi / 2)
192assert.eq(math.atan(nan), nan)
193assert.fails(lambda: math.atan("0"), "got string, want float or int")
194# atan2
195assert.eq(math.atan2(1.0, 1.0), math.pi / 4)
196assert.eq(math.atan2(-1.0, 1.0), -math.pi / 4)
197assert.eq(math.atan2(0.0, 10.0), 0)
198assert.eq(math.atan2(0.0, -10.0), math.pi)
199assert.eq(math.atan2(-0.0, -10.0), -math.pi)
200assert.eq(math.atan2(10.0, 0.0), math.pi / 2)
201assert.eq(math.atan2(-10.0, 0.0), -math.pi / 2)
202assert.eq(math.atan2(1, 1), math.pi / 4)
203assert.eq(math.atan2(-1, 1), -math.pi / 4)
204assert.eq(math.atan2(0, 10.0), 0)
205assert.eq(math.atan2(0.0, -10), math.pi)
206assert.eq(math.atan2(-0.0, -10), -math.pi)
207assert.eq(math.atan2(10.0, 0), math.pi / 2)
208assert.eq(math.atan2(-10.0, 0), -math.pi / 2)
209assert.eq(math.atan2(1.0, nan), nan)
210assert.eq(math.atan2(nan, 1.0), nan)
211assert.eq(math.atan2(10.0, inf), 0)
212assert.eq(math.atan2(-10.0, inf), 0)
213assert.eq(math.atan2(10.0, -inf), math.pi)
214assert.eq(math.atan2(-10.0, -inf), -math.pi)
215assert.eq(math.atan2(inf, 10.0), math.pi / 2)
216assert.eq(math.atan2(inf, -10.0), math.pi / 2)
217assert.eq(math.atan2(-inf, 10.0), -math.pi / 2)
218assert.eq(math.atan2(-inf, -10.0), -math.pi / 2)
219assert.eq(math.atan2(inf, inf), math.pi / 4)
220assert.eq(math.atan2(-inf, inf), -math.pi / 4)
221assert.eq(math.atan2(inf, -inf), 3 * math.pi / 4)
222assert.eq(math.atan2(-inf, -inf), -3 * math.pi / 4)
223assert.fails(lambda: math.atan2("0", 1.0), "got string, want float or int")
224assert.fails(lambda: math.atan2(1.0, "0"), "got string, want float or int")
225# cos
226assert.eq(math.cos(0.0), 1)
227assert.true(near(math.cos(math.pi / 2), 0, 0.00000000000001))
228assert.eq(math.cos(math.pi), -1)
229assert.true(near(math.cos(-math.pi / 2), 0, 0.00000000000001))
230assert.eq(math.cos(-math.pi), -1)
231assert.eq(math.cos(inf), nan)
232assert.eq(math.cos(-inf), nan)
233assert.eq(math.cos(nan), nan)
234assert.fails(lambda: math.cos("0"), "got string, want float or int")
235# hypot
236assert.eq(math.hypot(4.0, 3.0), 5.0)
237assert.eq(math.hypot(4, 3), 5.0)
238assert.eq(math.hypot(inf, 3.0), inf)
239assert.eq(math.hypot(-inf, 3.0), inf)
240assert.eq(math.hypot(3.0, inf), inf)
241assert.eq(math.hypot(3.0, -inf), inf)
242assert.eq(math.hypot(nan, 3.0), nan)
243assert.eq(math.hypot(3.0, nan), nan)
244assert.fails(lambda: math.hypot("0", 1.0), "got string, want float or int")
245assert.fails(lambda: math.hypot(1.0, "0"), "got string, want float or int")
246# sin
247assert.eq(math.sin(0.0), 0)
248assert.eq(math.sin(0), 0)
249assert.eq(math.sin(math.pi / 2), 1)
250assert.eq(math.sin(-math.pi / 2), -1)
251assert.eq(math.sin(inf), nan)
252assert.eq(math.sin(-inf), nan)
253assert.eq(math.sin(nan), nan)
254assert.fails(lambda: math.sin("0"), "got string, want float or int")
255# tan
256assert.eq(math.tan(0.0), 0)
257assert.eq(math.tan(0), 0)
258assert.true(near(math.tan(math.pi / 4), 1, 0.00000000000001))
259assert.true(near(math.tan(-math.pi / 4), -1, 0.00000000000001))
260assert.eq(math.tan(inf), nan)
261assert.eq(math.tan(-inf), nan)
262assert.eq(math.tan(nan), nan)
263assert.fails(lambda: math.tan("0"), "got string, want float or int")
264# degrees
265oneDeg = 57.29577951308232
266assert.eq(math.degrees(1.0), oneDeg)
267assert.eq(math.degrees(1), oneDeg)
268assert.eq(math.degrees(-1.0), -oneDeg)
269assert.eq(math.degrees(-1), -oneDeg)
270assert.eq(math.degrees(inf), inf)
271assert.eq(math.degrees(-inf), -inf)
272assert.eq(math.degrees(nan), nan)
273assert.fails(lambda: math.degrees("0"), "got string, want float or int")
274# radians
275oneRad = 0.017453292519943295
276assert.eq(math.radians(1.0), oneRad)
277assert.eq(math.radians(-1.0), -oneRad)
278assert.eq(math.radians(1), oneRad)
279assert.eq(math.radians(-1), -oneRad)
280assert.eq(math.radians(inf), inf)
281assert.eq(math.radians(-inf), -inf)
282assert.eq(math.radians(nan), nan)
283assert.fails(lambda: math.radians("0"), "got string, want float or int")
284# acosh
285assert.eq(math.acosh(1.0), 0)
286assert.eq(math.acosh(1), 0)
287assert.eq(math.acosh(0.99), nan)
288assert.eq(math.acosh(0), nan)
289assert.eq(math.acosh(-0.99), nan)
290assert.eq(math.acosh(-inf), nan)
291assert.eq(math.acosh(inf), inf)
292assert.eq(math.acosh(nan), nan)
293assert.fails(lambda: math.acosh("0"), "got string, want float or int")
294# asinh
295asinhOne = 0.8813735870195432
296assert.eq(math.asinh(0.0), 0)
297assert.eq(math.asinh(0), 0)
298assert.true(near(math.asinh(1.0), asinhOne, 0.00000001))
299assert.true(near(math.asinh(1), asinhOne, 0.00000001))
300assert.true(near(math.asinh(-1.0), -asinhOne, 0.00000001))
301assert.true(near(math.asinh(-1), -asinhOne, 0.00000001))
302assert.eq(math.asinh(inf), inf)
303assert.eq(math.asinh(-inf), -inf)
304assert.eq(math.asinh(nan), nan)
305assert.fails(lambda: math.asinh("0"), "got string, want float or int")
306# atanh
307atanhHalf = 0.5493061443340548
308assert.eq(math.atanh(0.0), 0)
309assert.eq(math.atanh(0), 0)
310assert.eq(math.atanh(0.5), atanhHalf)
311assert.eq(math.atanh(-0.5), -atanhHalf)
312assert.eq(math.atanh(1), inf)
313assert.eq(math.atanh(-1), -inf)
314assert.eq(math.atanh(1.1), nan)
315assert.eq(math.atanh(-1.1), nan)
316assert.eq(math.atanh(inf), nan)
317assert.eq(math.atanh(-inf), nan)
318assert.eq(math.atanh(nan), nan)
319assert.fails(lambda: math.atanh("0"), "got string, want float or int")
320# cosh
321coshOne = 1.5430806348152437
322assert.eq(math.cosh(1.0), coshOne)
323assert.eq(math.cosh(1), coshOne)
324assert.eq(math.cosh(0.0), 1)
325assert.eq(math.cosh(0), 1)
326assert.eq(math.cosh(-inf), inf)
327assert.eq(math.cosh(inf), inf)
328assert.eq(math.cosh(nan), nan)
329assert.fails(lambda: math.cosh("0"), "got string, want float or int")
330# sinh
331sinhOne = 1.1752011936438014
332assert.eq(math.sinh(0.0), 0)
333assert.eq(math.sinh(0), 0)
334assert.eq(math.sinh(1.0), sinhOne)
335assert.eq(math.sinh(1), sinhOne)
336assert.eq(math.sinh(-1.0), -sinhOne)
337assert.eq(math.sinh(-1), -sinhOne)
338assert.eq(math.sinh(-inf), -inf)
339assert.eq(math.sinh(inf), inf)
340assert.eq(math.sinh(nan), nan)
341assert.fails(lambda: math.sinh("0"), "got string, want float or int")
342# tanh
343tanhOne = 0.7615941559557649
344assert.eq(math.tanh(0.0), 0)
345assert.eq(math.tanh(0), 0)
346assert.eq(math.tanh(1.0), tanhOne)
347assert.eq(math.tanh(1), tanhOne)
348assert.eq(math.tanh(-1.0), -tanhOne)
349assert.eq(math.tanh(-1), -tanhOne)
350assert.eq(math.tanh(-inf), -1)
351assert.eq(math.tanh(inf), 1)
352assert.eq(math.tanh(nan), nan)
353assert.fails(lambda: math.tanh("0"), "got string, want float or int")
354# log
355assert.eq(math.log(math.e), 1)
356assert.eq(math.log(10, 10), 1)
357assert.eq(math.log(10.0, 10.0), 1)
358assert.eq(math.log(2, 2.0), 1)
359assert.fails(lambda: math.log(2, 1), "division by zero")
360assert.fails(lambda: math.log(0.99, 1.0), "division by zero")
361assert.eq(math.log(0.0), -inf)
362assert.eq(math.log(0), -inf)
363assert.eq(math.log(-1.0), nan)
364assert.eq(math.log(-1), nan)
365assert.eq(math.log(nan), nan)
366assert.fails(lambda: math.log("0"), "got string, want float or int")
367assert.fails(lambda: math.log(10, "10"), "got string, want float or int")
368# gamma
369assert.eq(math.gamma(1.0), 1)
370assert.eq(math.gamma(1), 1)
371assert.eq(math.gamma(-1), nan)
372assert.eq(math.gamma(0), inf)
373assert.eq(math.gamma(-inf), nan)
374assert.eq(math.gamma(inf), inf)
375assert.eq(math.gamma(nan), nan)
376assert.fails(lambda: math.gamma("0"), "got string, want float or int")
377# Constants
378assert.eq(math.e, 2.7182818284590452)
379assert.eq(math.pi, 3.1415926535897932)
View as plain text