File: math.star

package info (click to toggle)
golang-starlark 0.0~git20240725.42030a7-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 1,296 kB
  • sloc: makefile: 8; sh: 8
file content (379 lines) | stat: -rw-r--r-- 13,837 bytes parent folder | download | duplicates (2)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# Tests of math module.

load('math.star', 'math')
load('assert.star', 'assert')

def near(got, want, threshold):
  return math.fabs(got-want) < threshold

inf, nan = float("inf"), float("nan")

# ceil
assert.eq(math.ceil(0.0), 0.0)
assert.eq(math.ceil(0.4), 1.0)
assert.eq(math.ceil(0.5), 1.0)
assert.eq(math.ceil(1.0), 1.0)
assert.eq(math.ceil(10.0), 10.0)
assert.eq(math.ceil(0), 0.0)
assert.eq(math.ceil(1), 1.0)
assert.eq(math.ceil(10), 10.0)
assert.eq(math.ceil(-0.0), 0.0)
assert.eq(math.ceil(-0.4), 0.0)
assert.eq(math.ceil(-0.5), 0.0)
assert.eq(math.ceil(-1.0), -1.0)
assert.eq(math.ceil(-10.0), -10.0)
assert.eq(math.ceil(-1), -1.0)
assert.eq(math.ceil(-10), -10.0)
assert.eq(type(math.ceil(0)), "int")
assert.eq(type(math.ceil(0.4)), "int")
assert.eq(type(math.ceil(10)), "int")
assert.eq(type(math.ceil(-10.0)), "int")
assert.eq(type(math.ceil(-0.5)), "int")
assert.eq(math.ceil((1<<63) + 0.5), int(float((1<<63) + 1)))
assert.fails(
  lambda: math.ceil(inf), "cannot convert float infinity to integer")
assert.fails(
  lambda: math.ceil(-inf), "cannot convert float infinity to integer")
assert.fails(
  lambda: math.ceil(nan), "cannot convert float NaN to integer")
assert.fails(lambda: math.ceil("0"), "got string, want float or int")
# fabs
assert.eq(math.fabs(2.0), 2.0)
assert.eq(math.fabs(0.0), 0.0)
assert.eq(math.fabs(-2.0), 2.0)
assert.eq(math.fabs(2), 2)
assert.eq(math.fabs(0), 0)
assert.eq(math.fabs(-2), 2)
assert.eq(math.fabs(inf), inf)
assert.eq(math.fabs(-inf), inf)
assert.eq(math.fabs(nan), nan)
assert.fails(lambda: math.fabs("0"), "got string, want float or int")
# floor
assert.eq(math.floor(0.0), 0.0)
assert.eq(math.floor(0.4), 0.0)
assert.eq(math.floor(0.5), 0.0)
assert.eq(math.floor(1.0), 1.0)
assert.eq(math.floor(10.0), 10.0)
assert.eq(math.floor(-0.0), 0.0)
assert.eq(math.floor(-0.4), -1.0)
assert.eq(math.floor(-0.5), -1.0)
assert.eq(math.floor(-1.0), -1.0)
assert.eq(math.floor(-10.0), -10.0)
assert.eq(type(math.floor(0)), "int")
assert.eq(type(math.floor(0.4)), "int")
assert.eq(type(math.floor(10)), "int")
assert.eq(type(math.floor(-10.0)), "int")
assert.eq(type(math.floor(-0.5)), "int")
assert.eq(math.floor((1<<63) + 0.5), int(float(1<<63)))
assert.fails(
  lambda: math.floor(inf), "cannot convert float infinity to integer")
assert.fails(
  lambda: math.floor(-inf), "cannot convert float infinity to integer")
assert.fails(
  lambda: math.floor(nan), "cannot convert float NaN to integer")
assert.fails(lambda: math.floor("0"), "got string, want float or int")
# mod
assert.eq(math.mod(5, 3), 2)
assert.eq(math.mod(inf, 1), nan)
assert.eq(math.mod(-inf, 1.0), nan)
assert.eq(math.mod(nan, 1.0), nan)
assert.eq(math.mod(1.0, 0.0), nan)
assert.eq(math.mod(1.0, inf), 1)
assert.eq(math.mod(1.0, -inf), 1)
assert.eq(math.mod(1.0, nan), nan)
assert.fails(lambda: math.mod("0", 1.0), "got string, want float or int")
assert.fails(lambda: math.mod(1.0, "0"), "got string, want float or int")
# pow
assert.eq(math.pow(5, 3), 125)
assert.eq(math.pow(5, 0), 1)
assert.eq(math.pow(5, 1), 5)
assert.eq(math.pow(1, 5), 1)
assert.eq(math.pow(inf, 1), inf)
assert.eq(math.pow(-inf, 1.0), -inf)
assert.eq(math.pow(nan, 1.0), nan)
assert.eq(math.pow(1.1, inf), inf)
assert.eq(math.pow(1.1, -inf), 0)
assert.eq(math.pow(2.0, nan), nan)
assert.fails(lambda: math.pow("0", 1.0), "got string, want float or int")
assert.fails(lambda: math.pow(1.0, "0"), "got string, want float or int")
# copysign
assert.eq(math.copysign(3.2, -1), -3.2)
assert.eq(math.copysign(inf, -1.0),-inf)
assert.eq(math.copysign(-inf, -1), -inf)
assert.eq(math.copysign(nan, -1), nan)
assert.eq(math.copysign(-1, nan), 1)
assert.fails(lambda: math.copysign("0", 1.0), "got string, want float or int")
assert.fails(lambda: math.copysign(1.0, "0"), "got string, want float or int")
# remainder
assert.eq(math.remainder(3, 5), -2)
assert.eq(math.remainder(1, 0), nan)
assert.eq(math.remainder(2, inf), 2)
assert.eq(math.remainder(2, -inf), 2)
assert.eq(math.remainder(inf, -1.0), nan)
assert.eq(math.remainder(-inf, -1), nan)
assert.eq(math.remainder(nan, -1), nan)
assert.eq(math.remainder(-1, nan), nan)
assert.fails(lambda: math.remainder("0", 1.0), "got string, want float or int")
assert.fails(lambda: math.remainder(1.0, "0"), "got string, want float or int")
# round
assert.eq(math.round(0.0), 0.0)
assert.eq(math.round(0.4), 0.0)
assert.eq(math.round(0.5), 1.0)
assert.eq(math.round(0.6), 1.0)
assert.eq(math.round(1.0), 1.0)
assert.eq(math.round(10.0), 10.0)
assert.eq(math.round(inf), inf)
assert.eq(math.round(nan), nan)
assert.eq(math.round(-0.4), 0.0)
assert.eq(math.round(-0.5), -1.0)
assert.eq(math.round(-0.6), -1.0)
assert.eq(math.round(-1.0), -1.0)
assert.eq(math.round(-10.0), -10.0)
assert.eq(math.round(-inf), -inf)
assert.fails(lambda: math.round("0"), "got string, want float or int")
# exp
assert.eq(math.exp(0.0), 1)
assert.eq(math.exp(1.0), math.e)
assert.true(near(math.exp(2.0), math.e * math.e, 0.00000000000001))
assert.eq(math.exp(-1.0), 1 / math.e)
assert.eq(math.exp(0), 1)
assert.eq(math.exp(1), math.e)
assert.true(near(math.exp(2), math.e * math.e, 0.00000000000001))
assert.eq(math.exp(-1), 1 / math.e)
assert.eq(math.exp(inf), inf)
assert.eq(math.exp(-inf), 0)
assert.eq(math.exp(nan), nan)
assert.fails(lambda: math.exp("0"), "got string, want float or int")
# sqrt
assert.eq(math.sqrt(0.0), 0.0)
assert.eq(math.sqrt(4.0), 2.0)
assert.eq(math.sqrt(-4.0), nan)
assert.eq(math.sqrt(0), 0)
assert.eq(math.sqrt(4), 2)
assert.eq(math.sqrt(-4), nan)
assert.eq(math.sqrt(nan), nan)
assert.eq(math.sqrt(inf), inf)
assert.eq(math.sqrt(-inf), nan)
assert.fails(lambda: math.sqrt("0"), "got string, want float or int")
# acos
assert.eq(math.acos(1.0), 0)
assert.eq(math.acos(1), 0)
assert.eq(math.acos(0.0), math.pi / 2)
assert.eq(math.acos(0), math.pi / 2)
assert.eq(math.acos(-1.0), math.pi)
assert.eq(math.acos(-1), math.pi)
assert.eq(math.acos(1.01), nan)
assert.eq(math.acos(-1.01), nan)
assert.eq(math.acos(inf), nan)
assert.eq(math.acos(-inf), nan)
assert.eq(math.acos(nan), nan)
assert.fails(lambda: math.acos("0"), "got string, want float or int")
# asin
assert.eq(math.asin(0.0), 0)
assert.eq(math.asin(1.0), math.pi / 2)
assert.eq(math.asin(-1.0), -math.pi / 2)
assert.eq(math.asin(0), 0)
assert.eq(math.asin(1), math.pi / 2)
assert.eq(math.asin(-1), -math.pi / 2)
assert.eq(math.asin(1.01), nan)
assert.eq(math.asin(-1.01), nan)
assert.eq(math.asin(inf), nan)
assert.eq(math.asin(-inf), nan)
assert.eq(math.asin(nan), nan)
assert.fails(lambda: math.asin("0"), "got string, want float or int")
# atan
assert.eq(math.atan(0.0), 0)
assert.eq(math.atan(1.0), math.pi / 4)
assert.eq(math.atan(-1.0), -math.pi / 4)
assert.eq(math.atan(1), math.pi / 4)
assert.eq(math.atan(-1), -math.pi / 4)
assert.eq(math.atan(inf), math.pi / 2)
assert.eq(math.atan(-inf), -math.pi / 2)
assert.eq(math.atan(nan), nan)
assert.fails(lambda: math.atan("0"), "got string, want float or int")
# atan2
assert.eq(math.atan2(1.0, 1.0), math.pi / 4)
assert.eq(math.atan2(-1.0, 1.0), -math.pi / 4)
assert.eq(math.atan2(0.0, 10.0), 0)
assert.eq(math.atan2(0.0, -10.0), math.pi)
assert.eq(math.atan2(-0.0, -10.0), -math.pi)
assert.eq(math.atan2(10.0, 0.0), math.pi / 2)
assert.eq(math.atan2(-10.0, 0.0), -math.pi / 2)
assert.eq(math.atan2(1, 1), math.pi / 4)
assert.eq(math.atan2(-1, 1), -math.pi / 4)
assert.eq(math.atan2(0, 10.0), 0)
assert.eq(math.atan2(0.0, -10), math.pi)
assert.eq(math.atan2(-0.0, -10), -math.pi)
assert.eq(math.atan2(10.0, 0), math.pi / 2)
assert.eq(math.atan2(-10.0, 0), -math.pi / 2)
assert.eq(math.atan2(1.0, nan), nan)
assert.eq(math.atan2(nan, 1.0), nan)
assert.eq(math.atan2(10.0, inf), 0)
assert.eq(math.atan2(-10.0, inf), 0)
assert.eq(math.atan2(10.0, -inf), math.pi)
assert.eq(math.atan2(-10.0, -inf), -math.pi)
assert.eq(math.atan2(inf, 10.0), math.pi / 2)
assert.eq(math.atan2(inf, -10.0), math.pi / 2)
assert.eq(math.atan2(-inf, 10.0), -math.pi / 2)
assert.eq(math.atan2(-inf, -10.0), -math.pi / 2)
assert.eq(math.atan2(inf, inf), math.pi / 4)
assert.eq(math.atan2(-inf, inf), -math.pi / 4)
assert.eq(math.atan2(inf, -inf), 3 * math.pi / 4)
assert.eq(math.atan2(-inf, -inf), -3 * math.pi / 4)
assert.fails(lambda: math.atan2("0", 1.0), "got string, want float or int")
assert.fails(lambda: math.atan2(1.0, "0"), "got string, want float or int")
# cos
assert.eq(math.cos(0.0), 1)
assert.true(near(math.cos(math.pi / 2), 0, 0.00000000000001))
assert.eq(math.cos(math.pi), -1)
assert.true(near(math.cos(-math.pi / 2), 0, 0.00000000000001))
assert.eq(math.cos(-math.pi), -1)
assert.eq(math.cos(inf), nan)
assert.eq(math.cos(-inf), nan)
assert.eq(math.cos(nan), nan)
assert.fails(lambda: math.cos("0"), "got string, want float or int")
# hypot
assert.eq(math.hypot(4.0, 3.0), 5.0)
assert.eq(math.hypot(4, 3), 5.0)
assert.eq(math.hypot(inf, 3.0), inf)
assert.eq(math.hypot(-inf, 3.0), inf)
assert.eq(math.hypot(3.0, inf), inf)
assert.eq(math.hypot(3.0, -inf), inf)
assert.eq(math.hypot(nan, 3.0), nan)
assert.eq(math.hypot(3.0, nan), nan)
assert.fails(lambda: math.hypot("0", 1.0), "got string, want float or int")
assert.fails(lambda: math.hypot(1.0, "0"), "got string, want float or int")
# sin
assert.eq(math.sin(0.0), 0)
assert.eq(math.sin(0), 0)
assert.eq(math.sin(math.pi / 2), 1)
assert.eq(math.sin(-math.pi / 2), -1)
assert.eq(math.sin(inf), nan)
assert.eq(math.sin(-inf), nan)
assert.eq(math.sin(nan), nan)
assert.fails(lambda: math.sin("0"), "got string, want float or int")
# tan
assert.eq(math.tan(0.0), 0)
assert.eq(math.tan(0), 0)
assert.true(near(math.tan(math.pi / 4), 1, 0.00000000000001))
assert.true(near(math.tan(-math.pi / 4), -1, 0.00000000000001))
assert.eq(math.tan(inf), nan)
assert.eq(math.tan(-inf), nan)
assert.eq(math.tan(nan), nan)
assert.fails(lambda: math.tan("0"), "got string, want float or int")
# degrees
oneDeg = 57.29577951308232
assert.eq(math.degrees(1.0), oneDeg)
assert.eq(math.degrees(1), oneDeg)
assert.eq(math.degrees(-1.0), -oneDeg)
assert.eq(math.degrees(-1), -oneDeg)
assert.eq(math.degrees(inf), inf)
assert.eq(math.degrees(-inf), -inf)
assert.eq(math.degrees(nan), nan)
assert.fails(lambda: math.degrees("0"), "got string, want float or int")
# radians
oneRad = 0.017453292519943295
assert.eq(math.radians(1.0), oneRad)
assert.eq(math.radians(-1.0), -oneRad)
assert.eq(math.radians(1), oneRad)
assert.eq(math.radians(-1), -oneRad)
assert.eq(math.radians(inf), inf)
assert.eq(math.radians(-inf), -inf)
assert.eq(math.radians(nan), nan)
assert.fails(lambda: math.radians("0"), "got string, want float or int")
# acosh
assert.eq(math.acosh(1.0), 0)
assert.eq(math.acosh(1), 0)
assert.eq(math.acosh(0.99), nan)
assert.eq(math.acosh(0), nan)
assert.eq(math.acosh(-0.99), nan)
assert.eq(math.acosh(-inf), nan)
assert.eq(math.acosh(inf), inf)
assert.eq(math.acosh(nan), nan)
assert.fails(lambda: math.acosh("0"), "got string, want float or int")
# asinh
asinhOne = 0.8813735870195432
assert.eq(math.asinh(0.0), 0)
assert.eq(math.asinh(0), 0)
assert.true(near(math.asinh(1.0), asinhOne, 0.00000001))
assert.true(near(math.asinh(1), asinhOne, 0.00000001))
assert.true(near(math.asinh(-1.0), -asinhOne, 0.00000001))
assert.true(near(math.asinh(-1), -asinhOne, 0.00000001))
assert.eq(math.asinh(inf), inf)
assert.eq(math.asinh(-inf), -inf)
assert.eq(math.asinh(nan), nan)
assert.fails(lambda: math.asinh("0"), "got string, want float or int")
# atanh
atanhHalf = 0.5493061443340548
assert.eq(math.atanh(0.0), 0)
assert.eq(math.atanh(0), 0)
assert.eq(math.atanh(0.5), atanhHalf)
assert.eq(math.atanh(-0.5), -atanhHalf)
assert.eq(math.atanh(1), inf)
assert.eq(math.atanh(-1), -inf)
assert.eq(math.atanh(1.1), nan)
assert.eq(math.atanh(-1.1), nan)
assert.eq(math.atanh(inf), nan)
assert.eq(math.atanh(-inf), nan)
assert.eq(math.atanh(nan), nan)
assert.fails(lambda: math.atanh("0"), "got string, want float or int")
# cosh
coshOne = 1.5430806348152437
assert.eq(math.cosh(1.0), coshOne)
assert.eq(math.cosh(1), coshOne)
assert.eq(math.cosh(0.0), 1)
assert.eq(math.cosh(0), 1)
assert.eq(math.cosh(-inf), inf)
assert.eq(math.cosh(inf), inf)
assert.eq(math.cosh(nan), nan)
assert.fails(lambda: math.cosh("0"), "got string, want float or int")
# sinh
sinhOne = 1.1752011936438014
assert.eq(math.sinh(0.0), 0)
assert.eq(math.sinh(0), 0)
assert.eq(math.sinh(1.0), sinhOne)
assert.eq(math.sinh(1), sinhOne)
assert.eq(math.sinh(-1.0), -sinhOne)
assert.eq(math.sinh(-1), -sinhOne)
assert.eq(math.sinh(-inf), -inf)
assert.eq(math.sinh(inf), inf)
assert.eq(math.sinh(nan), nan)
assert.fails(lambda: math.sinh("0"), "got string, want float or int")
# tanh
tanhOne = 0.7615941559557649
assert.eq(math.tanh(0.0), 0)
assert.eq(math.tanh(0), 0)
assert.eq(math.tanh(1.0), tanhOne)
assert.eq(math.tanh(1), tanhOne)
assert.eq(math.tanh(-1.0), -tanhOne)
assert.eq(math.tanh(-1), -tanhOne)
assert.eq(math.tanh(-inf), -1)
assert.eq(math.tanh(inf), 1)
assert.eq(math.tanh(nan), nan)
assert.fails(lambda: math.tanh("0"), "got string, want float or int")
# log
assert.eq(math.log(math.e), 1)
assert.eq(math.log(10, 10), 1)
assert.eq(math.log(10.0, 10.0), 1)
assert.eq(math.log(2, 2.0), 1)
assert.fails(lambda: math.log(2, 1), "division by zero")
assert.fails(lambda: math.log(0.99, 1.0), "division by zero")
assert.eq(math.log(0.0), -inf)
assert.eq(math.log(0), -inf)
assert.eq(math.log(-1.0), nan)
assert.eq(math.log(-1), nan)
assert.eq(math.log(nan), nan)
assert.fails(lambda: math.log("0"), "got string, want float or int")
assert.fails(lambda: math.log(10, "10"), "got string, want float or int")
# gamma
assert.eq(math.gamma(1.0), 1)
assert.eq(math.gamma(1), 1)
assert.eq(math.gamma(-1), nan)
assert.eq(math.gamma(0), inf)
assert.eq(math.gamma(-inf), nan)
assert.eq(math.gamma(inf), inf)
assert.eq(math.gamma(nan), nan)
assert.fails(lambda: math.gamma("0"), "got string, want float or int")
# Constants
assert.eq(math.e, 2.7182818284590452)
assert.eq(math.pi, 3.1415926535897932)