forked from arturo-lang/arturo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumbers.nim
1504 lines (1389 loc) · 53.2 KB
/
Numbers.nim
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#=======================================================
# Arturo
# Programming Language + Bytecode VM compiler
# (c) 2019-2024 Yanis Zafirópulos
#
# @file: library/Numbers.nim
#=======================================================
## The main Numbers module
## (part of the standard library)
#=======================================
# Pragmas
#=======================================
{.used.}
#=======================================
# Libraries
#=======================================
import math, random, sequtils, sugar
when defined(WEB):
import std/jsbigints
when defined(GMP):
import helpers/bignums as BignumsHelper
import helpers/maths
import helpers/ranges
import vm/values/custom/vrange
import vm/values/custom/vquantity
import vm/errors
import vm/lib
#=======================================
# Helpers
#=======================================
template processTrigonometric(fun: untyped): untyped =
var v = x
if xKind == Quantity:
v = newQuantity(x.q.convertTo(parseAtoms("rad")))
if v.kind==Complex: push(newComplex(fun(v.z)))
elif v.kind==Rational: push(newRational(fun(toFloat(v.rat))))
else: push(newFloating(fun(asFloat(v))))
#=======================================
# Definitions
#=======================================
# TODO(Numbers) add `cbrt` built-in function
# the goal would be to have a function that returns the cubic root of a number
# potential use: https://rosettacode.org/wiki/Cubic_special_primes
# labels: library, enhancement, new feature
proc defineLibrary*() =
#----------------------------
# Functions
#----------------------------
builtin "abs",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "get the absolute value for given integer",
args = {
"value" : {Integer,Floating,Complex,Rational}
},
attrs = NoAttrs,
returns = {Integer,Floating},
example = """
print abs 6 ; 6
print abs 6-7 ; 1
..........
abs to :complex @[pi 1]
; => 3.296908309475615
""":
#=======================================================
if xKind==Integer:
if x.iKind==NormalInteger:
push(newInteger(abs(x.i)))
else:
when defined(WEB) or defined(GMP):
push(newInteger(abs(x.bi)))
elif xKind==Floating:
push(newFloating(abs(x.f)))
elif xKind==Complex:
push(newFloating(abs(x.z)))
else:
push(newRational(abs(x.rat)))
builtin "acos",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the inverse cosine of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print acos 0 ; 1.570796326794897
print acos 0.3 ; 1.266103672779499
print acos 1.0 ; 0.0
..........
acos to :complex @[pi 1]
; => 0.3222532939814587-1.86711439316026i
""":
#=======================================================
processTrigonometric(arccos)
builtin "acosh",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the inverse hyperbolic cosine of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print acosh 1.0 ; 0.0
print acosh 2 ; 1.316957896924817
print acosh 5.0 ; 2.292431669561178
..........
acosh to :complex @[pi 1]
; => 1.86711439316026+0.3222532939814587i
""":
#=======================================================
processTrigonometric(arccosh)
builtin "acsec",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the inverse cosecant of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print acsec 0 ; nan
print acsec 1.0 ; 1.570796326794897
print acsec 10 ; 0.1001674211615598
..........
acsec to :complex @[pi 1]
; => 0.2918255976444114-0.0959139808172324i
""":
#=======================================================
processTrigonometric(arccsc)
builtin "acsech",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the inverse hyperbolic cosecant of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print acsech 0 ; inf
print acsech 1.0 ; 0.0
print acsech 10 ; 0.09983407889920758
..........
acsech to :complex @[pi 1]
; => 0.2862356627279947-0.08847073864038091i
""":
#=======================================================
processTrigonometric(arccsch)
builtin "actan",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the inverse cotangent of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print actan 0 ; 1.570796326794897
print actan 1 ; 0.7853981633974483
print actan 10.0 ; 0.09966865249116204
..........
actan to :complex @[pi 1]
; => 0.2834557524705047-0.08505998507745414i
""":
#=======================================================
processTrigonometric(arccot)
builtin "actanh",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the inverse hyperbolic cotangent of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print actanh 0 ; nan
print actanh 1 ; inf
print actanh 10.0 ; 0.1003353477310756
..........
actanh to :complex @[pi 1]
; => 0.2946214403408572-0.09996750087543603i
""":
#=======================================================
processTrigonometric(arccoth)
builtin "angle",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the phase angle of given number",
args = {
"number" : {Complex}
},
attrs = NoAttrs,
returns = {Floating},
example = """
a: to complex [1 1] ; a: 1.0+1.0i
print angle a ; 0.7853981633974483
""":
#=======================================================
push(newFloating(phase(x.z)))
builtin "asec",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the inverse secant of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print asec 0 ; nan
print asec 45 ; 1.548572275176629
print asec 5 ; 1.369438406004566
..........
asec to :complex @[pi 1]
; => 1.278970729150485+0.09591398081723231i
""":
#=======================================================
processTrigonometric(arcsec)
builtin "asech",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the inverse hyperbolic secant of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print asech 0 ; inf
print asech 0.45 ; 1.436685652839686
print asech 1 ; 0.0
..........
asech to :complex @[pi 1]
; => 0.09591398081723221-1.278970729150485i
""":
#=======================================================
processTrigonometric(arcsech)
builtin "asin",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the inverse sine of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print asin 0 ; 0.0
print asin 0.3 ; 0.3046926540153975
print asin 1.0 ; 1.570796326794897
..........
asin to :complex @[pi 1]
; => 1.248543032813438+1.867114393160262i
""":
#=======================================================
processTrigonometric(arcsin)
builtin "asinh",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the inverse hyperbolic sine of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print asinh 0 ; 0.0
print asinh 0.3 ; 0.2956730475634224
print asinh 1.0 ; 0.881373587019543
..........
asinh to :complex @[pi 1]
; => 1.904627686970658+0.2955850342116299i
""":
#=======================================================
processTrigonometric(arcsinh)
builtin "atan",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the inverse tangent of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print atan 0 ; 0.0
print atan 0.3 ; 0.2914567944778671
print atan 1.0 ; 0.7853981633974483
..........
atan to :complex @[pi 1]
; => 1.287340574324392+0.08505998507745416i
""":
#=======================================================
processTrigonometric(arctan)
builtin "atan2",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the inverse tangent of y / x",
args = {
"y" : {Integer,Floating,Rational},
"x" : {Integer,Floating,Rational}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
atan2 1 1 ; 0.7853981633974483
atan2 1 1.5 ; 0.9827937232473291
""":
#=======================================================
push(newFloating(arctan2(asFloat(y), asFloat(x))))
builtin "atanh",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the inverse hyperbolic tangent of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print atanh 0 ; 0.0
print atanh 0.3 ; 0.3095196042031118
print atanh 1.0 ; inf
..........
atanh to :complex @[pi 1]
; => 0.2946214403408571+1.470828825919461i
""":
#=======================================================
processTrigonometric(arctanh)
builtin "ceil",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the smallest integer not smaller than given value",
args = {
"value" : {Integer,Floating,Rational}
},
attrs = NoAttrs,
returns = {Integer},
example = """
print ceil 2.1 ; 3
print ceil 2.9 ; 3
print ceil neg 3.5 ; -3
print ceil 4 ; 4
print ceil to :rational @[neg 7 2] ; -3
""":
#=======================================================
push(newInteger(int(ceil(asFloat(x)))))
builtin "clamp",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "force value within given range",
args = {
"number" : {Integer, Floating, Rational},
"range" : {Range, Block}
},
attrs = NoAttrs,
returns = {Integer, Floating, Rational},
example = """
clamp 2 1..3 ; 2
clamp 0 1..3 ; 1
clamp 4 1..3 ; 3
clamp 4 3..1 ; 3
clamp 5 range.step: 2 0 5 ; 4
clamp 4.5 0..6 ; 4.5
clamp to :rational [1 5] 0..1 ; 1/5
clamp 4.5 [1 2.5] ; 2.5
clamp 2 [5 10] ; 5
clamp 2 [10 5] ; 5
clamp 2.5 @[1 to :rational [5 2]] ; 2.5
""":
#=======================================================
case y.kind
of Range:
if not y.rng.numeric:
Error_IncompatibleValueType("clamp", valueKind(y), "numeric range")
if (let minElem = y.rng.min()[1]; x.asFloat < float(minElem.i)): push(minElem)
elif (let maxElem = y.rng.max()[1]; x.asFloat > float(maxElem.i)): push(maxElem)
else: push(x)
of Block:
y.requireBlockSize(2)
let firstElem {.cursor} = y.a[0]
let secondElem {.cursor} = y.a[1]
firstElem.requireValue({Integer, Floating, Rational})
secondElem.requireValue({Integer, Floating, Rational})
let minElem = min([firstElem, secondElem])
let maxElem = max([firstElem, secondElem])
if x.asFloat < minElem.asFloat: push(minElem)
elif x.asFloat > maxElem.asFloat: push(maxElem)
else: push(x)
else:
discard
builtin "conj",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the complex conjugate of given number",
args = {
"number" : {Complex}
},
attrs = NoAttrs,
returns = {Complex},
example = """
b: to :complex [1 2] ; b: 1.0+2.0i
print conj b ; 1.0-2.0i
""":
#=======================================================
push(newComplex(conjugate(x.z)))
builtin "cos",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the cosine of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print cos 0 ; 1.0
print cos 0.3 ; 0.955336489125606
print cos 1.0 ; 0.5403023058681398
..........
cos to :complex [1 1]
; => 0.8337300251311491-0.9888977057628651i
""":
#=======================================================
processTrigonometric(cos)
builtin "cosh",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the hyperbolic cosine of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print cosh 0 ; 1.0
print cosh 0.3 ; 1.04533851412886
print cosh 1.0 ; 1.543080634815244
..........
cosh to :complex [2 1]
; => 2.032723007019666+3.0518977991518i
""":
#=======================================================
processTrigonometric(cosh)
builtin "csec",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the cosecant of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print csec 0 ; inf
print csec 0.3 ; 3.383863361824123
print csec 1.0 ; 1.188395105778121
..........
csec to :complex [1 1]
; => 0.6215180171704283-0.3039310016284264i
""":
#=======================================================
processTrigonometric(csc)
builtin "csech",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the hyperbolic cosecant of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print csech 0 ; inf
print csech 0.3 ; 3.283853396698424
print csech 1.0 ; 0.8509181282393216
..........
csech to :complex [1 1]
; => 0.3039310016284264-0.6215180171704283i
""":
#=======================================================
processTrigonometric(csch)
builtin "ctan",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the cotangent of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print ctan 0 ; inf
print ctan 0.3 ; 3.232728143765828
print ctan 1.0 ; 0.6420926159343308
..........
ctan to :complex [1 1]
; => 0.2176215618544027-0.8680141428959249i
""":
#=======================================================
processTrigonometric(cot)
builtin "ctanh",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the hyperbolic cotangent of given angle",
args = {
"angle" : {Integer,Floating,Complex,Rational,Quantity}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print ctanh 0 ; inf
print ctanh 0.3 ; 3.432738430321741
print ctanh 1.0 ; 1.313035285499331
..........
ctanh to :complex [1 1]
; => 0.8680141428959249-0.2176215618544027i
""":
#=======================================================
processTrigonometric(coth)
builtin "denominator",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "get the denominator of given number",
args = {
"number" : {Integer,Floating,Rational}
},
attrs = NoAttrs,
returns = {Integer},
example = """
num: to :rational 12.4 ; num: 62/5
print denominator num
; => 5
..........
print denominator 10
; => 1
""":
#=======================================================
var rat: VRational
if xKind==Rational:
rat = x.rat
elif xKind==Integer:
rat = toRational(x.i)
else:
rat = toRational(x.f)
if rat.rKind == NormalRational:
push(newInteger(getDenominator(rat)))
else:
push(newInteger(getDenominator(rat, big=true)))
builtin "digits",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "convert a number into an array of digits or an array of digits back into a number",
args = {
"number" : {Integer, Block},
},
attrs = {
"base" : ({Integer},"use given based (default: 10)")
},
returns = {Block},
example = """
digits 123
; => [1 2 3]
digits [1 2 3]
; => 123
digits 0
; => [0]
digits neg 12345
; => [1 2 3 4 5]
; digits 1231231231231231231231231231023
; => [1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 0 2 3]
""":
#=======================================================
var base = 10
if checkAttr("base"):
base = aBase.i
if x.kind == Block:
var digits = x.a
var composedNumber = 0
for digit in digits:
requireValue(digit, {Integer})
composedNumber = composedNumber * base + digit.i
push newInteger(composedNumber)
else:
if x.iKind == NormalInteger:
push newBlock(getDigits(x.i, base).map((z)=>newInteger(z)))
else:
when defined(WEB) or defined(GMP):
push newBlock(getDigits(x.bi, base).map((z)=>newInteger(z)))
builtin "exp",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the exponential function for given value",
args = {
"value" : {Integer,Floating,Complex,Rational}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print exp 1.0 ; 2.718281828459045
print exp 0 ; 1.0
print exp neg 1.0 ; 0.3678794411714423
..........
exp to :complex @[pi 1]
; => 12.50296958887651+19.47222141884161i
""":
#=======================================================
if xKind==Complex: push(newComplex(exp(x.z)))
else: push(newFloating(exp(asFloat(x))))
builtin "factorial",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the factorial of given value",
args = {
"value" : {Integer}
},
attrs = NoAttrs,
returns = {Integer},
example = """
factorial 1 ; => 1
factorial 5 ; => 120
factorial 20 ; => 2432902008176640000
""":
#=======================================================
if unlikely(x.iKind == BigInteger):
Error_InvalidOperation("factorial", valueKind(x, withBigInfo=true), "")
else:
push(factorial(x.i))
builtin "factors",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "get list of factors for given integer",
args = {
"number" : {Integer}
},
attrs = {
"prime" : ({Logical},"prime factorization")
},
returns = {Block},
example = """
factors 16 ; => [1 2 4 8 16]
..........
factors.prime 48 ; => [2 2 2 2 3]
unique factors.prime 48 ; => [2 3]
factors.prime 18446744073709551615123120
; => [2 2 2 2 3 5 61 141529 26970107 330103811]
""":
#=======================================================
var prime = false
if (hadAttr("prime")): prime = true
if x.iKind==NormalInteger:
if prime:
push(newBlock(primeFactorization(x.i).map((x)=>newInteger(x))))
else:
push(newBlock(factors(x.i).map((x)=>newInteger(x))))
else:
when defined(WEB) or defined(GMP):
# TODO(Numbers\factors) `.prime` not working for Web builds
# labels: web,enhancement
if prime:
when not defined(WEB):
push(newBlock(primeFactorization(x.bi).map((x)=>newInteger(x))))
else:
discard
else:
push(newBlock(factors(x.bi).map((x)=>newInteger(x))))
builtin "floor",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the largest integer not greater than given value",
args = {
"value" : {Integer,Floating,Rational}
},
attrs = NoAttrs,
returns = {Integer},
example = """
print floor 2.1 ; 2
print floor 2.9 ; 2
print floor neg 3.5 ; -4
print floor 4 ; 4
print floor to :rational @[neg 7 2] ; -4
""":
#=======================================================
push(newInteger(int(floor(asFloat(x)))))
when not defined(WEB):
builtin "gamma",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the gamma function for given value",
args = {
"value" : {Integer,Floating,Rational}
},
attrs = NoAttrs,
returns = {Floating},
example = """
print gamma 3.0 ; 2.0
print gamma 10.0 ; 362880.0
print gamma 15 ; 87178291199.99985
""":
#=======================================================
push(newFloating(gamma(asFloat(x))))
builtin "gcd",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate greatest common divisor for given collection of integers",
args = {
"numbers" : {Block}
},
attrs = NoAttrs,
returns = {Integer},
example = """
print gcd [48 60 120] ; 12
""":
#=======================================================
var current = x.a[0]
requireValue(current, {Integer})
var i = 1
# TODO(Numbers\gcd) not working for Web builds
# labels: web,enhancement
while i<x.a.len:
let elem {.cursor.} = x.a[i]
requireValue(elem, {Integer})
if current.iKind==NormalInteger:
if elem.iKind==BigInteger:
when defined(GMP):
current = newInteger(gcd(current.i, elem.bi))
else:
current = newInteger(gcd(current.i, elem.i))
else:
when defined(GMP):
if elem.iKind==BigInteger:
current = newInteger(gcd(current.bi, elem.bi))
else:
current = newInteger(gcd(current.bi, elem.i))
inc(i)
push(current)
builtin "hypot",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the hypotenuse of a right-angle triangle with given base and height",
args = {
"base" : {Integer,Floating,Rational},
"height": {Integer,Floating,Rational}
},
attrs = NoAttrs,
returns = {Floating},
example = """
print hypot 3 4
; 5.0
print hypot 4.0 5.0
; 6.403124237432849
""":
#=======================================================
push(newFloating(hypot(asFloat(x), asFloat(y))))
builtin "lcm",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate least common multiplier for given collection of integers",
args = {
"numbers" : {Block}
},
attrs = NoAttrs,
returns = {Integer},
example = """
print lcm [48 60 120] ; 240
""":
#=======================================================
var current = x.a[0]
requireValue(current, {Integer})
var i = 1
# TODO(Numbers\lcm) not working for Web builds
# labels: web,enhancement
while i<x.a.len:
let elem {.cursor.} = x.a[i]
requireValue(elem, {Integer})
if current.iKind==NormalInteger:
if elem.iKind==BigInteger:
when defined(GMP):
current = newInteger(lcm(current.i, elem.bi))
else:
current = newInteger(lcm(current.i, elem.i))
else:
when defined(GMP):
if elem.iKind==BigInteger:
current = newInteger(lcm(current.bi, elem.bi))
else:
current = newInteger(lcm(current.bi, elem.i))
inc(i)
push(current)
builtin "ln",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the natural logarithm of given value",
args = {
"value" : {Integer,Floating,Complex,Rational}
},
attrs = NoAttrs,
returns = {Floating,Complex},
example = """
print ln 1.0 ; 0.0
print ln 0 ; -inf
print ln neg 7.0 ; nan
..........
ln to :complex @[pi 1]
; => 1.19298515341341+0.308169071115985i
""":
#=======================================================
if xKind==Complex: push(newComplex(ln(x.z)))
else: push(newFloating(ln(asFloat(x))))
builtin "log",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "calculate the logarithm of value using given base",
args = {
"value" : {Integer,Floating,Rational},
"base" : {Integer,Floating,Rational}
},
attrs = NoAttrs,
returns = {Floating},
example = """
print log 9 3 ; 2.0
print log 32.0 2.0 ; 5.0
print log 0.0 2 ; -inf
print log 100.0 10.0 ; 2.0
""":
#=======================================================
push(newFloating(log(asFloat(x),asFloat(y))))
builtin "numerator",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "get the numerator of given number",
args = {
"number" : {Integer,Floating,Rational}
},
attrs = NoAttrs,
returns = {Integer},
example = """
num: to :rational 12.4 ; num: 62/5
print numerator num
; => 62
..........
print numerator 10
; => 10
""":
#=======================================================
var rat: VRational
if xKind==Rational:
rat = x.rat
elif xKind==Integer:
rat = toRational(x.i)
else:
rat = toRational(x.f)
if rat.rKind == NormalRational:
push(newInteger(getNumerator(rat)))
else:
push(newInteger(getNumerator(rat, big=true)))
when defined(GMP):
# TODO(Numbers\powmod) not working for Web builds
# labels: web,enhancement
builtin "powmod",
alias = unaliased,
op = opNop,
rule = PrefixPrecedence,
description = "modular exponentation: calculate the result of (base^exponent) % divider",
args = {
"base" : {Integer},
"exponent" : {Integer},
"divider" : {Integer}
},
attrs = NoAttrs,
returns = {Integer,Null},
example = """
powmod 1 10 3 ; => 1
powmod 3 2 6 ; => 3
powmod 5 5 15 ; => 5
powmod 2 3 5 ; => 3
powmod 2 4 5 ; => 1