-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
Num.pm
445 lines (392 loc) · 10.9 KB
/
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
my class Num {
method Num() { self }
method Bridge(Num:D:) { self }
method Int(Num:D:) {
(self == $Inf || self == -$Inf) ??
fail("Cannot coerce Inf to an Int") !!
nqp::fromnum_I(nqp::unbox_n(self), Int);
}
multi method new() { nqp::p6box_n(0) }
multi method new($n as Num) { $n }
multi method perl(Num:D:) {
my $res = self.Str;
if $res.index('e').defined {
$res;
} else {
$res ~ 'e0';
}
}
method Rat(Num:D: Real $epsilon = 1.0e-6) {
my sub modf($num) { my $q = $num.Int; $num - $q, $q; }
(self == $Inf || self == -$Inf) && fail("Cannot coerce Inf to a Rat");
my Num $num = self;
my Int $signum = $num < 0 ?? -1 !! 1;
$num = -$num if $signum == -1;
# Find convergents of the continued fraction.
my Num $r = $num - $num.Int;
my Int $q = $num.Int;
my ($a, $b) = 1, $q;
my ($c, $d) = 0, 1;
while $r != 0 && abs($num - ($b/$d)) > $epsilon {
($r, $q) = modf(1/$r);
($a, $b) = ($b, $q*$b + $a);
($c, $d) = ($d, $q*$d + $c);
}
# Note that this result has less error than any Rational with a
# smaller denominator but it is not (necessarily) the Rational
# with the smallest denominator that has less than $epsilon error.
# However, to find that Rational would take more processing.
($signum * $b) / $d;
}
multi method atan2(Num:D: Num:D $x = 1e0) {
nqp::p6box_n(nqp::atan2_n(nqp::unbox_n(self), nqp::unbox_n($x)));
}
multi method Str(Num:D:) {
nqp::p6box_s(nqp::unbox_n(self));
}
method succ(Num:D:) { self + 1e0 }
method pred(Num:D:) { self - 1e0 }
method isNaN(Num:D: ) {
self != self;
}
method abs(Num:D: ) {
nqp::p6box_n(nqp::abs_n(nqp::unbox_n(self)));
}
multi method exp(Num:D: ) {
nqp::p6box_n(nqp::exp_n(nqp::unbox_n(self)));
}
proto method log(|$) {*}
multi method log(Num:D: ) {
nqp::p6box_n(nqp::log_n(nqp::unbox_n(self)));
}
multi method log(Num:D: Num \$base) {
self.log() / $base.log();
}
proto method sqrt(|$) {*}
multi method sqrt(Num:D: ) {
nqp::p6box_n(nqp::sqrt_n(nqp::unbox_n(self)));
}
method rand(Num:D: ) {
nqp::p6box_n(pir::rand__NN(nqp::unbox_n(self)));
}
method ceiling(Num:D: ) {
# TODO: should check if self is -Inf/Inf/NaN, and otherwise
# use nqp::fromnum_I
nqp::p6bigint(pir::ceil__NN(nqp::unbox_n(self)));
}
method floor(Num:D: ) {
# TODO same as in ceiling()
nqp::p6bigint(pir::floor__NN(nqp::unbox_n(self)));
}
proto method sin(|$) {*}
multi method sin(Num:D: ) {
nqp::p6box_n(nqp::sin_n(nqp::unbox_n(self)));
}
proto method asin(|$) {*}
multi method asin(Num:D: ) {
nqp::p6box_n(nqp::asin_n(nqp::unbox_n(self)));
}
proto method cos(|$) {*}
multi method cos(Num:D: ) {
nqp::p6box_n(nqp::cos_n(nqp::unbox_n(self)));
}
proto method acos(|$) {*}
multi method acos(Num:D: ) {
nqp::p6box_n(nqp::acos_n(nqp::unbox_n(self)));
}
proto method tan(|$) {*}
multi method tan(Num:D: ) {
nqp::p6box_n(nqp::tan_n(nqp::unbox_n(self)));
}
proto method atan(|$) {*}
multi method atan(Num:D: ) {
nqp::p6box_n(nqp::atan_n(nqp::unbox_n(self)));
}
proto method sec(|$) {*}
multi method sec(Num:D: ) {
nqp::p6box_n(nqp::sec_n(nqp::unbox_n(self)));
}
proto method asec(|$) {*}
multi method asec(Num:D: ) {
nqp::p6box_n(nqp::asec_n(nqp::unbox_n(self)));
}
method cosec(Num:D:) {
nqp::p6box_n(nqp::div_n(1, nqp::sin_n(nqp::unbox_n(self))));
}
method acosec(Num:D:) {
nqp::p6box_n(nqp::asin_n(nqp::div_n(1, nqp::unbox_n(self))));
}
method cotan(Num:D:) {
nqp::p6box_n(nqp::div_n(1, nqp::tan_n(nqp::unbox_n(self))));
}
method acotan(Num:D:) {
nqp::p6box_n(nqp::atan_n(nqp::div_n(1, nqp::unbox_n(self))));
}
proto method sinh(|$) {*}
multi method sinh(Num:D: ) {
nqp::p6box_n(nqp::sinh_n(nqp::unbox_n(self)));
}
proto method asinh(|$) {*}
multi method asinh(Num:D: ) {
(self + (self * self + 1).sqrt).log;
}
proto method cosh(|$) {*}
multi method cosh(Num:D: ) {
nqp::p6box_n(nqp::cosh_n(nqp::unbox_n(self)));
}
proto method acosh(|$) {*}
multi method acosh(Num:D: ) {
(self + (self * self - 1).sqrt).log;
}
proto method tanh(|$) {*}
multi method tanh(Num:D: ) {
nqp::p6box_n(nqp::tanh_n(nqp::unbox_n(self)));
}
proto method atanh(|$) {*}
multi method atanh(Num:D: ) {
((1 + self) / (1 - self)).log / 2;
}
proto method sech(|$) {*}
multi method sech(Num:D: ) {
nqp::p6box_n(nqp::sech_n(nqp::unbox_n(self)));
}
proto method asech(|$) {*}
multi method asech(Num:D: ) {
(1 / self).acosh;
}
proto method cosech(|$) {*}
multi method cosech(Num:D: ) {
nqp::p6box_n(nqp::div_n(1, nqp::sinh_n(nqp::unbox_n(self))));
}
proto method acosech(|$) {*}
multi method acosech(Num:D: ) {
(1 / self).asinh;
}
proto method cotanh(|$) {*}
multi method cotanh(Num:D: ) {
nqp::p6box_n(nqp::div_n(1, nqp::tanh_n(nqp::unbox_n(self))));
}
proto method acotanh(|$) {*}
multi method acotanh(Num:D: ) {
(1 / self).atanh;
}
}
my constant pi = 3.14159265e0;
my constant e = 2.71828183e0;
multi prefix:<++>(Num:D \$a is rw) { # XXX
$a = nqp::p6box_n(nqp::add_n(nqp::unbox_n($a), 1))
}
multi prefix:<++>(Num:U \$a is rw) { # XXX
$a = 1e0;
}
multi prefix:<-->(Num:D \$a is rw) { # XXX
$a = nqp::p6box_n(nqp::sub_n(nqp::unbox_n($a), 1))
}
multi prefix:<-->(Num:U \$a is rw) { # XXX
$a = -1e0;
}
multi postfix:<++>(Num:D \$a is rw) { # XXX
my $b = $a;
$a = nqp::p6box_n(nqp::add_n(nqp::unbox_n($a), 1));
$b
}
multi postfix:<++>(Num:U \$a is rw) { # XXX
$a = 1e0;
0
}
multi postfix:<-->(Num:D \$a is rw) { # XXX
my $b = $a;
$a = nqp::p6box_n(nqp::sub_n(nqp::unbox_n($a), 1));
$b
}
multi postfix:<-->(Num:U \$a is rw) { # XXX
$a = -1e0;
0
}
multi prefix:<->(Num:D \$a) {
nqp::p6box_n(nqp::neg_n(nqp::unbox_n($a)))
}
multi prefix:<->(num $a) {
nqp::neg_n($a);
}
multi prefix:<abs>(Num:D \$a) {
nqp::p6box_n(nqp::abs_n(nqp::unbox_n($a)))
}
multi prefix:<abs>(num $a) {
nqp::abs_n($a)
}
multi infix:<+>(Num:D \$a, Num:D \$b) {
nqp::p6box_n(nqp::add_n(nqp::unbox_n($a), nqp::unbox_n($b)))
}
multi infix:<+>(num $a, num $b) {
nqp::add_n($a, $b)
}
multi infix:<->(Num:D \$a, Num:D \$b) {
nqp::p6box_n(nqp::sub_n(nqp::unbox_n($a), nqp::unbox_n($b)))
}
multi infix:<->(num $a, num $b) {
nqp::sub_n($a, $b)
}
multi infix:<*>(Num:D \$a, Num:D \$b) {
nqp::p6box_n(nqp::mul_n(nqp::unbox_n($a), nqp::unbox_n($b)))
}
multi infix:<*>(num \$a, num \$b) {
nqp::mul_n($a, $b)
}
multi infix:</>(Num:D \$a, Num:D \$b) {
nqp::p6box_n(nqp::div_n(nqp::unbox_n($a), nqp::unbox_n($b)))
}
multi infix:</>(num $a, num $b) {
nqp::div_n($a, $b)
}
multi infix:<%>(Num:D \$a, Num:D \$b) {
nqp::p6box_n(nqp::mod_n(nqp::unbox_n($a), nqp::unbox_n($b)))
}
multi infix:<%>(num $a, num $b) {
nqp::mod_n($a, $b)
}
multi infix:<**>(Num:D \$a, Num:D \$b) {
nqp::p6box_n(nqp::pow_n(nqp::unbox_n($a), nqp::unbox_n($b)))
}
multi infix:<**>(num $a, num $b) {
nqp::pow_n($a, $b)
}
multi infix:<cmp>(Num:D \$a, Num:D \$b) {
nqp::p6box_i(nqp::cmp_n(nqp::unbox_n($a), nqp::unbox_n($b)))
}
multi infix:<cmp>(num $a, num $b) {
nqp::cmp_n($a, $b)
}
multi infix:«<=>»(Num:D \$a, Num:D \$b) {
nqp::p6box_i(nqp::cmp_n(nqp::unbox_n($a), nqp::unbox_n($b)))
}
multi infix:«<=>»(num $a, num $b) returns int {
nqp::cmp_n($a, $b)
}
multi infix:<===>(Num:D \$a, Num:D \$b) {
nqp::p6bool(nqp::iseq_n(nqp::unbox_n($a), nqp::unbox_n($b)))
}
multi infix:<===>(num $a, num $b) returns Bool:D {
nqp::p6bool(nqp::iseq_n($a, $b))
}
multi infix:<==>(Num:D \$a, Num:D \$b) returns Bool:D {
nqp::p6bool(nqp::iseq_n(nqp::unbox_n($a), nqp::unbox_n($b)))
}
multi infix:<==>(num $a, num $b) returns Bool:D {
nqp::p6bool(nqp::iseq_n($a, $b))
}
multi infix:<!=>(Num:D \$a, Num:D \$b) returns Bool:D {
nqp::p6bool(nqp::isne_n(nqp::unbox_n($a), nqp::unbox_n($b)))
}
multi infix:<!=>(num $a, num $b) returns Bool:D {
nqp::p6bool(nqp::isne_n($a, $b))
}
multi infix:«<»(Num:D \$a, Num:D \$b) returns Bool:D {
nqp::p6bool(nqp::islt_n(nqp::unbox_n($a), nqp::unbox_n($b)))
}
multi infix:«<»(num $a, num $b) returns Bool:D {
nqp::p6bool(nqp::islt_n($a, $b))
}
multi infix:«<=»(Num:D \$a, Num:D \$b) returns Bool:D {
nqp::p6bool(nqp::isle_n(nqp::unbox_n($a), nqp::unbox_n($b)))
}
multi infix:«<=»(num $a, num $b) returns Bool:D {
nqp::p6bool(nqp::isle_n($a, $b))
}
multi infix:«>»(Num:D \$a, Num:D \$b) returns Bool:D {
nqp::p6bool(nqp::isgt_n(nqp::unbox_n($a), nqp::unbox_n($b)))
}
multi infix:«>»(num $a, num $b) returns Bool:D {
nqp::p6bool(nqp::isgt_n($a, $b))
}
multi infix:«>=»(Num:D \$a, Num:D \$b) returns Bool:D {
nqp::p6bool(nqp::isge_n(nqp::unbox_n($a), nqp::unbox_n($b)))
}
multi infix:«>=»(num $a, num $b) returns Bool:D {
nqp::p6bool(nqp::isge_n($a, $b))
}
sub rand() returns Num:D {
nqp::p6box_n(pir::rand__NN(1));
}
# TODO: default seed of 'time'
sub srand(Int $seed) returns Int:D {
nqp::p6box_i(pir::srand__0I($seed))
}
multi sub atan2(Num:D $a, Num:D $b = 1e0) {
nqp::p6box_n(nqp::atan2_n(nqp::unbox_n($a), nqp::unbox_n($b)));
}
multi sub cosec(Num:D \$x) {
nqp::p6box_n(nqp::div_n(1, nqp::sin_n(nqp::unbox_n($x))));
}
multi sub acosec(Num:D \$x) {
nqp::p6box_n(nqp::asin_n(nqp::div_n(1, nqp::unbox_n($x))));
}
multi sub log(num $x) {
nqp::log_n($x);
}
multi sub sin(num $x) {
nqp::sin_n($x);
}
multi sub asin(num $x) {
nqp::asin_n($x);
}
multi sub cos(num $x) {
nqp::cos_n($x);
}
multi sub acos(num $x) {
nqp::acos_n($x);
}
multi sub tan(num $x) {
nqp::tan_n($x);
}
multi sub atan(num $x) {
nqp::atan_n($x);
}
multi sub sec(num $x) {
nqp::sec_n($x);
}
multi sub asec(num $x) {
nqp::asec_n($x);
}
multi sub cotan(num $x) {
nqp::div_n(1, nqp::tan_n($x));
}
multi sub acotan(num $x) {
nqp::div_n(1, nqp::atan_n($x));
}
multi sub sinh(num $x) {
nqp::sinh_n($x);
}
multi sub asinh(num $x) {
log($x + ($x * $x + 1e0));
}
multi sub cosh(num $x) {
nqp::cosh_n($x);
}
multi sub acosh(num $x) {
log($x + ($x * $x - 1e0))
}
multi sub tanh(num $x) {
nqp::tanh_n($x);
}
multi sub atanh(num $x) {
log((1 + $x) / (1 - $x)) / 2e0;
}
multi sub sech(num $x) {
nqp::sech_n($x);
}
multi sub asech(num $x) {
acosh(1e0 / $x);
}
multi sub cosech(num $x) {
1e0 / sinh($x)
}
multi sub acosech(num $x) {
asinh(1e0 / $x);
}
multi sub cotanh(num $x) {
1e0 / tanh($x);
}
multi sub acotanh(num $x) {
atanh(1e0 / $x)
}