-
Notifications
You must be signed in to change notification settings - Fork 10
/
number.js
1559 lines (1378 loc) · 51.4 KB
/
number.js
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
/*
Ethereal Farm
Copyright (C) 2020-2024 Lode Vandevenne
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Defines the number type, Num, for this incremental game: floating point with much bigger possible exponent.
/**
* NOTE: while this supports very large numbers, there's a limit: max exponent is 9007199254740992, min exponent is -9007199254740992. But there is already visible precision loss here, so to be safe only use a factor 100 of that less as exponent.
* performance notes:
* -putting "new" in front is slightly faster than without
* -giving a separate b and e is slower (because it then normalizes), than initializing to 0 or initializing with an existing Num.
* @param {Num=|number=} b Num object to clone, or real number base. can be any float value, but typically scaled to 1..2 or -1..-2. Special values: 0: value is 0 no matter what exponent. Infinity/-Infinity/NaN: entire number is infinity/-infinity/nan, no matter what exponent
* @param {number=} e exponent in binary, not decimal! always integer, max 2**53, min -2**53. Should never be set to non-integer, NaN, Infinity, ..., only base (b) may do that
* @constructor
*/
function Num(b, e) {
// Allow calling it without new, but act like new
if(!(this instanceof Num)) {
return new Num(b, e);
}
if(b instanceof(Num)) {
this.b = b.b;
this.e = b.e;
// no scaleInPlace: assume the input Num already was correct, as it should
} else if(b == undefined || (b == 0 && e == undefined)) {
// value = b * 2**e
this.b = 0;
this.e = 0;
} else {
this.b = b;
this.e = (e == undefined) ? 0 : e;
Num.scaleInPlace(this);
}
}
// constructor that takes decimal exponent, rather than binary like function Num does
// this function loses precision for very large exponents, do not give exponetns like 9007199254740992 to this one since double precision float loses too much precision for high values
// e is optional, if not given it's assumed to be 0
// e.g. Num.makeDecimal(3, 5) will return a number representing 3*10^5 = 300000
Num.makeDecimal = function(b, e) {
if(e == undefined) e = 0;
var e2 = e * log2_10;
var ef = Math.floor(e2);
var f = Math.pow(2, e2 - ef);
b *= f;
return new Num(b, ef);
};
Num.prototype.clone = function() {
return new Num(this);
};
// reset to 0 without allocating new Num object
Num.prototype.reset = function() {
this.b = 0;
this.e = 0;
};
// Ensures that the exponent e is an integer within range -9007199254740992..9007199254740992,
// since that's the assumptions some functionalty makes
Num.prototype.ensureIntegerExponent_ = function() {
if(this.e == 0) return; // optimization for this common case
if(isNaN(this.e)) {
this.b = NaN;
this.e = 0;
return;
}
if(this.e == Infinity) {
if(this.b == 0) {
this.b = NaN;
this.e = 0;
return;
} else {
this.b = this.b < 0 ? -Infinity : Infinity;
this.e = 0;
return;
}
}
// at this point, the exponent can no longer distinguish successive integers, so no longer supported.
if(this.e > 9007199254740992) {
if(!isNaN(this.b)) this.b = (this.b == 0) ? 0 : (this.b < 0 ? -Infinity : Infinity);
this.e = 0;
return;
}
if(this.e < -9007199254740992) {
if(!isNaN(this.b)) this.b = 0;
this.e = 0;
return;
}
//if(this.e != Math.floor(this.e) && !isNaN(this.e)) {
if(!Number.isInteger(this.e)) {
// e should be integer. E.g. the rpow function can cause this situation
var e2 = Math.floor(this.e);
this.b *= Math.pow(2, this.e - e2);
this.e = e2;
}
}
// scales exponent of this to become e
// NOTE: for best precision, e should be larger than this.e, that is, scale to the larger exponent. The opposite direction may cause infinity.
// NOTE: even more care needed, e.g. Num(5, -2000).add(Num(0, 0)) will fail if scaleToInPlace is used naively. So too eqr(0).
Num.prototype.scaleToInPlace = function(e) {
if(this.b == 0) {
this.e = e;
return this;
}
this.ensureIntegerExponent_();
var d = this.e - e;
var t = 1 / 256.0;
var u = 1 / 4294967296.0;
if(d > 1023) {
// value cannot be represented scaled to e, it's infinity compared to e
this.e = e;
this.b = Infinity;
return this;
}
if(d < -1074) {
// value cannot be represented scaled to e, it's 0 compared to e
// NOTE: -1074, not -1023, due to denormalized floats
this.e = e;
this.b = 0;
return this;
}
while(d >= 32) {
d -= 32;
this.e -= 32;
this.b *= 4294967296;
}
while(d >= 8) {
d -= 8;
this.e -= 8;
this.b *= 256;
}
while(d >= 1) {
d -= 1;
this.e -= 1;
this.b *= 2;
}
while(d <= -32) {
d += 32;
this.e += 32;
this.b *= u;
}
while(d <= -8) {
d += 8;
this.e += 8;
this.b *= t;
}
while(d <= -1) {
d += 1;
this.e += 1;
this.b *= 0.5;
}
return this;
};
// scales exponent of this to become e
Num.prototype.scaleTo = function(e) {
var res = new Num(this);
res.scaleToInPlace(e);
return res;
};
Num.scaleTo = function(a, e) { return a.scaleTo(e); };
// sales the exponent in a good range for this number's value
Num.scaleInPlace = function(v) {
// optimization, do this before v.ensureIntegerExponent_, for the common cases of value 0 or 1
if(v.b == 0 && v.e <= 9007199254740992 && v.e >= -9007199254740992) {
v.e = 0;
return v;
}
if(v.b == 1 && v.e == 0) {
return v;
}
v.ensureIntegerExponent_();
if(v.b == 0 || v.b == Infinity || v.b == -Infinity || isNaN(v.b)) {
v.e = 0;
return v;
}
var neg = v.b < 0;
if(neg) v.b = -v.b;
var t = 1 / 256.0;
var u = 1 / 4294967296.0;
while(v.b <= u) {
v.b *= 4294967296;
v.e -= 32;
}
while(v.b <= t) {
v.b *= 256;
v.e -= 8;
}
while(v.b <= 0.5) {
v.b *= 2;
v.e--;
}
while(v.b >= 4294967296) {
v.b *= u;
v.e += 32;
}
while(v.b >= 256) {
v.b *= t;
v.e += 8;
}
while(v.b >= 2) {
v.b *= 0.5;
v.e++;
}
if(neg) v.b = -v.b;
return v;
};
Num.prototype.scaleInPlace = function() {
return Num.scaleInPlace(this);
};
/*
// commented out: not necessary: normally numbers keep themselves scaled, and only scaleInPlace is needed when manipulating b and e directly
Num.prototype.scale = function() {
var res = new Num(this);
res.scaleInPlace();
return res;
};
Num.scale = function(a) { return a.scale(); };
*/
Num.prototype.addInPlace = function(b) {
if(b.eqr(0)) return this; // avoid scaling in place to 0
if(this.e > b.e) b = b.scaleTo(this.e); else this.scaleToInPlace(b.e);
this.b += b.b;
this.scaleInPlace();
return this;
};
Num.prototype.add = function(b) {
var res = new Num(this);
res.addInPlace(b);
return res;
};
Num.add = function(a, b) { return a.add(b); };
Num.prototype.addrInPlace = function(r) {
return this.addInPlace(new Num(r));
};
Num.prototype.addr = function(r) {
var res = new Num(this);
res.addrInPlace(r);
return res;
};
Num.add = function(a, r) { return a.add(r); };
//in place!
Num.prototype.inc = function() {
this.addrInPlace(1);
};
//in place!
Num.prototype.dec = function() {
this.subrInPlace(1);
};
Num.prototype.subInPlace = function(b) {
if(b.eqr(0)) return this; // avoid scaling in place to 0
if(this.e > b.e) b = b.scaleTo(this.e); else this.scaleToInPlace(b.e);
this.b -= b.b;
this.scaleInPlace();
return this;
};
Num.prototype.sub = function(b) {
var res = new Num(this);
res.subInPlace(b);
return res;
};
Num.sub = function(a, b) { return a.sub(b); };
Num.prototype.subrInPlace = function(r) {
return this.subInPlace(new Num(r));
};
Num.prototype.subr = function(r) {
var res = new Num(this);
return res.subrInPlace(r);
};
Num.subr = function(a, r) { return a.subr(r); };
// rsub = inverted order (the first argument, Num, subtracted from the regular Number r)
// sets this to r - this
Num.prototype.rsubInPlace = function(r) {
this.b = -this.b;
return this.addrInPlace(r);
};
Num.prototype.rsub = function(r) {
var res = new Num(this);
return res.rsubInPlace(r);
};
Num.rsub = function(a, r) { return a.rsub(r); };
Num.prototype.negInPlace = function() {
this.b = -this.b;
};
Num.prototype.neg = function() {
var res = new Num(this);
res.negInPlace();
return res;
};
Num.neg = function(a) { return a.neg(); };
Num.prototype.mulInPlace = function(b) {
this.b *= b.b;
this.e += b.e;
this.scaleInPlace();
};
Num.prototype.mul = function(b) {
var res = new Num(this);
res.mulInPlace(b);
return res;
};
Num.mul = function(a, b) { return a.mul(b); };
Num.prototype.mulrInPlace = function(r) {
this.b *= r;
this.scaleInPlace();
return this;
};
Num.prototype.mulr = function(r) {
var res = new Num(this);
res.mulrInPlace(r);
return res;
};
Num.mul = function(a, r) { return a.mul(r); };
// posmul exists to have the same set of function names as Res (the resources
// object) has: this multiplies the current value with b only if the current
// value is positive. If the current value is negative, it's left unchanged.
Num.prototype.posmulInPlace = function(b) {
if(this.ltr(0)) return this;
this.b *= b.b;
this.e += b.e;
this.scaleInPlace();
return this;
};
Num.prototype.posmul = function(b) {
var res = new Num(this);
res.posmulInPlace(b);
return res;
};
Num.posmul = function(a, b) { return a.posmul(b); };
Num.prototype.posmulrInPlace = function(r) {
if(this.ltr(0)) return this;
this.b *= r;
this.scaleInPlace();
return this;
};
Num.prototype.posmulr = function(r) {
var res = new Num(this);
res.posmulrInPlace(r);
return res;
};
Num.posmul = function(a, r) { return a.posmul(r); };
Num.prototype.divInPlace = function(b) {
this.b /= b.b;
this.e -= b.e;
this.scaleInPlace();
return this;
};
Num.prototype.div = function(b) {
var res = new Num(this);
res.divInPlace(b);
return res;
};
Num.div = function(a, b) { return a.div(b); };
Num.prototype.inv = function() {
var res = new Num(1);
res.divInPlace(this);
return res;
};
Num.prototype.divrInPlace = function(r) {
this.b /= r;
this.scaleInPlace();
return this;
};
Num.prototype.divr = function(r) {
var res = new Num(this);
res.divrInPlace(r);
return res;
};
Num.divr = function(a, r) { return a.divr(r); };
// rdiv: returns r / this, where r is a regular JS number
Num.prototype.rdivInPlace = function(r) {
var res = Num(r);
res.divInPlace(this);
this.b = res.b;
this.e = res.e;
return this;
};
Num.prototype.rdiv = function(r) {
var res = new Num(this);
res.rdivInPlace(r);
return res;
};
Num.rdiv = function(a, r) { return a.rdiv(r); };
Num.prototype.absInPlace = function() {
if(this.b < 0) this.b = -this.b;
return this;
};
Num.prototype.abs = function() {
var res = new Num(this);
res.absInPlace();
return res;
};
Num.abs = function(a) { return a.abs(); };
Num.prototype.sqrtInPlace = function() {
if(this.b < 0) {
this.b = this.e = NaN;
return this;
}
var e2 = this.e / 2;
var e = Math.trunc(e2);
var f = Math.pow(2, e2 - e);
var b = Math.sqrt(this.b) * f;
this.b = b;
this.e = e;
this.scaleInPlace();
return this;
};
Num.prototype.sqrt = function() {
var res = new Num(this);
res.sqrtInPlace();
return res;
};
Num.sqrt = function(a) { return a.sqrt(); };
// power to a regular JS number, r may not be of type Num, but standard JS Number
Num.prototype.powrInPlace = function(r) {
if(this.b < 0 && r != Math.floor(r)) {
this.b = this.e = NaN;
return this;
}
return this.powInPlace(new Num(r));
};
Num.prototype.powr = function(r) {
var res = new Num(this);
res.powrInPlace(r);
return res;
};
Num.powr = function(a, r) { return a.powr(r); };
Num.rpowr = function(a, b) { return Num(a).powr(b); };
// r ** this (aka r ^ this); r is regular JS number.
// Negative r is not supported
// NOTE: since r is a regular JS number, r only supports only up to JS float precision
// NOTE: the exponent itself of a Num still only has JS float precision, and using powers like this can easily bring you beyond that, so this function easily reaches the limits of Num.
Num.prototype.rpowInPlace = function(r) {
if(r == 0) {
if(this.gtr(0)) return new Num(0);
if(this.ltr(0)) return new Num(Infinity);
return new Num(1); // 0**0
}
this.scaleToInPlace(0);
this.e = this.b;
this.b = 1;
if(r != 2) {
var e = this.e * (Math.log(r) / ln_2);
var e2 = Math.floor(e);
this.e = e2;
var f = Math.pow(2, e - e2);
this.b *= f;
}
this.scaleInPlace();
return this;
};
Num.prototype.rpow = function(r) {
var res = new Num(this);
res.rpowInPlace(r);
return res;
};
Num.rpow = function(r, a) { return a.rpow(r); };
// Negative this not supported
// NOTE: Number can have big values but not unlimited, pow can very easily reach the limit. Use this function carefully.
Num.prototype.powInPlace = function(b) {
if(this.eqr(0)) {
if(b.gtr(0)) return new Num(0);
if(b.ltr(0)) return new Num(Infinity);
return new Num(1); // 0**0
}
var r = Num.rlog(this);
var e = Num.exp(b.mulr(r));
this.b = e.b;
this.e = e.e;
return this;
};
Num.prototype.pow = function(b) {
var result = new Num(this);
result.powInPlace(b);
return result;
};
Num.pow = function(a, b) {
return a.pow(b);
};
Num.prototype.expInPlace = function() {
this.rpowInPlace(Math.E);
return this;
};
Num.prototype.exp = function() {
var res = new Num(this);
res.expInPlace();
return res;
};
Num.exp = function(a) { return a.exp(); }
// returned as regular JS number
Num.prototype.rlog2 = function() {
var r = this.e;
r += Math.log2(this.b);
return r;
};
Num.rlog2 = function(a) { return a.rlog2(); }
Num.prototype.log2 = function() { return new Num(this.rlog2()); }
Num.log2 = function(a) { return new Num(a.rlog2()); }
// returned as regular JS number
Num.prototype.rlog10 = function() {
var r = this.e * log10_2;
r += Math.log10(this.b);
return r;
};
Num.rlog10 = function(a) { return a.rlog10(); }
Num.prototype.log10 = function() { return new Num(this.rlog10()); }
Num.log10 = function(a) { return new Num(a.rlog10()); }
// natural logarithm (ln), returned as regular JS number
Num.prototype.rlog = function() {
var r = this.e * ln_2;
r += Math.log(this.b);
return r;
};
Num.rlog = function(a) { return a.rlog(); }
Num.prototype.log = function() { return new Num(this.rlog()); }
Num.log = function(a) { return new Num(a.rlog()); }
// log with base r, returned as regular JS number and with r regular JS number
Num.prototype.rlogr = function(r) {
var lr = 1 / Math.log(r);
var res = this.e * ln_2 * lr;
res += Math.log(this.b) * lr;
return res;
};
Num.rlogr = function(a, r) { return a.rlogr(r); }
Num.prototype.gt = function(b) {
if(b.eqr(0)) return this.b > 0 && !isNaN(this.e); // avoid scaling in place to 0
if(this.eqr(0)) return b.b <= 0 && !isNaN(b.e); // avoid scaling in place to 0
return this.b > b.scaleTo(this.e).b;
};
Num.gt = function(a, b) { return a.gt(b); };
Num.prototype.ge = function(b) {
if(b.eqr(0)) return this.b >= 0 && !isNaN(this.e); // avoid scaling in place to 0
if(this.eqr(0)) return b.b < 0 && !isNaN(b.e); // avoid scaling in place to 0
return this.b >= b.scaleTo(this.e).b;
};
Num.ge = function(a, b) { return a.ge(b); };
Num.prototype.eq = function(b) {
if(b.eqr(0)) return this.eqr(0); // avoid scaling in place to 0
return this.b == b.scaleTo(this.e).b;
};
Num.eq = function(a, b) { return a.eq(b); };
Num.prototype.lt = function(b) {
if(b.eqr(0)) return this.b < 0 && !isNaN(this.e); // avoid scaling in place to 0
if(this.eqr(0)) return b.b >= 0 && !isNaN(b.e); // avoid scaling in place to 0
return this.b < b.scaleTo(this.e).b;
}
Num.lt = function(a, b) { return a.lt(b); };
Num.prototype.le = function(b) {
if(b.eqr(0)) return this.b <= 0 && !isNaN(this.e); // avoid scaling in place to 0
if(this.eqr(0)) return b.b > 0 && !isNaN(b.e); // avoid scaling in place to 0
return this.b <= b.scaleTo(this.e).b;
};
Num.le = function(a, b) { return a.le(b); };
Num.prototype.neq = function(b) { return !this.eq(b); };
Num.neq = function(a, b) { return a.neq(b); };
// equals regular JS number
Num.prototype.eqr = function(r) {
if(r == 0) return this.b == 0; // avoid expensive scaling in place to 0 in this case
return r == this.scaleTo(0).b;
};
Num.eqr = function(a, r) { return a.eqr(r); };
Num.prototype.neqr = function(r) {
if(r == 0) return this.b != 0; // avoid expensive scaling in place to 0 in this case
return r != this.scaleTo(0).b;
};
Num.neqr = function(a, r) { return a.neqr(r); };
Num.prototype.gtr = function(r) {
if(r == 0) return this.b > 0; // avoid expensive scaling in place to 0 in this case
return this.gt(new Num(r));
};
Num.gtr = function(a, r) { return a.gtr(r); };
Num.prototype.ger = function(r) {
if(r == 0) return this.b >= 0; // avoid expensive scaling in place to 0 in this case
return this.ge(new Num(r));
};
Num.ger = function(a, r) { return a.ger(r); };
Num.prototype.ltr = function(r) {
if(r == 0) return this.b < 0; // avoid expensive scaling in place to 0 in this case
return this.lt(new Num(r));
};
Num.ltr = function(a, r) { return a.ltr(r); };
Num.prototype.ler = function(r) {
if(r == 0) return this.b <= 0; // avoid expensive scaling in place to 0 in this case
return this.le(new Num(r));
};
Num.ler = function(a, r) { return a.ler(r); };
// synonyms
Num.prototype.lte = Num.prototype.le;
Num.lte = Num.le;
Num.prototype.lter = Num.prototype.ler;
Num.lter = Num.ler;
Num.prototype.gte = Num.prototype.ge;
Num.gte = Num.ge;
Num.prototype.gter = Num.prototype.ger;
Num.gter = Num.ger;
// Returns a copy of the min/max value, not the original object, so can use "in place" operations on original without affecting min/max result
Num.max = function(a, b) { return new Num(a.gt(b) ? a : b); }
Num.min = function(a, b) { return new Num(a.lt(b) ? a : b); }
// rel_eps is a regular JS number and represents how much a and b may differ, e.g. if rel_eps is 0.1 they may differ up to 10%
Num.prototype.near = function(b, rel_eps) {
if(this.eqr(0) != b.eqr(0)) return false;
if(this.gtr(0) != b.gtr(0)) return false;
var d = this.mulr(rel_eps);
if(b.lt(this.sub(d))) return false;
if(b.gt(this.add(d))) return false;
return true;
};
Num.near = function(a, b, rel_eps) {
return a.near(b, rel_eps);
};
// Only b indicates NaN or Infinity, but for safety, e is also checked
Num.prototype.isNaN = function() { return isNaN(this.b) || isNaN(this.e); };
Num.isNaN = function(a) { return isNaN(a.b) || isNaN(a.e); };
Num.prototype.isInfinity = function() { return this.b == Infinity; };
Num.isInfinity = function(a) { return a.b == Infinity; };
Num.prototype.isNegInfinity = function() { return this.b == -Infinity; };
Num.isNegInfinity = function(a) { return a.b == -Infinity; };
// mostly for the result of invalid computations, so also exponent is checked in similar way
Num.prototype.isNaNOrInfinity = function() { return isNaN(this.b) || isNaN(this.e) || this.b == Infinity || this.e == Infinity || this.b == -Infinity || this.e == -Infinity; };
Num.isNaNOrInfinity = function(a) { return isNaN(a.b) || isNaN(a.e) || a.b == Infinity || a.e == Infinity || a.b == -Infinity || a.e == -Infinity; };
// return 1-2-5 sequence number with index i (i is regular JS number, 0 corresponds to return value 1)
// the sequence is 1, 2, 5, 10, 20, 50, 100, 200, 500, etc... [search key: 1,2,5,10,20,50]
Num.get125 = function(i) {
var m = i % 3;
var d = Math.floor(i / 3);
var b = m == 0 ? 1 : (m == 1 ? 2 : 5);
var mul = Num.rpow(10, new Num(d));
return mul.mulr(b);
};
/*
rounds the base value to 6 bits of precision or to integer
the goal of this is the following:
Num.rpow(10, Num(1))
outputs:
Num {b: 1.2500000000000002, e: 3}
that the cost of a blackberry slightly higher than 10.
so if you then collect 10 resources, and try to plant one, it fails
so instead let the berry cost start at
Num.roundNicely(Num.rpow(10, Num(1)))
and it'll work perfectly.
*/
Num.roundNicely = function(num) {
var res = new Num(num);
// This should at least support values like 1000, 1.25, 1000000000, ..., but to remove the .0000...00002 of 1.2500000000000002 can't support very large integer either
res.b = Math.round(num.b * 2147483648) / 2147483648;
res.scaleInPlace();
return res;
}
var log10_2 = 0.30102999566398114; // ln(2) / ln(10) = log10(2)
var log2_10 = 1 / log10_2; // ln(10) / ln(2) - log2(10)
var ln_2 = 0.6931471805599453; // ln(2)
Num.N_Names = []; // names of the notations
Num.N_Help = []; // help of the notations
var n_i = 0;
Num.N_LATIN = n_i; Num.N_Names[n_i] = 'latin suffixes'; Num.N_Help[n_i] = 'Latin suffixes for large numbers, such as T for trillion, Qa for quadrillion, V for vigintillion, ...'; n_i++; // abbreviation if available, else engineering, also full if fits in #digits precision: most human-like notation
Num.N_HYBRID_T = n_i; Num.N_Names[n_i] = 'suffixes up to T, then eng'; Num.N_Help[n_i] = 'Latin suffixes, but only up to T (trillion, 1e12), then switches to engineering notation'; n_i++;
Num.N_HYBRID_U = n_i; Num.N_Names[n_i] = 'suffixes up to U, then eng'; Num.N_Help[n_i] = 'Latin suffixes, but only up to U (undecillion, 1e36), then switches to engineering notation'; n_i++; // a later version of hybrid
Num.N_SI = n_i; Num.N_Names[n_i] = 'suffixes SI'; Num.N_Help[n_i] = 'SI suffixes, such as K for kilo (1000), G for giga (1e9), up to Y for yotta (1e24). For larger numbers switches to engineering notation'; n_i++; // using the SI prefixes K,M,G,T,P,E,Z,Y, then engineering
Num.N_ABC = n_i; Num.N_Names[n_i] = 'suffixes abc'; Num.N_Help[n_i] = 'abc suffixes, 1a for 1000, 1b for 1000000, 1aa for 1e81, etc...'; n_i++; // using the SI prefixes K,M,G,T,P,E,Z,Y, then engineering
Num.N_ENG = n_i; Num.N_Names[n_i] = 'engineering'; Num.N_Help[n_i] = 'Use exponent notation, and the exponents are always multiples of 3. E.g. 10e6 for 10 million'; n_i++; // strict engineering notation
Num.N_SCI = n_i; Num.N_Names[n_i] = 'scientific'; Num.N_Help[n_i] = 'Always uses exponents, such as 2e4 for 20000 (4 zeroes)'; n_i++; // strictly scientific notation, with base in range 1..10 (not 0..1)
Num.N_LOG = n_i; Num.N_Names[n_i] = 'logarithm'; Num.N_Help[n_i] = 'Uses the base-10 logarithm'; n_i++; // exponential notation (not to be confused with scientific): 10^log10(number)
Num.N_EXP = n_i; Num.N_Names[n_i] = 'natural'; Num.N_Help[n_i] = 'Uses the natural logarithm with e = 2.71828...'; n_i++; // exponential notation (not to be confused with scientific): e^ln(number)
Num.N_HEX = n_i; Num.N_Names[n_i] = 'hexadecimal'; Num.N_Help[n_i] = 'Uses base 16'; n_i++; // hex notation with hex exponent (but otherwise behaves like scientific or engineering)
Num.N_FULL = n_i; Num.N_Names[n_i] = 'full'; Num.N_Help[n_i] = 'Prints value with all digits, switches to scientific once unreasonably big'; n_i++;
Num.N_HYBRID_T_SCI = n_i; Num.N_Names[n_i] = 'suffixes up to T, then sci'; Num.N_Help[n_i] = 'Latin suffixes, but only up to T (trillion, 1e12), then switches to scientific notation'; n_i++;
Num.N_HYBRID_U_SCI = n_i; Num.N_Names[n_i] = 'suffixes up to U, then sci'; Num.N_Help[n_i] = 'Latin suffixes, but only up to U (undecillion, 1e36), then switches to scientific notation'; n_i++; // a later version of hybrid
Num.N_SI_SCI = n_i; Num.N_Names[n_i] = 'suffixes SI'; Num.N_Help[n_i] = 'SI suffixes, such as K for kilo (1000), G for giga (1e9), up to Y for yotta (1e24). For larger numbers switches to scientific notation'; n_i++; // using the SI prefixes K,M,G,T,P,E,Z,Y, then engineering
Num.N_Amount = n_i; // amount of notations
// The settings for toString if none given to the parameters, changeable as user option
Num.notation = Num.N_LATIN;
Num.precision = 3;
// units when standalone
var suffix_units0 = [
'K', // thousand, e3, latin 0
'M', // million, e6, latin 1
'B', // billion, e9, latin 2
'T', // trillion, e12, latin 3
'Qa', // quadrillion, e15, latin 4
'Qi', // quintillion, e18, latin 5
'Sx', // sextillion, e21, latin 6
'Sp', // septillion, e24, latin 7
'Oc', // octillion, e27, latin 8 --> has the c since O is confusable with digit 0
'N', // nonillion, e30, latin 9
];
// units when not standalone, but after a C, an Mi, ...
var suffix_units1 = [
'',
'U', // un, latin 1
'Du', // duo, latin 2 --> disambiguated with De (deca, 10)
'T', // trillion, e12, latin 3
'Qa', // quadrillion, e15, latin 4
'Qi', // quintillion, e18, latin 5
'Sx', // sextillion, e21, latin 6
'Sp', // septillion, e24, latin 7
'Oc', // octillion, e27, latin 8 --> has the c since O is confusable with digit 0
'N', // nonillion, e30, latin 9
];
// units when in front of a ten
var suffix_units10 = [
'',
'U', // un, latin 1 --> NOTE: when this is prefix of D, so we form UD (for latin 11), we turn it into just U because it's still unique at that poitn
'D', // duo, latin 2 --> not Du since not confusable with deca when in front of a ten
'T', // tre, latin 3
'Qa', // quattr/quadr, latin 4
'Qi', // quin, latin 5
'Sx', // sex, latin 6
'Sp', // sept, latin 7
'Oc', // oct, latin 8 --> has the c since O is confusable with digit 0
'N', // non, latin 9
];
// the tens, when standalone
var suffix_tens0 = [
'',
'D', // decillion, e33, latin 10, unique letter so far so doesn't need g or e behind it, and not confusable with Duo when standalone
'V', // vigintillion, e63, latin 20, unique letter so far so doesn't need the g behind it
'Tg', // trigintillion, e93, latin 30
'Qr', // quadragintillion, e123, latin 40 --> not Qag to avoid *three* letterw which is a bit much
'Qq', // quinquagintillion, e153, latin 50
'Sa', // sexagintillion, e183, latin 60
'Su', // septuagintillion, e213, latin 70
'Oa', // octagintillion, e243, latin 80 --> not Og, to avoid collision with OG for units_cent when case is ignored
'Na', // nonagintillion, e273, latin 90 --> not Ng, to avoid collision with NG for units_cent when case is ignored
];
// the tens, when after C, Mi, ...
var suffix_tens1 = [
'',
'De', // decillion, e33, latin 10, --> the e is to disambiguate from Du (duo, 2)
'V', // vigintillion, e63, latin 20, unique letter so far so doesn't need the g behind it
'Tg', // trigintillion, e93, latin 30
'Qr', // quadragintillion, e123, latin 40 --> not Qag to avoid *three* letters which is a bit much, not Qa to avoid collision with suffix_units
'Qq', // quinquagintillion, e153, latin 50
'Sa', // sexagintillion, e183, latin 60
'Su', // septuagintillion, e213, latin 70
'Oa', // octagintillion, e243, latin 80 --> not Og, to avoid collision with OG for units_cent when case is ignored
'Na', // nonagintillion, e273, latin 90 --> not Ng, to avoid collision with NG for units_cent when case is ignored
];
// Note how some have 'C', others 'G' due to how the names are (C from cen, G from gen)
var units_cents = [
'',
'C', // centillion, e303, latin 100
'DC', // duocentillion, e603, latin 200
'TC', // trecentillion, e903, latin 300
'QC', // quadringentillion, e1203, latin 400 --> here I let the C/G disambiguate between Qa qnd Qi. Trying to keep this 2 instead of 3 characters...
'QG', // quingentillion, e1503, latin 500
'SC', // sescentillion, e1803, latin 600 --> here at least the C makes more sense, given the name...
'SG', // septingentillion, e2103, latin 700
'OG', // octingentillion, e2403, latin 800
'NG', // nongentillion, e2703, latin 900
];
// the suffixes for 0-999 (as latin number, exponent is 3+3*that), up to right before millillion that is
var suffixes = [];
// the suffixes for what comes after the Mi for 1000-1999, as well as all higher values for anything after the last Mi or sandwiched between two Mi's
// same size as suffixes (that is, 1000 elements), but for appending to Mi, some of the first entries differ (e.g. K and B become Du and T instead)
var millsuffixes = [];
// the prefixes before the first Mi for values larger than 1999
// exactly the same except the single 'U' is replaced by '' because 1-thousand is to be shown as Mi, not as UMi. But thousand-and-one is MiU (with the U from millsuffixes).
var millprefixes = [];
function preparePrefixes() {
for(var c = 0; c < 10; c++) {
for(var d = 0; d < 10; d++) {
for(var u = 0; u < 10; u++) {
// the order is: hundreds, units, ten!
var s = units_cents[c];
s += (c == 0 && d == 0) ? suffix_units0[u] : (c == 0 ? suffix_units10[u] : suffix_units1[u]);
if(!(c == 0 && u == 1 && d == 1)) s += (c == 0 ? suffix_tens0[d] : suffix_tens1[d]); // UD becomes just U because unique
suffixes.push(s);
}
}
}
for(var c = 0; c < 10; c++) {
for(var d = 0; d < 10; d++) {
for(var u = 0; u < 10; u++) {
// the order is: hundreds, units, ten!
var s = units_cents[c];
s += (d == 0) ? suffix_units1[u] : suffix_units10[u];
// disambiguation with De is needed in all cases except if there's some unit value in front of the D
s += (u == 0) ? suffix_tens1[d] : suffix_tens0[d];
millsuffixes.push(s);
millprefixes.push(s == 'U' ? '' : s);
}
}
}
}
preparePrefixes();
// here e is the actual latin number represented, e.g. 100 for Centum
function getLatinSuffixV(e) {
if(e < 0) return undefined;
if(e == Infinity) return undefined;
if(e < 1000) return suffixes[e];
var result = millsuffixes[e % 1000];
while(e) {
e = Math.floor(e / 1000);
if(!e) break;
result = (e < 1000 ? millprefixes : millsuffixes)[e % 1000] + 'Mi' + result;
}
return result;
}
// exponent e must be multiple of 3
// returns undefined if out of range
function getLatinSuffix(e) {
if(e < 3) return undefined;
return getLatinSuffixV(Math.floor(e / 3) - 1);
}
var suffixes_inv = undefined;
var suffixes_inv2 = undefined;
// parses latin suffixes such as 'M', 'Qi' and 'DuMiMiC'. Case-insensitive. Returns the latin value itself, e.g. 100 for 'C'
function parseLatinSuffixV(s) {
if(!s) return -1; // since 'K' represents 0, no suffix is below that, so -1
s = s.toLowerCase();
if(!suffixes_inv) {
suffixes_inv = {};
for(var i = 0; i < suffixes.length; i++) {
suffixes_inv[suffixes[i].toLowerCase()] = i;
//if(suffixes[i].length == 1) suffixes_inv[suffixes[i].toLowerCase()] = i;
}
}
var r = suffixes_inv[s];
if(r != undefined) return r;
if(!suffixes_inv2) {
suffixes_inv2 = [];
suffixes_inv2[1] = {};
suffixes_inv2[2] = {};
suffixes_inv2[3] = {};
suffixes_inv2[4] = {};
suffixes_inv2[5] = {};
suffixes_inv2[6] = {};
for(var i = 1; i < millsuffixes.length; i++) {
suffixes_inv2[millsuffixes[i].length][millsuffixes[i].toLowerCase()] = i;
}
suffixes_inv2[2]['mi'] = 0;
}
var mul = 1;
var result = 0;
var mi = false;
var first = true;
for(;;) {
var found = false;
for(var j = 6; j >= 1; j--) {
if(s.length < j) continue;
var suffix = s.substr(s.length - j);
var exp = suffixes_inv2[j][suffix];
if(exp != undefined) {
found = true;
if(j == 2 && suffix == 'mi') {
mul *= 1000;
mi = true;
} else {
result += exp * mul;
if(!first && !mi) return -1; // invalid, all must be separated by Mi
mi = false;
}
s = s.substr(0, s.length - j);
first = false;
break;
}
}
if(!found) return -1;
if(s.length == 0) break;
}
if(mi) result += mul;
return result;
}
// supports plain, scientific, engineering and latin notations.
Num.parse = function(text) {
var isDigit = function(c) {
c = c.charCodeAt(0);
return c >= 48 && c <= 57;
};
var isDigitOrSign = function(c) {
return c == '-' || c == '+' || isDigit(c);
};
var isDigitOrSymbol = function(c) {
return c == '.' || isDigitOrSign(c);
};
var l = text.toLowerCase();
if(l == 'inf' || l == 'infinity') return new Num(Infinity, 0);
if(l == '-inf' || l == '-infinity') return new Num(-Infinity, 0);
if(l == 'nan') return new Num(NaN, 0);
var e = text.indexOf('e');
if(e < 0) e = text.indexOf('E');
if(e > 0 && e + 1 < text.length && isDigitOrSign(text[e + 1])) {
var base = parseFloat(text.substr(0, e));
var exp = parseFloat(text.substr(e + 1));