-
-
Notifications
You must be signed in to change notification settings - Fork 373
/
Num.pm6
621 lines (563 loc) · 16.8 KB
/
Num.pm6
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
my class X::Cannot::Capture { ... }
my class X::Numeric::DivideByZero { ... }
my class X::Numeric::CannotConvert { ... }
my class Num does Real { # declared in BOOTSTRAP
# class Num is Cool
# has num $!value is box_target;
multi method WHICH(Num:D:) {
nqp::box_s(
nqp::concat(
nqp::if(
nqp::eqaddr(self.WHAT,Num),
'Num|',
nqp::concat(nqp::unbox_s(self.^name), '|')
),
nqp::unbox_n(self)
),
ValueObjAt
)
}
multi method Bool(Num:D:) { nqp::p6bool(nqp::isne_n(self,0e0)) }
method Capture() { die X::Cannot::Capture.new: :what(self) }
method Num() { self }
method Bridge(Num:D:) { self }
method Range(Num:U:) { Range.new(-Inf,Inf) }
method Int(Num:D:) {
nqp::isnanorinf(nqp::unbox_n(self))
?? X::Numeric::CannotConvert.new(:source(self), :target(Int)).fail
!! nqp::fromnum_I(nqp::unbox_n(self),Int)
}
multi method new() { nqp::box_n(0e0, self) }
multi method new($n) { nqp::box_n($n.Num, self) }
multi method perl(Num:D:) {
my str $res = self.Str;
nqp::isnanorinf(nqp::unbox_n(self))
|| nqp::isge_i(nqp::index($res,'e'),0)
|| nqp::isge_i(nqp::index($res,'E'),0)
?? $res
!! nqp::concat($res,'e0')
}
method Rat(Num:D: Real $epsilon = 1.0e-6, :$fat) {
my \RAT = $fat ?? FatRat !! Rat;
return RAT.new: (
nqp::iseq_n(self, self) ?? nqp::iseq_n(self, Inf) ?? 1 !! -1 !! 0
), 0
if nqp::isnanorinf(nqp::unbox_n(self));
my Num $num = self;
$num = -$num if (my int $signum = $num < 0);
my num $r = $num - floor($num);
# basically have an Int
if nqp::iseq_n($r,0e0) {
RAT.new(nqp::fromnum_I(self,Int),1)
}
# find convergents of the continued fraction.
else {
my Int $q = nqp::fromnum_I($num, Int);
my Int $a = 1;
my Int $b = $q;
my Int $c = 0;
my Int $d = 1;
while nqp::isne_n($r,0e0) && abs($num - ($b / $d)) > $epsilon {
my num $modf_arg = 1e0 / $r;
$q = nqp::fromnum_I($modf_arg, Int);
$r = $modf_arg - floor($modf_arg);
my $orig_b = $b;
$b = $q * $b + $a;
$a = $orig_b;
my $orig_d = $d;
$d = $q * $d + $c;
$c = $orig_d;
}
# 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.
RAT.new($signum ?? -$b !! $b, $d)
}
}
method FatRat(Num:D: Real $epsilon = 1.0e-6) {
self.Rat($epsilon, :fat);
}
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(nqp::rand_n(nqp::unbox_n(self)));
}
method ceiling(Num:D: ) {
nqp::isnanorinf(nqp::unbox_n(self))
?? self
!! nqp::fromnum_I(nqp::ceil_n(nqp::unbox_n(self)), Int);
}
method floor(Num:D: ) {
nqp::isnanorinf(nqp::unbox_n(self))
?? self
!! nqp::fromnum_I(nqp::floor_n(nqp::unbox_n(self)), Int);
}
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(1e0, nqp::sin_n(nqp::unbox_n(self))));
}
method acosec(Num:D:) {
nqp::p6box_n(nqp::asin_n(nqp::div_n(1e0, nqp::unbox_n(self))));
}
method cotan(Num:D:) {
nqp::p6box_n(nqp::div_n(1e0, nqp::tan_n(nqp::unbox_n(self))));
}
method acotan(Num:D:) {
nqp::p6box_n(nqp::atan_n(nqp::div_n(1e0, 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: ) {
nqp::isnanorinf(self)
?? self
!! (self + (self * self + 1e0).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 < 1e0
?? NaN
!! (self + (self * self - 1e0).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(1e0:) { ∞ }
multi method atanh(Num:D: ) {
((1e0 + self) / (1e0 - self)).log / 2e0;
}
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: ) {
(1e0 / self).acosh;
}
proto method cosech(|) {*}
multi method cosech(Num:D: ) {
nqp::p6box_n(nqp::div_n(1e0, nqp::sinh_n(nqp::unbox_n(self))));
}
proto method acosech(|) {*}
multi method acosech(Num:D: ) {
(1e0 / self).asinh;
}
proto method cotanh(|) {*}
multi method cotanh(Num:D: ) {
nqp::p6box_n(nqp::div_n(1e0, nqp::tanh_n(nqp::unbox_n(self))));
}
proto method acotanh(|) {*}
multi method acotanh(Num:D: ) {
(1e0 / self).atanh;
}
method is-prime(--> Bool:D) {
nqp::p6bool(
nqp::if(
nqp::isnanorinf(self),
False,
nqp::if(
nqp::iseq_n(self,nqp::floor_n(self)),
nqp::fromnum_I(self,Int).is-prime
)
)
)
}
method narrow(Num:D:) {
my $i := self.Int;
$i.defined && $i.Num ≅ self
?? $i
!! self
}
}
my constant tau = 6.28318_53071_79586_476e0;
my constant pi = 3.14159_26535_89793_238e0;
my constant e = 2.71828_18284_59045_235e0;
my constant π := pi;
my constant τ := tau;
#?if moar
my constant 𝑒 := e;
#?endif
multi sub prefix:<++>(Num:D $a is rw) {
$a = nqp::p6box_n(nqp::add_n(nqp::unbox_n($a), 1e0))
}
multi sub prefix:<++>(Num:U $a is rw) {
$a = 1e0;
}
multi sub prefix:<++>(num $a is rw --> num) {
$a = nqp::add_n($a, 1e0)
}
multi sub prefix:<-->(Num:D $a is rw) {
$a = nqp::p6box_n(nqp::sub_n(nqp::unbox_n($a), 1e0))
}
multi sub prefix:<-->(Num:U $a is rw) {
$a = -1e0;
}
multi sub prefix:<-->(num $a is rw --> num) {
$a = nqp::sub_n($a, 1e0)
}
multi sub postfix:<++>(Num:D $a is rw) {
my $b = $a;
$a = nqp::p6box_n(nqp::add_n(nqp::unbox_n($a), 1e0));
$b
}
multi sub postfix:<++>(Num:U $a is rw) {
$a = 1e0;
0e0
}
multi sub postfix:<++>(num $a is rw --> num) {
my num $b = $a;
$a = nqp::add_n($a, 1e0);
$b
}
multi sub postfix:<-->(Num:D $a is rw) {
my $b = $a;
$a = nqp::p6box_n(nqp::sub_n(nqp::unbox_n($a), 1e0));
$b
}
multi sub postfix:<-->(Num:U $a is rw) {
$a = -1e0;
0e0
}
multi sub postfix:<-->(num $a is rw --> num) {
my num $b = $a;
$a = nqp::sub_n($a, 1e0);
$b
}
multi sub prefix:<->(Num:D \a) {
nqp::p6box_n(nqp::neg_n(nqp::unbox_n(a)))
}
multi sub prefix:<->(num $a --> num) {
nqp::neg_n($a);
}
multi sub abs(Num:D \a) {
nqp::p6box_n(nqp::abs_n(nqp::unbox_n(a)))
}
multi sub abs(num $a --> num) {
nqp::abs_n($a)
}
multi sub infix:<+>(Num:D \a, Num:D \b) {
nqp::p6box_n(nqp::add_n(nqp::unbox_n(a), nqp::unbox_n(b)))
}
multi sub infix:<+>(num $a, num $b --> num) {
nqp::add_n($a, $b)
}
multi sub infix:<->(Num:D \a, Num:D \b) {
nqp::p6box_n(nqp::sub_n(nqp::unbox_n(a), nqp::unbox_n(b)))
}
multi sub infix:<->(num $a, num $b --> num) {
nqp::sub_n($a, $b)
}
multi sub infix:<*>(Num:D \a, Num:D \b) {
nqp::p6box_n(nqp::mul_n(nqp::unbox_n(a), nqp::unbox_n(b)))
}
multi sub infix:<*>(num $a, num $b --> num) {
nqp::mul_n($a, $b)
}
multi sub infix:</>(Num:D \a, Num:D \b) {
b
?? nqp::p6box_n(nqp::div_n(nqp::unbox_n(a), nqp::unbox_n(b)))
!! Failure.new(X::Numeric::DivideByZero.new(:using</>, :numerator(a)))
}
multi sub infix:</>(num $a, num $b --> num) {
$b
?? nqp::div_n($a, $b)
!! Failure.new(X::Numeric::DivideByZero.new(:using</>, :numerator($a)))
}
multi sub infix:<%>(Num:D \a, Num:D \b) {
b
?? nqp::p6box_n(nqp::mod_n(nqp::unbox_n(a), nqp::unbox_n(b)))
!! Failure.new(X::Numeric::DivideByZero.new(:using<%>, :numerator(a)))
}
multi sub infix:<%>(num $a, num $b --> num) {
$b
?? nqp::mod_n($a, $b)
!! Failure.new(X::Numeric::DivideByZero.new(:using<%>, :numerator($a)))
}
# (If we get 0 here, must be underflow, since floating overflow provides Inf.)
multi sub infix:<**>(Num:D \a, Num:D \b) {
nqp::p6box_n(nqp::pow_n(nqp::unbox_n(a), nqp::unbox_n(b)))
or a == 0e0 || b.abs == Inf
?? 0e0
!! Failure.new(X::Numeric::Underflow.new)
}
multi sub infix:<**>(num $a, num $b --> num) {
nqp::pow_n($a, $b)
or $a == 0e0 || $b.abs == Inf
?? 0e0
!! Failure.new(X::Numeric::Underflow.new)
}
# Here we sort NaN in with string "NaN"
multi sub infix:<cmp>(Num:D \a, Num:D \b) {
ORDER(nqp::cmp_n(nqp::unbox_n(a), nqp::unbox_n(b))) or
a === b ?? Same # === cares about signed zeros, we don't, so:
!! nqp::iseq_n(a, 0e0) && nqp::iseq_n(b, 0e0)
?? Same !! a.Stringy cmp b.Stringy;
}
multi sub infix:<cmp>(num $a, num $b) {
ORDER(nqp::cmp_n($a, $b)) or
$a === $b ?? Same # === cares about signed zeros, we don't, so:
!! nqp::iseq_n($a, 0e0) && nqp::iseq_n($b, 0e0)
?? Same !! $a.Stringy cmp $b.Stringy;
}
# Here we treat NaN as undefined
multi sub infix:«<=>»(Num:D \a, Num:D \b) {
ORDER(nqp::cmp_n(nqp::unbox_n(a), nqp::unbox_n(b))) or
a == b ?? Same !! Nil;
}
multi sub infix:«<=>»(num $a, num $b) {
ORDER(nqp::cmp_n($a, $b)) or
$a == $b ?? Same !! Nil;
}
multi sub infix:<===>(Num:D \a, Num:D \b) {
nqp::p6bool(
nqp::eqaddr(a.WHAT,b.WHAT)
&& (
( # Both are NaNs
nqp::not_i(nqp::isle_n(a, nqp::inf))
&& nqp::not_i(nqp::isle_n(b, nqp::inf))
)
|| (
nqp::iseq_n(a, b)
&& ( # if we're dealing with zeros, ensure the signs match
nqp::isne_n(a, 0e0)
|| nqp::if( # 1/-0 = -Inf; 1/0 = +Inf
nqp::islt_n(nqp::div_n(1e0,a), 0e0), # a is -0, if true:
nqp::islt_n(nqp::div_n(1e0,b), 0e0), # check b is -0 too
nqp::isgt_n(nqp::div_n(1e0,b), 0e0), # check b is +0 too
)
)
)
)
)
}
multi sub infix:<===>(num \a, num \b --> Bool:D) {
nqp::p6bool(
nqp::eqaddr(a.WHAT,b.WHAT)
&& (
( # Both are NaNs
nqp::not_i(nqp::isle_n(a, nqp::inf))
&& nqp::not_i(nqp::isle_n(b, nqp::inf))
)
|| (
nqp::iseq_n(a, b)
&& ( # if we're dealing with zeros, ensure the signs match
nqp::isne_n(a, 0e0)
|| nqp::if( # 1/-0 = -Inf; 1/0 = +Inf
nqp::islt_n(nqp::div_n(1e0,a), 0e0), # a is -0, if true:
nqp::islt_n(nqp::div_n(1e0,b), 0e0), # check b is -0 too
nqp::isgt_n(nqp::div_n(1e0,b), 0e0), # check b is +0 too
)
)
)
)
)
}
multi sub infix:<≅>( Inf, Inf) { Bool::True }
multi sub infix:<≅>(-Inf, -Inf) { Bool::True }
multi sub infix:<==>(Num:D \a, Num:D \b --> Bool:D) {
nqp::p6bool(nqp::iseq_n(nqp::unbox_n(a), nqp::unbox_n(b)))
}
multi sub infix:<==>(num $a, num $b --> Bool:D) {
nqp::p6bool(nqp::iseq_n($a, $b))
}
multi sub infix:<!=>(num $a, num $b --> Bool:D) {
nqp::p6bool(nqp::isne_n($a, $b))
}
multi sub infix:«<»(Num:D \a, Num:D \b --> Bool:D) {
nqp::p6bool(nqp::islt_n(nqp::unbox_n(a), nqp::unbox_n(b)))
}
multi sub infix:«<»(num $a, num $b --> Bool:D) {
nqp::p6bool(nqp::islt_n($a, $b))
}
multi sub infix:«<=»(Num:D \a, Num:D \b --> Bool:D) {
nqp::p6bool(nqp::isle_n(nqp::unbox_n(a), nqp::unbox_n(b)))
}
multi sub infix:«<=»(num $a, num $b --> Bool:D) {
nqp::p6bool(nqp::isle_n($a, $b))
}
multi sub infix:«>»(Num:D \a, Num:D \b --> Bool:D) {
nqp::p6bool(nqp::isgt_n(nqp::unbox_n(a), nqp::unbox_n(b)))
}
multi sub infix:«>»(num $a, num $b --> Bool:D) {
nqp::p6bool(nqp::isgt_n($a, $b))
}
multi sub infix:«>=»(Num:D \a, Num:D \b --> Bool:D) {
nqp::p6bool(nqp::isge_n(nqp::unbox_n(a), nqp::unbox_n(b)))
}
multi sub infix:«>=»(num $a, num $b --> Bool:D) {
nqp::p6bool(nqp::isge_n($a, $b))
}
proto sub rand(*%) {*}
multi sub rand(--> Num:D) { nqp::p6box_n(nqp::rand_n(1e0)) }
proto sub srand($, *%) {*}
multi sub srand(Int:D $seed --> Int:D) { nqp::p6box_i(nqp::srand($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(1e0, nqp::sin_n(nqp::unbox_n(x))));
}
multi sub acosec(Num:D \x) {
nqp::p6box_n(nqp::asin_n(nqp::div_n(1e0, nqp::unbox_n(x))));
}
multi sub log(num $x --> num) {
nqp::log_n($x);
}
multi sub sin(num $x --> num) {
nqp::sin_n($x);
}
multi sub asin(num $x --> num) {
nqp::asin_n($x);
}
multi sub cos(num $x --> num) {
nqp::cos_n($x);
}
multi sub acos(num $x --> num) {
nqp::acos_n($x);
}
multi sub tan(num $x --> num) {
nqp::tan_n($x);
}
multi sub atan(num $x --> num) {
nqp::atan_n($x);
}
multi sub sec(num $x --> num) {
nqp::sec_n($x);
}
multi sub asec(num $x --> num) {
nqp::asec_n($x);
}
multi sub cotan(num $x --> num) {
nqp::div_n(1e0, nqp::tan_n($x));
}
multi sub acotan(num $x --> num) {
nqp::atan_n(nqp::div_n(1e0, $x));
}
multi sub sinh(num $x --> num) {
nqp::sinh_n($x);
}
multi sub asinh(num $x --> num) {
# ln(x + √(x²+1))
nqp::isnanorinf($x)
?? $x
!! nqp::log_n(
nqp::add_n(
$x,
nqp::pow_n( nqp::add_n(nqp::mul_n($x,$x), 1e0), .5e0 )
)
)
}
multi sub cosh(num $x --> num) {
nqp::cosh_n($x);
}
multi sub acosh(num $x --> num) {
# ln(x + √(x²-1))
$x < 1e0
?? NaN
!! nqp::log_n(
nqp::add_n(
$x,
nqp::pow_n( nqp::sub_n(nqp::mul_n($x,$x), 1e0), .5e0 )
)
)
}
multi sub tanh(num $x --> num) {
nqp::tanh_n($x);
}
multi sub atanh(num $x --> num) {
$x == 1e0 ?? Inf !! log((1e0 + $x) / (1e0 - $x)) / 2e0;
}
multi sub sech(num $x --> num) {
nqp::sech_n($x);
}
multi sub asech(num $x --> num) {
acosh(1e0 / $x);
}
multi sub cosech(num $x --> num) {
1e0 / sinh($x)
}
multi sub acosech(num $x --> num) {
asinh(1e0 / $x);
}
multi sub cotanh(num $x --> num) {
1e0 / tanh($x);
}
multi sub acotanh(num $x --> num) {
atanh(1e0 / $x)
}
multi sub floor(num $a --> num) {
nqp::floor_n($a)
}
multi sub ceiling(num $a --> num) {
nqp::ceil_n($a)
}
multi sub sqrt(num $a --> num) {
nqp::sqrt_n($a)
}
# vim: ft=perl6 expandtab sw=4