-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnum7.js
More file actions
1802 lines (1590 loc) · 79.8 KB
/
num7.js
File metadata and controls
1802 lines (1590 loc) · 79.8 KB
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
/** DEVELOPED ON AMD Ryzen 5 Mobile 3550H 16GB DDR4 DRAM AND NODE.JS SYSTEM v22.13.1 */
/**
* @file num7.js
* @author Giovanni Cipriani <giocip7@gmail.com>
* @date 2025
* @brief num7 ARBITRARY-PRECISION GENERAL PURPOSE ARITHMETIC-LOGIC DECIMAL CLASS FOR NODE.JS SYSTEM
*
* @see https://github.com/giocip/NODE_num7
*/
// DEFINE A CLASS WITH A CONSTRUCTOR, METHODS, AND 'this'
class Num {
//CONSTRUCTOR: INITIALIZES PROPERTIES WHEN AN INSTANCE IS CREATED
constructor(n, d = 80) {
//CLASS CONSTRUCTOR
//VALIDATION n
this.type = "Num" //'Num' TYPE SPECIFICATION
if(typeof n == "undefined") throw Error("Num CLASS CONSTRUCTOR => TypeError: missing 1 required positional argument: " + n)
else if(typeof n == "number") {
if (Number.isInteger(n)) n += ".0" //SUFFIX .0 FOR INTEGER TYPE
else throw Error("Num CLASS CONSTRUCTOR => number not valid: " + n) //process.exit(1)
}
else if (typeof n == "bigint") n += ".0" //SUFFIX .0 FOR ARBITRARY PRECISION INTEGER TYPE => 1000n
else if (n.type == "Num") {
this.d = d < n.d ? n.d : d
this.n = n.n
this.n0 = n.n0
this.n1 = n.n1
this.n2 = n.n2
this.L_n0 = n.L_n0
this.L_n1 = n.L_n1
return //this
}
else if (typeof n != "string") throw Error("Num CLASS CONSTRUCTOR => object data not valid: " + n)
let nn = n.replace(/_+/g, "") //CLEAR '_'
nn = nn.replace(/\s+/g, "") //CLEAR SPACE, TAB ECC.
nn = nn.toUpperCase() //ALL UPPERCASE
if (/E/.test(nn)) nn = Num.exp2num(nn) //CHECK FOR 'E' CHARACTER (EXPONENTIAL)
if (nn[0] == "-") {
this.n2 = "-"
nn = nn.slice(1)
} //NEGATIVE, REMOVE '-'
else if (nn[0] == "+") {
this.n2 = ""
nn = nn.slice(1)
} //POSITIVE, REMOVE '+'
else this.n2 = "" //POSITIVE
let nv = nn.split(".")
let nv0_isOnlyDigits = /^\d+$/.test(nv[0])
let nv1_isOnlyDigits = /^\d+$/.test(nv[1])
if (!nv0_isOnlyDigits || !nv1_isOnlyDigits)
throw Error("Num CLASS CONSTRUCTOR => typing error: " + n) //CHECK FOR ONLY DIGIT NUMBERIC STRING
this.n0 = nv[0]
this.n1 = nv[1]
this.L_n0 = this.n0.length
this.L_n1 = this.n1.length //CHECK FIRST TIME, LENGTH
if (!this.L_n0 || !this.L_n1)
throw Error("Num CLASS CONSTRUCTOR => missing string number: " + n)
if (this.L_n0 > 1) {
this.n0 = this.n0.replace(/^0+/, "") //CLEAR LEFT ZEROS
if (!this.n0.length) this.n0 = "0" //if ''
}
if (this.L_n1 > 1) {
this.n1 = this.n1.replace(/0+$/, "") //clear RIGHT zeros
if (!this.n1.length) this.n1 = "0" //if ''
}
this.L_n0 = this.n0.length
this.L_n1 = this.n1.length //CHECK FOR NEW LENGTH
this.n = this.n2 + this.n0 + "." + this.n1 //SET ALL NUMBER CLEANED
if (this.n0 == "0" && this.n1 == "0")
if (this.n2 == "-" || this.n[0] == "+") throw Error("Num CLASS CONSTRUCTOR => zero can not be signed: " + n)
this.d = d > (this.L_n0 + this.L_n1) ? d : (this.L_n0 + this.L_n1) //PRECISION
return //this
} //CONSTRUCTOR END
/** valueOf, Num RESULT FOR (+) OPERATOR */
// CODE: a = new Num('3.0'); console.log(a + ' Km') //3.0 Km
valueOf() { return this.n }
/** toString, Num DISPLAY FOR console.log */
// CODE: a = new Num('3.0'); console.log(a.toString()) //3.0
toString() { return this.n }
/** RETURN STRING COMMON STANDARD Round() -RELATIVE ROUND_HALF_CEIL d=1: 0.15 => 0.2 -0.15 => -0.1 */
// CODE: a = new Num(Num.pi); console.log(a.toString(), a.ToFixed()) //3.1415926535897932384626433832795 3.14
ToFixed(d = 2) { return this.Round(d).toString() }
/** length, Num DIGIT CHARACTER SIZE (RAM) */
// CODE: a = new Num('-3.141592654'); Num.print(a.length(), '\n') //23
length() { return this.n.length + this.L_n0 + this.L_n1 + this.n2.length }
/** (==) EQ, EQUAL LOGIC BINARY OPERATOR */
// CODE: a = new Num(3); b = new Num(3); console.log(a.EQ(b)) //true
EQ(sob) { sob = new Num(sob); return this.n == sob.n ? true : false; }
/** (!=) NE, NOT EQUAL LOGIC BINARY OPERATOR */
// CODE: a = new Num(3); b = new Num(33); console.log(a.NE(b)) //true
NE(sob) { sob = new Num(sob); return this.n != sob.n ? true : false; }
/** (>) GT, GREATER LOGIC BINARY OPERATOR */
// CODE: a = new Num(333); b = new Num(33); console.log(a.GT(b)) //true
GT(sob) {
sob = new Num(sob)
if (Num.int(this.n2 + this.n0) > Num.int(sob.n2 + sob.n0)) return true
let a, b, d_L1
if (Num.int(this.n2 + this.n0) == Num.int(sob.n2 + sob.n0)) {
d_L1 = this.L_n1 - sob.L_n1
if (d_L1 > 0) {
a = Num.int(this.n2 + this.n1)
b = Num.int(sob.n2 + sob.n1 + "".padStart(Math.abs(d_L1), "0"))
if (a > b) return true
} else if (d_L1 < 0) {
a = Num.int(this.n2 + this.n1 + "".padStart(Math.abs(d_L1), "0"))
b = Num.int(sob.n2 + sob.n1)
if (a > b) return true
} else { return Num.int(this.n2 + this.n1) > Num.int(sob.n2 + sob.n1) ? true : false }
}
return false
}
/** (>=) GE, GREATER OR EQUAL LOGIC BINARY OPERATOR */
// CODE: a = new Num(333); b = new Num(333); console.log(a.GE(b)) //true
GE(n) { let sob = new Num(n); return this.GT(sob) || this.EQ(sob) ? true : false }
/** (<) LT, LESS LOGIC BINARY OPERATOR */
// CODE: a = new Num(33); b = new Num(333); console.log(a.LT(b)) //true
LT(n) { let sob = new Num(n); return this.GE(sob) ? false : true }
/** (<=) LE, LESS OR EQUAL LOGIC BINARY OPERATOR */
// CODE: a = new Num(333); b = new Num(333); console.log(a.LE(b)) //true
LE(n) { let sob = new Num(n); return this.GT(sob) ? false : true }
/** Is_true, TRUE LOGIC UNARY FOR Num */
// CODE: a = new Num('-3.14'); if(a.Is_true()) console.log(a.toString(), 'true'); else console.log(a.toString(), 'false') //-3.14 true
Is_true() { return this.n == "0.0" ? false : true }
/** Is_false, FALSE LOGIC UNARY FOR Num */
// CODE: a = new Num('0.00'); if(a.Is_false()) console.log(a.toString(), 'true'); else console.log(a.toString(), 'false') //0.0 true
Is_false() { return this.n == "0.0" ? true : false }
/** Not, NOT LOGIC UNARY OPERATOR FOR Num */
// CODE: a = new Num('0.00'); if(a.Not()) console.log(a.toString(), 'true'); else console.log(a.toString(), 'false') //0.0 true
Not() { return this.n == "0.0" ? true : false }
/** (~) Notb, NOT UNARY BITWISE OPERATOR FOR Num */
// CODE:
// op1 = new Num('10.0')
// console.log(`${Num.int(op1).toString(2).padStart(8, '0')}`, op1.toString()) //00001010 10.0
// op2 = op1.Notb() //(~) NOT
// console.log(`${Num.int(op2).toString(2).padStart(8, '0')}`, op2.toString()) //00000101 5.0
Notb() {
if(!this.Is_numint() || this.n2) throw Error("Num.Notb => TypeError only positive integer allowed: " + this.n)
let t = ''
let bin = Num.int(this.n0).toString(2)
for(let i = 0; i < bin.length; i++) t += (bin[i] == '1' ? '0' : '1')
return new Num(BigInt('0b' + t))
}
/** Abs, RETURN THE ABSOLUTE VALUE OF A Num */
// CODE: a = new Num(-333); console.log(a.Abs().toString()) //333.0
Abs() { return new Num(this.n2 == "" ? this.n : this.n.slice(1)) }
/** Is_negative CHECK FOR Num NEGATIVE */
// CODE: a = new Num(-333); console.log(a.Is_negative()) //true
Is_negative() { if (this.n2 == "-") return true; return false }
/** Is_positive CHECK FOR Num POSITIVE */
// CODE: a = new Num(-333); console.log(a.Is_positive()) //false
Is_positive() { return !this.Is_negative() }
/** Is_zero CHECK FOR ZERO Num */
// CODE: a = new Num(0); console.log(a.Is_zero()) //true
Is_zero() {
if (this.n0 == "0" && this.n1 == "0" && this.n2 == "") return true //int, dec, sign Num PARTS
return false
}
/** Is_numint CHECKS IF INTEGER Num */
// CODE: a = new Num('3.00'); console.log(a.Is_numint()) //true
Is_numint() { if (this.n1 == "0") return true; return false }
/** Is_numfloat CHECKS IF FLOATING POINT Num */
// CODE: a = new Num('3.14'); console.log(a.Is_numfloat()) //true
Is_numfloat() { if (this.n1 != "0") return true; return false }
/** Is_numeven CHECK FOR Num EVEN (0 2 4 6 8) */
// CODE: a = new Num('8.00'); console.log(a.Is_numeven()) //true
Is_numeven() {
if (this.Is_numint()) {
if (this.Mod(2).Is_false()) return true
return false
}
throw Error("Num.Is_numeven => Num, must be integer value: ", this)
}
/** Is_numodd CHECK FOR Num ODD (1 3 5 7 9) */
// CODE: a = new Num('3.00'); console.log(a.Is_numodd()) //true
Is_numodd() { return this.Is_numeven() ? false : true }
/** (**) (EXPONENTIATION) POWER OPERATOR */
//CODE: a = new Num('3.00'); console.log(a.Pow(3).toString()) //27.0
//CODE: a = new Num('3.00'); console.log(a.Pow(-3).toString()) //0.037037037037037037037037037037037037037037037037037037037037037037037037037037035925925925925925925925925925925925925925925925925925925925925925925925925925925937037037037037037037037037037037037037037037037037037037037037037037037037037037
//CODE: a = new Num('-3.14'); console.log(a.Pow(4).toString()) //97.21171216
//CODE: a = new Num('-0.00140017007'); console.log(a.Pow(4).toString()) //0.00000000000384346702849149326098281069326401
Pow(E, d = 80) { //ARGUMENT E MANDATORY
let e = new Num(E)
if(Num.not(this) && Num.not(e)) throw Error("Num.Pow => UNDETERMINED: " + this.n0 + '^' + e.n0) //UNDETERMINED ERROR
if(Num.not(e)) return new Num(1); //POW ALWAYS ONE
if(e.Is_numfloat()) throw Error("Num.Pow => EXPONENT, must be integer value: " + e.toString()); //EXPONENT MUST BE INTEGER
else e = BigInt(e.n2 + e.n0)
if(e < 0n) {
e = -e
let b = this.Inv(d)
this.n2 = b.n2; this.n0 = b.n0; this.n1 = b.n1; this.L_n0 = b.L_n0; this.L_n1 = b.L_n1; this.n = b.n; this.d = b.d;
}
let DOT = Number(BigInt(this.L_n1) * e)
let result_str = String(BigInt(this.n0 + this.n1) ** e)
let decs_part = result_str.slice(-DOT)
let int_part = result_str.slice(0, -DOT)
return new Num((e % 2n ? this.n2 : '+') + (int_part ? int_part : '0') + '.' + ''.padStart(Number((DOT) - result_str.length), '0') + decs_part)
}
/** CLEAR VARIABLE */
//CODE: a = new Num('2.72'); a.Print(' '); a.Clear(); a.Print('\n') //2.72 0.0
Clear() {
let telf = this;
this.d = telf.d;
this.n = "0.0";
this.n0 = "0";
this.n1 = "0";
this.n2 = "";
this.L_n0 = 1;
this.L_n1 = 1;
return this;
}
/** INCREMENT ADDING VARIABLE METHOD -OBJECT MODIFY BY this REFERENCE */
//CODE: a = new Num('2.72'); a.Print(' '); a.Inc(); a.Print('\n') //2.72 3.72
Inc(sob = 1) {
let telf = this.Add(sob);
this.d = telf.d;
this.n = telf.n;
this.n0 = telf.n0;
this.n1 = telf.n1;
this.n2 = telf.n2;
this.L_n0 = telf.L_n0;
this.L_n1 = telf.L_n1;
return this;
}
/** INCREMENT MULTIPLYING VARIABLE METHOD -OBJECT MODIFY BY this REFERENCE */
//CODE: a = new Num('2.72'); a.Print(' '); a.IncMul(100); a.Print('\n') //2.72 272.0
IncMul(sob = 10) {
let telf = this.Mul(sob)
this.d = telf.d
this.n = telf.n
this.n0 = telf.n0
this.n1 = telf.n1
this.n2 = telf.n2
this.L_n0 = telf.L_n0
this.L_n1 = telf.L_n1
return this
}
/** DECREMENT SUBTRACTING VARIABLE METHOD -OBJECT MODIFY BY THIS REFERENCE */
//CODE: a = new Num('2.72'); a.Print(' '); a.Dec(); a.Print('\n') //2.72 1.72
Dec(sob = 1) {
let telf = this.Sub(sob)
this.d = telf.d
this.n = telf.n
this.n0 = telf.n0
this.n1 = telf.n1
this.n2 = telf.n2
this.L_n0 = telf.L_n0
this.L_n1 = telf.L_n1
return this
}
/** DECREMENT VARIABLE DIVIDING METHOD -OBJECT MODIFY BY this REFERENCE */
//CODE: a = new Num('272.0'); a.Print(' '); a.DecDiv(100); a.Print('\n') //272.0 2.72
DecDiv(sob = 10) {
let telf = this.Div(sob)
this.d = telf.d
this.n = telf.n
this.n0 = telf.n0
this.n1 = telf.n1
this.n2 = telf.n2
this.L_n0 = telf.L_n0
this.L_n1 = telf.L_n1
return this
}
/** (// %) CALCULATOR DIVMOD RETURN ARRAY (this // sob, this % sob) */
//CODE: a = new Num(10); qr = a.DivMod(4); Num.print(`${qr[0]} ${qr[1]}\n`) //2.0 2.0
DivMod(sob) {
const QR = [] //CREATE AN ARRAY
QR[0] = new Num(this).FloorDiv(new Num(sob))
QR[1] = new Num(this).Mod(new Num(sob))
return QR
}
/** (//) INTEGER DIVISION OPERATOR */
//CODE: a = new Num(15); Num.print(a.FloorDiv(6), '\n') //2.0
FloorDiv(sob) {
sob = new Num(sob)
if (this.n == "0.0") return new Num("0.0") //zero result modulus
let ze, x1, x2, x3
if (this.L_n1 > sob.L_n1) {
ze = this.L_n1 - sob.L_n1
x1 = Num.int(this.n2 + this.n0 + this.n1)
x2 = Num.int(sob.n2 + sob.n0 + sob.n1 + "".padStart(ze, "0"))
x3 = Num.divi(x1, x2, 0)
} else {
ze = sob.L_n1 - this.L_n1
var temp = this.n2 + this.n0 + this.n1 + "".padStart(ze, "0")
x1 = Num.int(temp)
x2 = Num.int(sob.n2 + sob.n0 + sob.n1)
x3 = Num.divi(x1, x2, 0)
}
return new Num(x3)
}
/** (%) MODULE OPERATOR (Num FLOATING POINT DIVISION REMAINDER) */
//CODE: a = new Num(15); Num.print(a.Mod(6), '\n') //3.0
Mod(sob) {
sob = new Num(sob)
if (this.n == "0.0") return new Num("0.0") //zero result modulus
let ze, x1, x2, x3
if (this.L_n1 > sob.L_n1) {
ze = this.L_n1 - sob.L_n1
x1 = Num.int(this.n2 + this.n0 + this.n1)
x2 = Num.int(sob.n2 + sob.n0 + sob.n1 + "".padStart(ze, "0"))
x3 = Num.divi(x1, x2, 0)
} else {
ze = sob.L_n1 - this.L_n1
x1 = Num.int(this.n2 + this.n0 + this.n1 + "".padStart(ze, "0"))
x2 = Num.int(sob.n2 + sob.n0 + sob.n1)
x3 = Num.divi(x1, x2, 0)
}
return this.Sub(new Num(x3).Mul(sob))
}
/** (+) OBJECT ADDITION METHOD */
//CODE: a = new Num('15.1'); Num.print(a.Add('6.2'), '\n') //21.3
Add(sob) {
sob = new Num(sob)
let x1
let x2
if (this.L_n1 < sob.L_n1) {
x1 = BigInt(this.n2 + this.n0 + this.n1 + "".padStart(sob.L_n1 - this.L_n1, "0"))
x2 = BigInt(sob.n2 + sob.n0 + sob.n1)
} else if (this.L_n1 > sob.L_n1) {
x1 = BigInt(this.n2 + this.n0 + this.n1)
x2 = BigInt(sob.n2 + sob.n0 + sob.n1 + "".padStart(this.L_n1 - sob.L_n1, "0"))
} else {
x1 = BigInt(this.n2 + this.n0 + this.n1)
x2 = BigInt(sob.n2 + sob.n0 + sob.n1)
}
let x3 = x1 + x2
if (!x3) return new Num("0.0") //ZERO RESULT SUM
let xt = String(x3)
let xt_L = xt.length
let xt_D = sob.L_n1 > this.L_n1 ? sob.L_n1 : this.L_n1
if (x3 < 0) { //NEGATIVE SUM
let ze = xt_D - xt_L + 1
if (ze >= 0) { //-1 < Negative Add < 0
let xtr = "-0" + "." + "".padStart(ze, "0") + xt.slice(1)
return new Num(xtr)
}
} else {
let ze = xt_D - xt_L
if (ze >= 0) { //0 < POSITIVE SUM < 1
let xtr = "0" + "." + "".padStart(ze, "0") + xt.slice("0")
return new Num(xtr)
}
}
return new Num(xt.slice(0, -xt_D) + "." + xt.slice(-xt_D))
}
//''' (-) OBJECT SUBTRACTION METHOD '''
//CODE: a = new Num('-15.1'); Num.print(a.Add('-6.2'), '\n') //-21.3
Sub(sob) {
sob = new Num(sob)
let x1
let x2
if (this.L_n1 < sob.L_n1) {
x1 = BigInt(this.n2 + this.n0 + this.n1 + "".padStart(sob.L_n1 - this.L_n1, "0"))
x2 = BigInt(sob.n2 + sob.n0 + sob.n1)
} else if (this.L_n1 > sob.L_n1) {
x1 = BigInt(this.n2 + this.n0 + this.n1)
x2 = BigInt(sob.n2 + sob.n0 + sob.n1 + "".padStart(this.L_n1 - sob.L_n1, "0"))
} else {
x1 = BigInt(this.n2 + this.n0 + this.n1)
x2 = BigInt(sob.n2 + sob.n0 + sob.n1)
}
let x3 = x1 - x2
if (!x3) return new Num("0.0") //ZERO RESULT DIF
let xt = String(x3)
let xt_L = xt.length
let xt_D = sob.L_n1 > this.L_n1 ? sob.L_n1 : this.L_n1
let ze
let xtr
if (x3 < 0) { //NEGATIVE DIF
ze = xt_D - xt_L + 1
if (ze >= 0) { //-1 < NEGATIVE DIF < 0
xtr = "-0" + "." + "".padStart(ze, "0") + xt.slice(1)
return new Num(xtr)
}
} else { //POSITIVE DIF
ze = xt_D - xt_L
if (ze >= 0) { //0 < POSITIVE DIF < 1
xtr = "0" + "." + "".padStart(ze, "0") + xt.slice(0)
return new Num(xtr)
}
}
return new Num(xt.slice(0, -xt_D) + "." + xt.slice(-xt_D))
}
//''' (*) OBJECT MULTIPLICATION METHOD '''
//CODE: a = new Num('-15.1'); Num.print(a.Mul('-6.2'), '\n') //93.62
Mul(sob) {
sob = new Num(sob)
let x1
let x2
x1 = BigInt(this.n2 + this.n0 + this.n1)
x2 = BigInt(sob.n2 + sob.n0 + sob.n1)
let x3 = x1 * x2
if (!x3) return new Num("0.0") //MULTIPLY BY 0
let xt = String(x3)
let xt_L = xt.length
let xt_D = this.L_n1 + sob.L_n1
let ze
if (x3 < 0) { //NEGATIVE
ze = xt_D - xt_L + 1
if (ze >= 0) return new Num("-0" + "." + "".padStart(ze, "0") + xt.slice(1)) //1.22*(-0.01)=-0.0122
return new Num(xt.slice(0, -xt_D) + "." + xt.slice(-xt_D)) //-1.22*3.0=-3.66
}
ze = xt_D - xt_L
if (ze >= 0) return new Num("0" + "." + "".padStart(ze, "0") + xt.slice(0)) //-1.22*(-0.01)=0.0122
return new Num(xt.slice(0, -xt_D) + "." + xt.slice(-xt_D)) //1.22*1.0=1.22
}
/** CALCULATOR MODE: MULTIPLY FOR TEN */
//CODE: a = new Num('3.2'); a = a._10x(); console.log(a.toString()) //32.0
_10x() { return this.Shift(1) }
/** CALCULATOR MODE: MULTIPLY FOR HUNDRED */
//CODE: a = new Num('3.2'); a = a._100x(); console.log(a.toString()) //320.0
_100x() { return this.Shift(2) }
/** CALCULATOR MODE: MULTIPLY FOR THOUSAND */
//CODE: a = new Num('3.2'); a = a._1000x(); console.log(a.toString()) //3200.0
_1000x() { return this.Shift(3) }
/** CALCULATOR MODE: DIVIDE FOR TEN */
//CODE: a = new Num('3.2'); a = a._10div(); console.log(a.toString()) //0.32
_10div() { return this.Shift(-1) }
/** CALCULATOR MODE: DIVIDE FOR HUNDRED */
//CODE: a = new Num('3.2'); a = a._100div(); console.log(a.toString()) //0.032
_100div() { return this.Shift(-2) }
/** CALCULATOR MODE: DIVIDE FOR THOUSAND */
//CODE: a = new Num('3.2'); a = a._1000div(); console.log(a.toString()) //0.0032
_1000div() { return this.Shift(-3) }
/** CALCULATOR MODE: DOUBLED VALUE */
//CODE: a = new Num ("123.0"); a._2x().Print("\n"); //246.0
_2x() { return this.Add(this) }
/** CALCULATOR MODE: TRIPLED VALUE */
//CODE: a = new Num ("123.0"); a._3x().Print("\n"); //369.0
_3x() { return this.Add(this).Add(this) }
/// CALCULATOR MODE: RETURN OBJECT MULTIPLIED OR DIVIDED FOR 10 POWER
// CODE: a = new Num("0.001"); a.Xe10("6.0").Print("\n"); //1000.0
// CODE: a = new Num("1000.0"); a.Xe10("-6.0").Print("\n"); //0.001
Xe10(p) { return this.Shift(p) }
///CALCULATOR MODE: EXPONENTIATION (POWER)
// CODE: a = new Num("4.0"); a.Xy("30.0").Print("\n"); //1152921504606846976.0
// CODE: a = new Num("-4.0"); a.Xy("-3.0").Print("\n"); //-0.015625
Xy(y) { return Num.pow(this, y) }
/** CALCULATOR MODE: BY THE RATE/ALL, RETURN THE PERCENTAGE VALUE OF this ALL/RATE */
// CODE: rate = new Num('22.0'); rate.Pct("1_648.98").Round().Print(" => PCT DISCOUNT\n"); //362.78 => PCT DISCOUNT
// CODE: all = new Num("1_648.98"); all.Pct(22).Round().Print(" => PCT DISCOUNT\n"); //362.78 => PCT DISCOUNT
Pct(RA = '1.0') { return Num.pct(RA, this) }
/** CALCULATOR MODE: BY THE ALL, RETURN THE RATE OF this PERCENTAGE */
// CODE: pct = new Num('362.78'); pct.Rate_all('1_648.98').Round().Print(" => rate\n"); //22.0 => rate
Rate_all(all) { return Num.rate(this, all) }
/** CALCULATOR MODE: BY THE PERCENTAGE, RETURN THE RATE OF this ALL */
// CODE: all = new Num("1_648.98"); all.Rate_pct('362.78').Round().Print(" => rate\n"); //22.0 => rate
Rate_pct(pct) { let PCT = new Num(pct); return PCT.Shift(2).Div(this) }
/** CALCULATOR MODE: BY THE PERCENTAGE RETURN THE ALL OF this RATE */
// CODE: rate = new Num('22.00025'); rate.All_pct('362.78').Round().Print(" => ALL\n"); //1648.98 => ALL
All_pct(PCT) { return Num.all(this, PCT) }
/** CALCULATOR MODE: BY THE RATE RETURN THE ALL OF this PCT */
// CODE: pct = new Num('362.78'); pct.All_rate('22.00025').Round().Print(" => ALL\n"); //1648.98 => ALL
All_rate(rate) { let RATE = new Num(rate); return this.Shift(2).Div(RATE) }
/** CALCULATOR MODE: BY THE RATE/ALL RETURN THE PERTHOUSAND OF this ALL/RATE */
// CODE: rate = new Num("2.0"); rate.Pth("10_000.0").Print("\n"); //20.0
// CODE: all = new Num("10_000.0"); all.Pth(2).Print("\n"); //20.0
Pth(RA = '1.0') { return Num.pth(RA, this) }
/** CALCULATOR MODE: BY THE ALL, RETURN THE RATE OF this PERTHOUSAND */
// CODE: pth = new Num(20); pth.RateTH_all(10000).Round().Print(" => rate_th\n"); //2.0 => rate_th
RateTH_all(all) { return Num.rate_th(this, all) }
/** CALCULATOR MODE: BY THE PERTHOUSAND, RETURN THE RATE OF this ALL */
// CODE: all = new Num(10000); all.RateTH_pth(20).Round().Print(" => rate_th\n"); //2.0 => rate_th
RateTH_pth(pth) { let PTH = new Num(pth); return PTH.Shift(3).Div(this) }
/** CALCULATOR MODE: BY THE PERTHOUSAND RETURN THE ALL OF this RATE */
// CODE: rate = new Num(2); rate.All_pth(20).Round().Print(" => ALL_th\n"); //10000.0 => ALL_th
All_pth(PTH) { return Num.all_th(this, PTH) }
/** CALCULATOR MODE: BY THE RATE RETURN THE ALL OF this PTH */
// CODE: pth = new Num(20); pth.All_rateTH(2).Round().Print(" => ALL_th\n"); //10000.0 => ALL_th
All_rateTH(rate_th) { let RATE_TH = new Num(rate_th); return this.Shift(3).Div(RATE_TH) }
/** (1/this) CALCULATOR NUMBER INVERSE METHOD */
// CODE: a = new Num(3); i = a.Inv(); i.Print('\n') //0.33333333333333333333333333333333333333333333333333333333333333333333333333333333
// CODE: a = new Num(3); i = a.Inv(6); i.Print('\n') //0.333333
Inv(precision = 80) { return Num.inv(this, precision) }
/** (/) OBJECT DIVISION METHOD */
// CODE: a = new Num('9.9'); Q = a.Div('3.3'); Q.Print('\n') //3.0
// CODE: a = new Num('9.9'); Q = a.Div('3.4', 7).Round(6); Q.Print('\n') //2.911765
Div(sob, d = 80) { //d => PRECISION DIGITS
sob = new Num(sob)
if (sob.toString() == "0.0") throw Error("Num.Div => ZeroDivisionError: " + sob)
if (this.n == "0.0") return new Num("0.0") //ZERO DIVIDEND MEANS ZERO QUOTIENT RESULT
let x1; let x2; let x3; let ze
let D = this.d > sob.d ? this.d : sob.d
if (this.L_n1 > sob.L_n1) {
ze = this.L_n1 - sob.L_n1
x1 = BigInt(this.n2 + this.n0 + this.n1)
x2 = BigInt(sob.n2 + sob.n0 + sob.n1 + "".padStart(ze, "0"))
x3 = Num.divi(x1, x2, d > D ? d : D)
} else {
ze = sob.L_n1 - this.L_n1
x1 = BigInt(this.n2 + this.n0 + this.n1 + "".padStart(ze, "0"))
x2 = BigInt(sob.n2 + sob.n0 + sob.n1)
x3 = Num.divi(x1, x2, d > D ? d : D)
}
return new Num(x3)
}
/** CALCULATOR SQUARE ROOT METHOD */
// CODE: a = new Num('2.0'); R = a.Sqrt(); R.Print('\n') //1.41421356237309504880168872420969807856967187537694807317667973799073247846210703
// CODE: a = new Num('2.0'); R = a.Sqrt(10); R.Round(9).Print('\n') //1.414213562
Sqrt(d = 80) { return new Num(Num.sqrt(this, d)) }
/** CALCULATOR ITH ROOT METHOD */
// CODE: a = new Num(27); RI = a.Root_i(3); RI.Print('\n') //3.0
// CODE: a = new Num('3.3'); RI = a.Root_i(3, 10).Round(9); RI.Print('\n') //1.488805553
Root_i(i = 3, d = 80) { return new Num(Num.root_i(this, i, d)) }
/** Num FLOATING POINT TRUNCATION */
// CODE: a = new Num('3.14159'); T = a.Trunc(4); T.Print('\n') //3.1415
Trunc(d = 0) {
let m = new Num(10).Pow(d), t = this.Mul(m)
return new Num(Num.int(t.n2 + t.n0)).Div(m)
}
/** Num FLOOR ROUNDING RELATIVE DOWN d=1: 0.12 => 0.1 -0.12 => -0.2 */
// CODE: a = new Num('3.14159'); T = a.Round_floor(4); T.Print('\n') //3.1415
Round_floor(d = 0) { //-> RELATIVE VALUE (REAL NUMBER R)
if (this.GE('0.0')) return this.Trunc(d) //POSITIVES AND ZERO
let e = new Num('1.0', d).Div(new Num('10.0').Pow(d)) //NEGATIVES
let t, t2
if(d >= 0) {
t = this.Trunc(d).Sub(e)
t2 = this.Sub(e)
return t.EQ(t2) ? this : t
}
if (e.LT(this)) return this
t = this.Trunc(d).Sub(e)
t2 = this.Sub(e)
return t.EQ(t2) ? this : t
}
/** Num HALF UP ROUNDING COMMON STANDARD -RELATIVE ROUND_HALF_CEIL d=1: 0.15 => 0.2 -0.15 => -0.1 */
// CODE: a = new Num('3.14159'); T = a.Round(4); T.Print('\n') //3.1416
Round(d = 2) {
let t = new Num('0.5').Mul(new Num(10).Pow(-d)).Add(this)
return t.Round_floor(d)
}
/** Num CEIL ROUNDING RELATIVE UP d=1: 0.12 => 0.2 -0.12 => -0.1 */
// CODE: a = new Num('3.14159'); T = a.Round_ceil(2); T.Print('\n') //3.15
Round_ceil(d = 0) {
if (this.LE(0)) return this.Trunc(d) //NEGATIVES AND ZERO
let e = new Num('1.0', d).Div(new Num('10.0').Pow(d)) //POSITIVES
let t, t2
if(d >= 0) {
t = this.Trunc(d).Add(e)
t2 = this.Add(e)
return t.EQ(t2) ? this : t
}
if (e.GT(this)) return this
t = this.Trunc(d).Add(e)
t2 = this.Add(e)
return t.EQ(t2) ? this : t
}
/** Num HALF EVEN ROUNDING */
// CODE: a = new Num('3.14159265'); T = a.Round_bank(7); T.Print('\n') //3.1415926
// CODE: a = new Num('3.1415'); T = a.Round_bank(3); T.Print('\n') //3.142
Round_bank(d = 2) {
if (d < 0) {
d = -d
let e = 10n**BigInt(d)
return ((this.Div(new Num(e))).Round_bank(0)).Mul(e) //RECURSION
}
let of = d - this.L_n1
if (of >= 0) return new Num(this.n) //NO ROUND
let a, b, c
if (!d) { //INTEGER ROUNDING (d=0)
a = Num.int(this.n0)
b = Num.int(this.n1.slice(0, 1))
c = Num.rstrip(this.n1.slice(1), '0')
if (b > 5n) {
a += 1n
return new Num(this.n2 + String(a) + '.0') //12.6 => 13.0 INTEGER
} else if(b == 5n) {
if (a % 2n) { //ODD
a += 1n
return new Num(this.n2 + String(a) + '.0') //13.5 => 14.0 INTEGER
}
else if (c != '') { //EVEN OVERFLOW
a += 1n
return new Num(this.n2 + String(a) + '.0') //12.51 => 13.0 INTEGER
}
else { //EVEN
if (!a) return new Num('0.0') //a == 0
return new Num(this.n2 + String(a) + '.0') //12.5 => 12.0 INTEGER -0.5 => 0.0
}
} else {
if(Num.int(this.n0) >= 1n) return new Num(this.n2 + String(a) + '.0') //12.3 => 12.0 INTEGER
return new Num('0.0') //#0.3 => 0.0 #Num(self.n2 + '0.0')
}
}
//FLOATING POINT ROUNDING (d>0)
a = Num.int(this.n1.slice(d-1, d))
b = Num.int(this.n1.slice(d, d+1))
c = Num.rstrip(this.n1.slice(d+1), '0')
let of2, s
if (b > 5n) {
a += 1n
of2 = 1
if (a > 9n) { //FLAG CARRY
while (a > 9) {
b = 0n
s = this.n1.slice(d-of2-1, d-of2)
if (!s) return new Num(this.n2 + String(Num.int(this.n0)+1n) + '.0') //3.99 => 4.0
a = Num.int(s)
a += 1n
of2 += 1
}
return new Num(this.n2 + this.n0 + '.' + this.n1.slice(0, d-of2) + String(a)) //3.095 => 3.1
}
return new Num(this.n2 + this.n0 + '.' + this.n1.slice(0, d-1) + String(a)) //3.1415 => 3.142
} else if(b == 5n) {
if (a % 2n) { //ODD
a += 1n
of2 = 1
while (a > 9n) {
b = 0
s = this.n1.slice(d-of2-1, d-of2)
if (!s) return new Num(this.n2 + String(Num.int(this.n0)+1n) + '.0') //3.95 => 4.0
a = Num.int(s) //3.0955 d=2 => 3.1
a += 1n
of2 += 1
}
return new Num(this.n2 + this.n0 + '.' + this.n1.slice(0, d-of2) + String(a)) //3.095 => 3.1
} else if(c != '') { //EVEN OVERFLOW
a += 1n
return new Num(this.n2 + this.n0 + '.' + this.n1.slice(0, d-1) + String(a)) //12.51 => 13.0 INTEGER
} else {
if (!Num.int(this.n0) && Num.int(this.n1) == 5) return new Num('0.0') // self.n0 == 0 and self.n1 == 5 (ex. -0.00000005) -ZERO SYMMETRIC MEETING
return new Num(this.n2 + this.n0 + '.' + this.n1.slice(0, d-1) + String(a)) //EVEN 5.65 => 5.6 -0.05 => 0.0
}
}
else {
try {
return new Num(this.n2 + this.n0 + '.' + this.n1.slice(0, d-1) + String(a)) //3.1415 => 3.14 #-0.02 => -0.0 ERROR!
} catch (e) { return new Num('0.0') } //-0.02 => 0.0 OK.
}
}
/** CONVERT A NUM OBJECT TO SCIENTIFIC NOTATION STRING */
// CODE: a = new Num('123.006789'); S = a.Num2exp(); Num.print(S + '\n') //1.23006789e2
Num2exp() { return Num.num2exp(this) }
/** RETURN AN ARRAY WITH NUM LENGTHS BEFORE AND AFTER FLOATING POINT DOT */
// CODE: a = new Num(Num.pi); a.Print('\n') ; Num.print(a.Len()[0] + ' ' + a.Len()[1] + '\n') //3.1415926535897932384626433832795 1 31
Len() { return [this.n0.length, this.n1.length == 1 && this.n1 == '0' ? 0 : this.n1.length] } //ARRAY
/** And BOOLEAN LOGIC OPERATOR */
// CODE: a = new Num('3.0'); b = new Num('4.0'); Num.print(a.And(b), '\n') //true
// CODE: a = new Num('3.0'); b = new Num('0.0'); Num.print(a.And(b), '\n') //false
// CODE: a = new Num('0.0'); b = new Num('4.0'); Num.print(a.And(b), '\n') //false
// CODE: a = new Num('0.0'); b = new Num('0.0'); Num.print(a.And(b), '\n') //false
And(b) { return this.NE(0) && b.NE(0) ? true : false }
/** Or BOOLEAN LOGIC OPERATOR */
// CODE: a = new Num('3.0'); b = new Num('4.0'); Num.print(a.Or(b), '\n') //true
// CODE: a = new Num('3.0'); b = new Num('0.0'); Num.print(a.Or(b), '\n') //true
// CODE: a = new Num('0.0'); b = new Num('4.0'); Num.print(a.Or(b), '\n') //true
// CODE: a = new Num('0.0'); b = new Num('0.0'); Num.print(a.Or(b), '\n') //false
Or(b) { return this.NE(0) || b.NE(0) ? true : false }
/** Andb BITWISE OPERATOR */
// CODE: a = new Num('255.0'); b = new Num('1.0'); Num.print(a.Andb(b), '\n') //1.0
Andb(sob) {
if(!this.Is_numint() || this.n2) throw Error("Num.Andb => TypeError only positive integer allowed: " + this.n)
sob = new Num(sob)
if(!sob.Is_numint() || sob.n2) throw Error("Num.Andb => TypeError only positive integer allowed: " + sob.n)
return new Num(BigInt(this.n0) & BigInt(sob.n0))
}
/** Orb BITWISE OPERATOR */
// CODE: a = new Num('0.0'); b = new Num('255.0'); Num.print(a.Orb(b), '\n') //255.0
Orb(sob) {
if(!this.Is_numint() || this.n2) throw Error("Num.Orb => TypeError only positive integer allowed: " + this.n)
sob = new Num(sob)
if(!sob.Is_numint() || sob.n2) throw Error("Num.Orb => TypeError only positive integer allowed: " + sob.n)
return new Num(BigInt(this.n0) | BigInt(sob.n0))
}
/** Xorb BITWISE OPERATOR */
// CODE: a = new Num('255.0'); b = new Num('255.0'); Num.print(a.Xorb(b), '\n') //0.0
Xorb(sob) {
if(!this.Is_numint() || this.n2) throw Error("Num.Xorb => TypeError only positive integer allowed: " + this.n)
sob = new Num(sob)
if(!sob.Is_numint() || sob.n2) throw Error("Num.Xorb => TypeError only positive integer allowed: " + sob.n)
return new Num(BigInt(this.n0) ^ BigInt(sob.n0))
}
/** In BOOLEAN ARRAY OPERATOR */
// CODE: A = [ new Num(3), new Num(-6), new Num(0), new Num('9.7'), new Num('6.1')]; Num.print(new Num('9.7').In(A), '\n') //true
In(arr) { return Num.in(arr, this) }
/** not in BOOLEAN ARRAY OPERATOR */
// CODE: A = [ new Num(3), new Num(-6), new Num(0), new Num('9.7'), new Num('6.1')]; Num.print(new Num('9.69').Not_In(A), '\n') //true
Not_In(arr) { return Num.not_in(arr, this) }
/** Is BOOLEAN OPERATOR (TWO VARIABLES WITH A SAME ADDRESS => ONE OBJECT) */
// CODE: a = new Num('3.14'); b = new Num('3.14'); console.log(a.Is(b)); //false
Is(b) { return Num.is(this, b) }
/** Is_not BOOLEAN OPERATOR (TWO VARIABLES WITH A SAME ADDRESS => ONE OBJECT) */
// CODE: a = new Num('3.14'); b = a; console.log(a.Is_not(b)); //false
Is_not(b) { return Num.is_not(this, b) }
/** F_price_over ADD OR SUB A PERCENTAGE VALUE TO this PRICE */
// CODE: price = new Num(1000); overPrice = price.F_price_over(22); overPrice.Print('\n') //1220.0
// CODE: price = new Num(1000); subPrice = price.F_price_over(-22); subPrice.Print('\n') //780.0
F_price_over(t = 22) {
t = new Num(t) //ALLOWING FLOAT STRING AS: '2.3'
let THIS = new Num(this.Mul(t).Shift(-2).Add(this))
return THIS
}
/** F_price_spinoff SPIN OFF PERCENTAGE TAX VALUE FROM A this PRICE */
// CODE: priceFinal = new Num(100); priceRaw = priceFinal.F_price_spinoff(22).Round(2); priceRaw.Print('\n') //81.97
F_price_spinoff(t = 22) {
t = new Num(t) //ALLOWING FLOAT STRING AS: '2.3'
let THIS = new Num(this.Div((t.Add(100).Shift(-2))))
return THIS
}
/** F_perf PERCENTAGE PERFORMANCE VALUE (DIRECT RATIO) */
// CODE: a = new Num(50); b = new Num(75); a.F_perf(b).Print('\n'); //50.0
F_perf(sob) {
sob = new Num(sob) //ALLOWING FLOAT STRING AS: '2.3'
return (sob.Sub(this)).Div(this).Shift(2)
}
/** F_perf_time PERCENTAGE AND RELATIVE MAGNITUDE ORDER TIME PERFORMANCE VALUE (INVERSE RATIO) */
// RETURN ARRAY BY TWO ELEMENTS
// CODE: a = new Num(50); b = new Num('37.5'); A = a.F_perf_time(b); A[0].Round().Print('\n'); A[1].Round(2).Print('\n'); //33.33 0.33
F_perf_time(sob) {
sob = new Num(sob) //ALLOWING FLOAT STRING AS: '2.3'
let THIS= new Num(this)
let R = ((THIS.Sub(sob)).Div(sob).Mul(100))
if(sob.GT(THIS)) return [R, sob.Invsign().Div(THIS).Add(1)]
else return [R, THIS.Div(sob).Sub(1)] //Array
}
/** INVERTED SIGN OF this Num */
// CODE: a = new Num('+3.14'); a.Print('\n'); a.Invsign(); a.Print('\n') //3.14 -3.14
Invsign() {
this.n2 = (this.n2 == '' ? '-' : '')
this.n = this.n2 + this.n0 + '.' + this.n1
return this
}
/** INVERTED SIGN OF this Num */
// CODE: a = new Num('+3.14'); a.Print('\n'); a.Minus_unary(); a.Print('\n') //3.14 -3.14
Minus_unary() { return this.Invsign() }
/** PLUS SIGN OF this Num */
// CODE: a = new Num('-3.14'); a.Print('\n'); a.Plus(); a.Print('\n') //-3.14 3.14
Plus() {
if(this.n2 == '') return this
this.n2 = '' //SET PLUS SIGN (+)
this.n = this.n2 + this.n0 + "." + this.n1
return this
}
/** MINUS SIGN OF Num */
// CODE: a = new Num('+3.14'); a.Print('\n'); a.Minus(); a.Print('\n') //3.14 -3.14
Minus() {
if(this.n0 + this.n1 == '00') throw Error("Num.Minus => zero can not be signed: " + this.n); //SIGNED ZERO ERROR
if(this.n2 == '-') return this
this.n2 = '-' //SET MINUS SIGN (-)
this.n = this.n2 + this.n0 + "." + this.n1
return this
}
/** INTEGER Num TO BigInt */
// CODE: a = new Num('1.0e500'); Num.print(a.ToBigInt() - 1n, '\n') //99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
ToBigInt() {
if(this.n1 != '0') throw Error("Num.toBigInt => TypeError number must be integer: " + this.n)
return BigInt(this.n2 + this.n0)
}
/** INTEGER Num TO BigInt */
// CODE: a = new Num('1.0e500'); Num.print(a.toInt() - 1n, '\n') //99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
toInt() { return Num.int(this) }
/** Num TO float */
// CODE: a = new Num('0.1'); Num.print(a.toFloat() + 0.2, '\n') //0.30000000000000004
toFloat() { return Number.parseFloat(this.Num2exp()) }
/** SHIFT Num (MULTIPLY AND DIVIDE BY TEN) */
// CODE: a = new Num(1); a = a.Shift(3); a.Print('\n') //1000.0
// CODE: a = new Num(1); a = a.Shift(-3); a.Print('\n') //0.001
Shift(zeros) { return Num.shift(this, zeros) }
/** PRECISION SET Num d PROPERTY (DIVISION OPERATION) */
// CODE: digits = 6; const a = new Num('3.14'), b = new Num(3); a.ToPrecision(digits); b.ToPrecision(digits); console.log(Num.div(a, b, digits).toString(), '\n'); //1.046666
ToPrecision(d) { this.d = d }
/** PRINT (VIDEO OUTPUT) */
// CODE: const a = new Num('-5005.77'); a.Print(' => ', 'VALUE\n') //-5005.77 => VALUE
Print(txt = '', txt0 = '', txt1 = '') { Num.print(this.n + txt + txt0 + txt1) }
/** CALCULATOR MODE: FACTORIAL COMPUTATION */
//CODE: const a = new Num('5.0'); console.log(a.Fact().toString()) //120
Fact() {
if(this.Is_numint()) return Num.fact(BigInt(this.n0))
throw Error("Num.Fact => TypeError number must be integer: " + this.n)
}
/** CALCULATOR MODE: SQUARE POWER */
// CODE: const a = new Num("3.1415"); a.x2().Print("\n"); //9.86902225
x2() { return Num.x2(this) }
/** CALCULATOR MODE: CUBE POWER */
// CODE: const a = new Num("123.456"); a.x3().Print("\n"); //1881640.295202816
x3() { return Num.x3(this) }
/** COPY Num OBJECT */
//CODE: a = new Num(3); b = a.Copy(); if(a == b) Num.print("UNIQUE OBJECT\n"); else Num.print("DOUBLED OBJECT\n") //DOUBLED OBJECT
Copy() { return new Num(this) }
//STATIC MEMBERS => STATIC METHODS AND STATIC VARIABLES *****************************************************************
//''' class VARIABLES LIST '''
static pi = "3.1415926535897932384626433832795"
static e = "2.7182818284590452353602874713527"
/** CONVERT A SCIENTIFIC NOTATION STRING NUMBER TO STRING NUMERIC */
// CODE: s = Num.exp2num('123.0e3'); console.log(s) //123000.0
static exp2num(s) {
if (typeof s != "string") throw Error("Num.exp2num => object data not valid: " + s)
let S = s
s = s.trim().replace(/_+/g, "").toUpperCase() //REMOVE SPACE AND UNDERSCORE (1_000 => 1000)
let be = s.split("E")
if (be[0].length == 0 || be[1].length == 0 || be.length == 1 || be.length > 2) throw Error("Num.exp2num => scientific notation not valid: " + S)
let be0 = be[0]; let be1 = be[1]; let POSE
if (be1[0] == "+") { POSE = true; be1 = be1.slice(1) } //CHECK EXPONENT SIGN => be1
else if (be1[0] == "-") { POSE = false; be1 = be1.slice(1) } else POSE = null
if (!Num.isDigit(be1)) throw Error("Num.exp2num => scientific notation not valid: " + S) //EXPONENT MUST BE ONLY DIGITS
be1 = Num.lstrip(be1, "0")
if (be1 == "") { //REBUILD RIGHT ENDING ZERO
if (POSE == true || POSE == false) throw Error("Num.exp2num => zero can not be signed: " + S) //SIGNED ZERO ERROR
be1 = "0"
}
let POS //CHECK BASE SIGN
if (be0[0] == "+") { POS = true; be0 = be0.slice(1) }
else if (be0[0] == "-") { POS = false; be0 = be0.slice(1) }
else POS = null
be0 = Num.lstrip(be0, "0") //CLEAR LEFT ZEROS
if (be0[0] == ".") be0 = "0" + "." + be0.slice(1) //REBUILD LEFT STARTING ZERO
let bf = be0.split(".")
if (bf.length == 1 || bf.length > 2) throw Error("Num.exp2num => scientific notation not valid (postfix .0 for base integer like 7.0e7): " + S)
let bf0 = bf[0]; let bf1 = bf[1]
if (!Num.isDigit(bf0) || !Num.isDigit(bf1)) throw Error("Num.exp2num => scientific notation not valid, typing error: " + S) //BASE MUST BE ONLY DIGITS
bf1 = Num.rstrip(bf1, "0") //CLEAR RIGHT ZEROS
if (bf1 == "") bf1 = "0" //REBUILD RIGHT ENDING ZERO
if (bf0 == "0" && bf1 == "0") { //ZERO BASE
if (POS == true || POS == false) throw Error("Num.exp2num => zero can not be signed: " + S) //SIGNED ZERO ERROR
return "0.0"
}
let EXP = parseInt((POSE == false ? "-" : "+") + be1)
POS = (POS == false ? "-" : "")
let L_bf0 = bf0.length //INTEGER BASE LENGTH
let L_bf1 = bf1.length //DECS BASE LENGTH
let r, DOT, s_CHECK
if (EXP >= 0) {
//POSITIVE INTEGER BASE AND EXPONENT >= 0
if (POS == "" && bf1 == "0") return bf0 + "".padStart(EXP, "0") + ".0" //2.0e3 => 2000.0
r = bf0 + bf1 + "".padStart(EXP - L_bf1, "0") //NEGATIVE INTEGER AND FLOATING POINT BASE
DOT = (EXP - L_bf1 < 0 ? 1 : 0)
s_CHECK = r.slice(0, L_bf0 + EXP) + "".padStart(DOT, ".") + r.slice(L_bf0 + EXP)
s_CHECK = Num.lstrip(s_CHECK, "0")
if (Num.IN(s_CHECK, "\\.")) { //if '.' in s_CHECK: /\./.test(s_CHECK)
if (s_CHECK[0] == ".") return POS + "0" + s_CHECK //0.0105e1 => 0.105
return POS + Num.lstrip(s_CHECK, "0") //-000.105e2 => -10.5 #000.10500e0 => 0.105
}
return POS + (s_CHECK + ".0") //+000.105e3 => 105.0 .0105000e4 => 105.0
}
//NEGATIVE EXPONENT (EXP < 0)
if (L_bf0 == 1) { //0.1e-1 => 0.01
r = "".padStart(-EXP, "0") + bf0 + bf1
return POS + "0." + Num.rstrip(r.slice(1), "0") //-0.2e-1 => -0.02
}
DOT = L_bf0 + EXP
if (DOT <= 0) return Num.rstrip(POS + "0." + "".padStart(-DOT, "0") + bf0 + bf1, "0") //-102.01e-3 => -0.10201
r = Num.rstrip(POS + bf0.slice(0, EXP) + "." + bf0.slice(EXP) + bf1, "0") //-102.01e-2 => -1.0201
return r[r.length - 1] != "." ? Num.rstrip(r, "0") : r + "0"; //3_000.0e-2 => 30.0
}
/** CHECK FOR ONLY DIGITS IN A STRING */
// HOW: true = '123' false = '123.0' '123a'
// CODE: s = '123'; console.log(Num.isDigit(s)) //true
static isDigit(str) { return /^\d+$/.test(str) }
/** CLEAR LEFT CHARACTER STRING */
// CODE: s = '00001230'; sc = Num.lstrip(s); Num.print(sc, '\n') //1230
static lstrip(str, ch = "0") { return str.replace(new RegExp("^" + ch + "+"), "") }
/** CLEAR RIGHT CHARACTER STRING */
// CODE: s = '1230.20030000'; sc = Num.rstrip(s); Num.print(sc, '\n') //1230.2003
static rstrip(str, ch = "0") { return str.replace(new RegExp(ch + "+$"), "") }
/** BOOLEAN IN CHECKS CHARACTER IN A STRING */
// CODE: Num.print(Num.IN('123.4', '.'), '\n') //true
static IN(s, ch) { return new RegExp(ch).test(s) ? true : false }
/** IT CHECKS TYPES number bigint onlyDigitString (PYTHON PORTABILITY) */
// true = 123 123.0 123n '123' false = '123.4' '123.0'
// CODE: Num.print(Num.isInt(123), '\n') //true
static isInt(n) {
if (typeof n == "number") {
if (Number.isInteger(n)) return true
return false
}
if (typeof n == "bigint") return true
if(Num.isDigit(n)) return true
return false
}
/** (PYTHON PORTABILITY) CONVERT A NUMERIC STRING TO BigInt BY TRUNCATION: OK => '0' '1' '2' ..., Error => '0.0' '1.0' '2.0' ... */
// CODE: Num.print(Num.int('123'), '\n') //123
static int(n) {
if(typeof(n) == 'number') {
let N = n.toString().toUpperCase().split('E')
let s
if(N[0] == '-INFINITY' || N[0] == 'INFINITY') throw Error("Num.int => ValueError: NOT A NUMBER (nan): " + n)
if(N[1]) { //EXPONENTIAL (ONLY INTEGER RESULTS)
let SIGN = (N[0][0] == '-' ? '-' : '')
if(Num.isDigit(N[0]) || SIGN && Num.isDigit(N[0].slice(1))) { //1e+21 => 1000000000000000000000
s = new Num(N[0] + '.0' + 'e' + N[1]) //-1e+21 => -1000000000000000000000
return BigInt(s.n2 + s.n0)
}
s = new Num(N[0] + 'e' + N[1]) //+-1.234567e+21 => +-1234567000000000000000
return BigInt(s.n2 + s.n0)
}
return BigInt(Number.parseInt(n)) //TRUNCATION -1.7 => -1n -2.3456e-1 => 0
}
if(typeof(n) == 'bigint') return n //BigInt
if(n.type == 'Num') return BigInt(n.n2 + n.n0) //Num TRUNCATION by BigInt
if(Num.isDigit(n)) return BigInt(n) //POSITIVE INTEGER STRING (ONLY DIGITS)
if(n[0] == '-' && Num.isDigit(n.slice(1))) return BigInt(n) //NEGATIVE INTEGER STRING
throw Error("Num.int => ValueError: invalid literal for int() with base 10: " + n)
}
/** DIVISION BETWEEN SIGNED INTEGER NUMBER