/
Any-num.pm
425 lines (321 loc) · 7.85 KB
/
Any-num.pm
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
augment class Any {
multi method abs() {
pir::abs__Nn(self.Num);
}
multi method exp() {
self.Num.exp;
}
our Int multi method ceiling() is export {
pir::box__PN(pir::ceil__IN(self))
}
our Str multi method chr() {
~(pir::chr__SI(self))
}
multi method cis() is export {
1.unpolar(self)
}
our Int multi method floor() is export {
pir::box__PI(pir::floor__IN(self))
}
our Num method rand() {
pir::box__PN(pir::rand__NN(self))
}
multi method roots($n) {
$.Complex.roots($n);
}
our Int multi method round() is export {
# pir::box__PI(pir::floor__IN(pir::add__NNN(self, 0.5)))
}
multi method sqrt() {
self.Num.sqrt;
}
INIT {
our @trig-base-conversions = (1.0, pi / 180.0, pi / 200.0, 2.0 * pi);
}
# Used by the :Trig subs and methods in the Int and Num classes.
our multi method !to-radians($base) {
self * pir::get_global__Ps('@trig-base-conversions')[$base];
}
our multi method !from-radians($base) {
self / pir::get_global__Ps('@trig-base-conversions')[$base];
}
multi method log() {
$.Num.log;
}
multi method log($base) {
$.Num.log($base);
}
multi method log10() {
$.Num.log10;
}
our Num multi method sin($base = Radians) {
self.Num.sin($base);
}
our Num multi method cos($base = Radians) {
self.Num.cos($base);
}
our Num multi method tan($base = Radians) {
self.Num.tan($base);
}
our Num multi method sec($base = Radians) {
self.Num.sec($base);
}
our Num multi method cosec($base = Radians) {
self.Num.cosec($base);
}
our Num multi method cotan($base = Radians) {
self.Num.cotan($base);
}
our Num multi method sinh($base = Radians) {
self.Num.sinh($base);
}
our Num multi method cosh($base = Radians) {
self.Num.cosh($base);
}
our Num multi method tanh($base = Radians) {
self.Num.tanh($base);
}
our Num multi method sech($base = Radians) {
self.Num.sech($base);
}
our Num multi method cosech($base = Radians) {
self.Num.cosech($base);
}
our Num multi method cotanh($base = Radians) {
self.Num.cotanh($base);
}
our Num multi method asin($base = Radians) {
self.Num.asin($base);
}
our Num multi method acos($base = Radians) {
self.Num.acos($base);
}
our Num multi method atan($base = Radians) {
self.Num.atan($base);
}
our Num multi method atan2($x = 1, $base = Radians) {
self.Num.atan2($x.Num, $base);
}
our Num multi method asec($base = Radians) {
self.Num.asec($base);
}
our Num multi method acosec($base = Radians) {
self.Num.acosec($base);
}
our Num multi method acotan($base = Radians) {
self.Num.acotan($base);
}
our Num multi method asinh($base = Radians) {
self.Num.asinh($base);
}
our Num multi method acosh($base = Radians) {
self.Num.acosh($base);
}
our Num multi method atanh($base = Radians) {
self.Num.atanh($base);
}
our Num multi method asech($base = Radians) {
self.Num.asech($base);
}
our Num multi method acosech($base = Radians) {
self.Num.acosech($base);
}
our Num multi method acotanh($base = Radians) {
self.Num.acotanh($base);
}
our ::Complex multi method unpolar($angle) is export {
Complex.new(self.Num * $angle.cos("radians"), self.Num * $angle.sin("radians"));
}
}
our proto sub abs($x) { $x.abs }
our multi sub prefix:<abs>($x) { $x.abs }
our proto sub exp($exponent) { $exponent.exp }
our proto sub log($x) { $x.log }
our multi sub log($x, $base) { $x.log($base) }
our proto sub log10($x) { $x.log10 }
# jnthn says that we should have both the multi sub declaration and the proto.
our multi sub sin($x, $base = Radians) {
$x.sin($base)
}
our proto sin($x, $base = Radians) {
sin($x, $base)
}
our multi sub asin($x, $base = Radians) {
$x.asin($base)
}
our proto asin($x, $base = Radians) {
asin($x, $base)
}
our multi sub cos($x, $base = Radians) {
$x.cos($base)
}
our proto cos($x, $base = Radians) {
cos($x, $base)
}
our multi sub acos($x, $base = Radians) {
$x.acos($base)
}
our proto acos($x, $base = Radians) {
acos($x, $base)
}
our multi sub tan($x, $base = Radians) {
$x.tan($base)
}
our proto tan($x, $base = Radians) {
tan($x, $base)
}
our multi sub atan($x, $base = Radians) {
$x.atan($base)
}
our proto atan($x, $base = Radians) {
atan($x, $base)
}
our multi sub sec($x, $base = Radians) {
$x.sec($base)
}
our proto sec($x, $base = Radians) {
sec($x, $base)
}
our multi sub asec($x, $base = Radians) {
$x.asec($base)
}
our proto asec($x, $base = Radians) {
asec($x, $base)
}
our multi sub cosec($x, $base = Radians) {
$x.cosec($base)
}
our proto cosec($x, $base = Radians) {
cosec($x, $base)
}
our multi sub acosec($x, $base = Radians) {
$x.acosec($base)
}
our proto acosec($x, $base = Radians) {
acosec($x, $base)
}
our multi sub cotan($x, $base = Radians) {
$x.cotan($base)
}
our proto cotan($x, $base = Radians) {
cotan($x, $base)
}
our multi sub acotan($x, $base = Radians) {
$x.acotan($base)
}
our proto acotan($x, $base = Radians) {
acotan($x, $base)
}
our multi sub sinh($x, $base = Radians) {
$x.sinh($base)
}
our proto sinh($x, $base = Radians) {
sinh($x, $base)
}
our multi sub asinh($x, $base = Radians) {
$x.asinh($base)
}
our proto asinh($x, $base = Radians) {
asinh($x, $base)
}
our multi sub cosh($x, $base = Radians) {
$x.cosh($base)
}
our proto cosh($x, $base = Radians) {
cosh($x, $base)
}
our multi sub acosh($x, $base = Radians) {
$x.acosh($base)
}
our proto acosh($x, $base = Radians) {
acosh($x, $base)
}
our multi sub tanh($x, $base = Radians) {
$x.tanh($base)
}
our proto tanh($x, $base = Radians) {
tanh($x, $base)
}
our multi sub atanh($x, $base = Radians) {
$x.atanh($base)
}
our proto atanh($x, $base = Radians) {
atanh($x, $base)
}
our multi sub sech($x, $base = Radians) {
$x.sech($base)
}
our proto sech($x, $base = Radians) {
sech($x, $base)
}
our multi sub asech($x, $base = Radians) {
$x.asech($base)
}
our proto asech($x, $base = Radians) {
asech($x, $base)
}
our multi sub cosech($x, $base = Radians) {
$x.cosech($base)
}
our proto cosech($x, $base = Radians) {
cosech($x, $base)
}
our multi sub acosech($x, $base = Radians) {
$x.acosech($base)
}
our proto acosech($x, $base = Radians) {
acosech($x, $base)
}
our multi sub cotanh($x, $base = Radians) {
$x.cotanh($base)
}
our proto cotanh($x, $base = Radians) {
cotanh($x, $base)
}
our multi sub acotanh($x, $base = Radians) {
$x.acotanh($base)
}
our proto acotanh($x, $base = Radians) {
acotanh($x, $base)
}
our multi sub atan2($y, $x = 1, $base = Radians) {
$y.atan2($x, $base)
}
our proto atan2($y, $x = 1, $base = Radians) {
atan2($y, $x, $base)
}
our Num sub rand (*@args) {
die "too many arguments passed - 0 params expected" if +@args;
1.rand
}
our multi sub sqrt(Any $x) {
$x.Num.sqrt
}
our proto sign($x) {
defined($x) ?? $x.Num.sign !! Mu;
}
# This one doesn't seem to be needed with the above defined.
# our multi sub sign($x) {
# defined($x) ?? $x.Num.sign !! undef;
# }
our multi sub roots($x, $n) {
$x.Complex.roots($n)
}
our multi sub infix:<cmp>(Num $a, Num $b) {
# TODO: should be Order::Same, ::Increase, ::Decrease once they work
if $a == $b {
0;
} else {
$a < $b ?? -1 !! 1;
}
}
our multi sub infix:«<=>»($a, $b) {
+$a cmp +$b
}
our proto chr($graph) {
$graph.chr;
}
our sub srand(Int $seed = time) {
pir::srand__0I($seed);
}
# vim: ft=perl6