-
Notifications
You must be signed in to change notification settings - Fork 2
/
gmp.umd.js
3498 lines (3475 loc) · 525 KB
/
gmp.umd.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
/*!
* gmp-wasm v1.1.0 (https://www.npmjs.com/package/gmp-wasm)
* (c) Dani Biro
* @license LGPL-3.0
*/
function gmpWasm() {
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.gmp = {}));
})(this, (function (exports) { 'use strict';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function isUint32(num) {
return Number.isSafeInteger(num) && num >= 0 && num < Math.pow(2, 32);
}
function assertUint32(num) {
if (!isUint32(num)) {
throw new Error('Invalid number specified: uint32_t is required');
}
}
function isInt32(num) {
return Number.isSafeInteger(num) && num >= -Math.pow(2, 31) && num < Math.pow(2, 31);
}
function assertInt32(num) {
if (!isInt32(num)) {
throw new Error('Invalid number specified: int32_t is required');
}
}
function assertArray(arr) {
if (!Array.isArray(arr)) {
throw new Error('Invalid parameter specified. Array is required!');
}
}
function isValidRadix(radix) {
return Number.isSafeInteger(radix) && radix >= 2 && radix <= 36;
}
function assertValidRadix(radix) {
if (!isValidRadix(radix)) {
throw new Error('radix must have a value between 2 and 36');
}
}
const FLOAT_SPECIAL_VALUES = {
'@NaN@': 'NaN',
'@Inf@': 'Infinity',
'-@Inf@': '-Infinity',
};
const FLOAT_SPECIAL_VALUE_KEYS = Object.keys(FLOAT_SPECIAL_VALUES);
const trimTrailingZeros = (num) => {
let pos = num.length - 1;
while (pos >= 0) {
if (num[pos] === '.') {
pos--;
break;
}
else if (num[pos] === '0') {
pos--;
}
else {
break;
}
}
if (pos !== num.length - 1) {
return num.slice(0, pos + 1);
}
if (num.length === 0) {
return '0';
}
return num;
};
const insertDecimalPoint = (mantissa, pointPos) => {
const isNegative = mantissa.startsWith('-');
const mantissaWithoutSign = isNegative ? mantissa.slice(1) : mantissa;
const sign = isNegative ? '-' : '';
let hasDecimalPoint = false;
if (pointPos <= 0) {
const zeros = '0'.repeat(-pointPos);
mantissa = `${sign}0.${zeros}${mantissaWithoutSign}`;
hasDecimalPoint = true;
}
else if (pointPos < mantissaWithoutSign.length) {
mantissa = `${sign}${mantissaWithoutSign.slice(0, pointPos)}.${mantissaWithoutSign.slice(pointPos)}`;
hasDecimalPoint = true;
}
else {
const zeros = '0'.repeat(pointPos - mantissaWithoutSign.length);
mantissa = `${mantissa}${zeros}`;
}
// trim trailing zeros after decimal point
if (hasDecimalPoint) {
mantissa = trimTrailingZeros(mantissa);
}
return mantissa;
};
// matches mpfr_rnd_t
/** Represents the different rounding modes. */
exports.FloatRoundingMode = void 0;
(function (FloatRoundingMode) {
/** Round to nearest, with ties to even. MPFR_RNDN */
FloatRoundingMode[FloatRoundingMode["ROUND_NEAREST"] = 0] = "ROUND_NEAREST";
/** Round toward zero. MPFR_RNDZ */
FloatRoundingMode[FloatRoundingMode["ROUND_TO_ZERO"] = 1] = "ROUND_TO_ZERO";
/** Round toward +Infinity. MPFR_RNDU */
FloatRoundingMode[FloatRoundingMode["ROUND_UP"] = 2] = "ROUND_UP";
/** Round toward -Infinity. MPFR_RNDD */
FloatRoundingMode[FloatRoundingMode["ROUND_DOWN"] = 3] = "ROUND_DOWN";
/** Round away from zero. MPFR_RNDA */
FloatRoundingMode[FloatRoundingMode["ROUND_FROM_ZERO"] = 4] = "ROUND_FROM_ZERO";
// /** (Experimental) Faithful rounding. MPFR_RNDF */
// ROUND_FAITHFUL = 5,
// /** (Experimental) Round to nearest, with ties away from zero. MPFR_RNDNA */
// ROUND_TO_NEAREST_AWAY_FROM_ZERO = -1,
})(exports.FloatRoundingMode || (exports.FloatRoundingMode = {}));
const INVALID_PARAMETER_ERROR$2 = 'Invalid parameter!';
function getFloatContext(gmp, ctx, ctxOptions) {
var _a, _b, _c;
const mpfr_t_arr = [];
const isInteger = (val) => ctx.intContext.isInteger(val);
const isRational = (val) => ctx.rationalContext.isRational(val);
const isFloat = (val) => ctx.floatContext.isFloat(val);
const globalRndMode = ((_a = ctxOptions.roundingMode) !== null && _a !== void 0 ? _a : exports.FloatRoundingMode.ROUND_NEAREST);
const globalPrecisionBits = (_b = ctxOptions.precisionBits) !== null && _b !== void 0 ? _b : 52;
const globalRadix = (_c = ctxOptions.radix) !== null && _c !== void 0 ? _c : 10;
assertUint32(globalPrecisionBits);
assertValidRadix(globalRadix);
const compare = (mpfr_t, val) => {
if (typeof val === 'number') {
assertInt32(val);
return gmp.mpfr_cmp_si(mpfr_t, val);
}
if (typeof val === 'string') {
const f = FloatFn(val, ctxOptions);
return gmp.mpfr_cmp(mpfr_t, f.mpfr_t);
}
if (isInteger(val)) {
return gmp.mpfr_cmp_z(mpfr_t, val.mpz_t);
}
if (isRational(val)) {
return gmp.mpfr_cmp_q(mpfr_t, val.mpq_t);
}
if (isFloat(val)) {
return gmp.mpfr_cmp(mpfr_t, val.mpfr_t);
}
throw new Error(INVALID_PARAMETER_ERROR$2);
};
const mergeFloatOptions = (options1, options2) => {
var _a, _b, _c, _d, _e, _f;
const precisionBits1 = (_a = options1 === null || options1 === void 0 ? void 0 : options1.precisionBits) !== null && _a !== void 0 ? _a : globalPrecisionBits;
const precisionBits2 = (_b = options2 === null || options2 === void 0 ? void 0 : options2.precisionBits) !== null && _b !== void 0 ? _b : globalPrecisionBits;
return {
precisionBits: Math.max(precisionBits1, precisionBits2),
roundingMode: (_d = (_c = options2 === null || options2 === void 0 ? void 0 : options2.roundingMode) !== null && _c !== void 0 ? _c : options1.roundingMode) !== null && _d !== void 0 ? _d : ctxOptions.roundingMode,
radix: (_f = (_e = options2 === null || options2 === void 0 ? void 0 : options2.radix) !== null && _e !== void 0 ? _e : options1.radix) !== null && _f !== void 0 ? _f : ctxOptions.radix,
};
};
const FloatPrototype = {
mpfr_t: 0,
precisionBits: -1,
rndMode: -1,
radix: -1,
type: 'float',
get options() {
var _a, _b, _c;
return {
precisionBits: (_a = this.precisionBits) !== null && _a !== void 0 ? _a : globalPrecisionBits,
roundingMode: (_b = this.rndMode) !== null && _b !== void 0 ? _b : globalRndMode,
radix: (_c = this.radix) !== null && _c !== void 0 ? _c : globalRadix,
};
},
get setOptions() {
return {
precisionBits: this.precisionBits,
roundingMode: this.rndMode,
radix: this.radix,
};
},
/** Returns the sum of this number and the given one. */
add(val) {
if (typeof val === 'number') {
const n = FloatFn(null, this.options);
gmp.mpfr_add_d(n.mpfr_t, this.mpfr_t, val, this.rndMode);
return n;
}
if (typeof val === 'string') {
const n = FloatFn(val, this.options);
gmp.mpfr_add(n.mpfr_t, this.mpfr_t, n.mpfr_t, this.rndMode);
return n;
}
if (isFloat(val)) {
const n = FloatFn(null, mergeFloatOptions(this.setOptions, val.setOptions));
gmp.mpfr_add(n.mpfr_t, this.mpfr_t, val.mpfr_t, this.rndMode);
return n;
}
if (isRational(val)) {
const n = FloatFn(null, this.options);
gmp.mpfr_add_q(n.mpfr_t, this.mpfr_t, val.mpq_t, this.rndMode);
return n;
}
if (isInteger(val)) {
const n = FloatFn(null, this.options);
gmp.mpfr_add_z(n.mpfr_t, this.mpfr_t, val.mpz_t, this.rndMode);
return n;
}
throw new Error(INVALID_PARAMETER_ERROR$2);
},
/** Returns the difference of this number and the given one. */
sub(val) {
if (typeof val === 'number') {
const n = FloatFn(null, this.options);
gmp.mpfr_sub_d(n.mpfr_t, this.mpfr_t, val, this.rndMode);
return n;
}
if (typeof val === 'string') {
const n = FloatFn(val, this.options);
gmp.mpfr_sub(n.mpfr_t, this.mpfr_t, n.mpfr_t, this.rndMode);
return n;
}
if (isFloat(val)) {
const n = FloatFn(null, mergeFloatOptions(this.setOptions, val.setOptions));
gmp.mpfr_sub(n.mpfr_t, this.mpfr_t, val.mpfr_t, this.rndMode);
return n;
}
if (isRational(val)) {
const n = FloatFn(null, this.options);
gmp.mpfr_sub_q(n.mpfr_t, this.mpfr_t, val.mpq_t, this.rndMode);
return n;
}
if (isInteger(val)) {
const n = FloatFn(null, this.options);
gmp.mpfr_sub_z(n.mpfr_t, this.mpfr_t, val.mpz_t, this.rndMode);
return n;
}
throw new Error(INVALID_PARAMETER_ERROR$2);
},
/** Returns the product of this number and the given one. */
mul(val) {
if (typeof val === 'number') {
const n = FloatFn(null, this.options);
if (isInt32(val)) {
gmp.mpfr_mul_si(n.mpfr_t, this.mpfr_t, val, this.rndMode);
}
else {
gmp.mpfr_mul_d(n.mpfr_t, this.mpfr_t, val, this.rndMode);
}
return n;
}
if (typeof val === 'string') {
const n = FloatFn(val, this.options);
gmp.mpfr_mul(n.mpfr_t, this.mpfr_t, n.mpfr_t, this.rndMode);
return n;
}
if (isFloat(val)) {
const n = FloatFn(null, mergeFloatOptions(this.setOptions, val.setOptions));
gmp.mpfr_mul(n.mpfr_t, this.mpfr_t, val.mpfr_t, this.rndMode);
return n;
}
if (isRational(val)) {
const n = FloatFn(null, this.options);
gmp.mpfr_mul_q(n.mpfr_t, this.mpfr_t, val.mpq_t, this.rndMode);
return n;
}
if (isInteger(val)) {
const n = FloatFn(null, this.options);
gmp.mpfr_mul_z(n.mpfr_t, this.mpfr_t, val.mpz_t, this.rndMode);
return n;
}
throw new Error(INVALID_PARAMETER_ERROR$2);
},
/** Returns the result of the division of this number by the given one. */
div(val) {
if (typeof val === 'number') {
const n = FloatFn(null, this.options);
gmp.mpfr_div_d(n.mpfr_t, this.mpfr_t, val, this.rndMode);
return n;
}
if (typeof val === 'string') {
const n = FloatFn(val, this.options);
gmp.mpfr_div(n.mpfr_t, this.mpfr_t, n.mpfr_t, this.rndMode);
return n;
}
if (isFloat(val)) {
const n = FloatFn(null, mergeFloatOptions(this.setOptions, val.setOptions));
gmp.mpfr_div(n.mpfr_t, this.mpfr_t, val.mpfr_t, this.rndMode);
return n;
}
if (isRational(val)) {
const n = FloatFn(null, this.options);
gmp.mpfr_div_q(n.mpfr_t, this.mpfr_t, val.mpq_t, this.rndMode);
return n;
}
if (isInteger(val)) {
const n = FloatFn(null, this.options);
gmp.mpfr_div_z(n.mpfr_t, this.mpfr_t, val.mpz_t, this.rndMode);
return n;
}
throw new Error(INVALID_PARAMETER_ERROR$2);
},
/** Returns the square root. */
sqrt() {
const n = FloatFn(null, this.options);
gmp.mpfr_sqrt(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the reciprocal square root. */
invSqrt() {
const n = FloatFn(null, this.options);
gmp.mpfr_rec_sqrt(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the cube root. */
cbrt() {
const n = FloatFn(null, this.options);
gmp.mpfr_cbrt(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the n-th root. */
nthRoot(nth) {
const n = FloatFn(null, this.options);
assertUint32(nth);
gmp.mpfr_rootn_ui(n.mpfr_t, this.mpfr_t, nth, this.rndMode);
return n;
},
/** Returns the number with inverted sign. */
neg() {
const n = FloatFn(null, this.options);
gmp.mpfr_neg(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the absolute value. */
abs() {
const n = FloatFn(null, this.options);
gmp.mpfr_abs(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the factorial */
factorial() {
const n = FloatFn(null, this.options);
if (gmp.mpfr_fits_uint_p(this.mpfr_t, this.rndMode) === 0) {
throw new Error('Invalid value for factorial()');
}
const value = gmp.mpfr_get_ui(this.mpfr_t, this.rndMode);
gmp.mpfr_fac_ui(n.mpfr_t, value, this.rndMode);
return n;
},
/** Returns true if the number is an integer */
isInteger() {
return gmp.mpfr_integer_p(this.mpfr_t) !== 0;
},
/** Returns true if the number is zero */
isZero() {
return gmp.mpfr_zero_p(this.mpfr_t) !== 0;
},
/** Returns true if the number is a regular number (i.e., neither NaN, nor an infinity nor zero) */
isRegular() {
return gmp.mpfr_regular_p(this.mpfr_t) !== 0;
},
/** Return true if the number is an ordinary number (i.e., neither NaN nor an infinity) */
isNumber() {
return gmp.mpfr_number_p(this.mpfr_t) !== 0;
},
/** Returns true if the number is an infinity */
isInfinite() {
return gmp.mpfr_inf_p(this.mpfr_t) !== 0;
},
/** Returns true if the number is NaN */
isNaN() {
return gmp.mpfr_nan_p(this.mpfr_t) !== 0;
},
/** Returns true if the current number is equal to the provided number */
isEqual(val) {
return compare(this.mpfr_t, val) === 0;
},
/** Returns true if the current number is less than the provided number */
lessThan(val) {
return compare(this.mpfr_t, val) < 0;
},
/** Returns true if the current number is less than or equal to the provided number */
lessOrEqual(val) {
return compare(this.mpfr_t, val) <= 0;
},
/** Returns true if the current number is greater than the provided number */
greaterThan(val) {
return compare(this.mpfr_t, val) > 0;
},
/** Returns true if the current number is greater than or equal to the provided number */
greaterOrEqual(val) {
return compare(this.mpfr_t, val) >= 0;
},
/** Returns the natural logarithm */
ln() {
const n = FloatFn(null, this.options);
gmp.mpfr_log(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the base 2 logarithm */
log2() {
const n = FloatFn(null, this.options);
gmp.mpfr_log2(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the base 10 logarithm */
log10() {
const n = FloatFn(null, this.options);
gmp.mpfr_log10(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the exponential (e^x) */
exp() {
const n = FloatFn(null, this.options);
gmp.mpfr_exp(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns 2 to the power of current number (2^x) */
exp2() {
const n = FloatFn(null, this.options);
gmp.mpfr_exp2(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns 10 to the power of current number (10^x) */
exp10() {
const n = FloatFn(null, this.options);
gmp.mpfr_exp10(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns this number exponentiated to the given value. */
pow(val) {
const n = FloatFn(null, this.options);
if (typeof val === 'number') {
if (isInt32(val)) {
gmp.mpfr_pow_si(n.mpfr_t, this.mpfr_t, val, this.rndMode);
}
else {
gmp.mpfr_pow(n.mpfr_t, this.mpfr_t, FloatFn(val).mpfr_t, this.rndMode);
}
}
else {
gmp.mpfr_pow(n.mpfr_t, this.mpfr_t, val.mpfr_t, this.rndMode);
}
return n;
},
/** Returns the sine */
sin() {
const n = FloatFn(null, this.options);
gmp.mpfr_sin(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the cosine */
cos() {
const n = FloatFn(null, this.options);
gmp.mpfr_cos(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the tangent */
tan() {
const n = FloatFn(null, this.options);
gmp.mpfr_tan(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the secant */
sec() {
const n = FloatFn(null, this.options);
gmp.mpfr_sec(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the cosecant */
csc() {
const n = FloatFn(null, this.options);
gmp.mpfr_csc(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the cotangent */
cot() {
const n = FloatFn(null, this.options);
gmp.mpfr_cot(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the arc-cosine */
acos() {
const n = FloatFn(null, this.options);
gmp.mpfr_acos(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the arc-sine */
asin() {
const n = FloatFn(null, this.options);
gmp.mpfr_asin(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the arc-tangent */
atan() {
const n = FloatFn(null, this.options);
gmp.mpfr_atan(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the hyperbolic sine */
sinh() {
const n = FloatFn(null, this.options);
gmp.mpfr_sinh(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the hyperbolic cosine */
cosh() {
const n = FloatFn(null, this.options);
gmp.mpfr_cosh(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the hyperbolic tangent */
tanh() {
const n = FloatFn(null, this.options);
gmp.mpfr_tanh(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the hyperbolic secant */
sech() {
const n = FloatFn(null, this.options);
gmp.mpfr_sech(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the hyperbolic cosecant */
csch() {
const n = FloatFn(null, this.options);
gmp.mpfr_csch(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the hyperbolic cotangent */
coth() {
const n = FloatFn(null, this.options);
gmp.mpfr_coth(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the inverse hyperbolic cosine */
acosh() {
const n = FloatFn(null, this.options);
gmp.mpfr_acosh(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the inverse hyperbolic sine */
asinh() {
const n = FloatFn(null, this.options);
gmp.mpfr_asinh(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the inverse hyperbolic tangent */
atanh() {
const n = FloatFn(null, this.options);
gmp.mpfr_atanh(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Calculate exponential integral */
eint() {
const n = FloatFn(null, this.options);
gmp.mpfr_eint(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Calculate the real part of the dilogarithm (the integral of -log(1-t)/t from 0 to op) */
li2() {
const n = FloatFn(null, this.options);
gmp.mpfr_li2(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Calculate the value of the Gamma function. */
gamma() {
const n = FloatFn(null, this.options);
gmp.mpfr_gamma(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Calculate the value of the logarithm of the absolute value of the Gamma function */
lngamma() {
const n = FloatFn(null, this.options);
gmp.mpfr_lngamma(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Calculate the value of the Digamma (sometimes also called Psi) function */
digamma() {
const n = FloatFn(null, this.options);
gmp.mpfr_digamma(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Calculate the value of the Beta function */
beta(op2) {
if (!isFloat(op2)) {
throw new Error('Only floats parameters are supported!');
}
const n = FloatFn(null, this.options);
gmp.mpfr_beta(n.mpfr_t, this.mpfr_t, op2.mpfr_t, this.rndMode);
return n;
},
/** Calculate the value of the Riemann Zeta function */
zeta() {
const n = FloatFn(null, this.options);
gmp.mpfr_zeta(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Calculate the value of the error function */
erf() {
const n = FloatFn(null, this.options);
gmp.mpfr_erf(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Calculate the value of the complementary error function */
erfc() {
const n = FloatFn(null, this.options);
gmp.mpfr_erfc(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Calculate the value of the first kind Bessel function of order 0 */
j0() {
const n = FloatFn(null, this.options);
gmp.mpfr_j0(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Calculate the value of the first kind Bessel function of order 1 */
j1() {
const n = FloatFn(null, this.options);
gmp.mpfr_j1(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Calculate the value of the first kind Bessel function of order n */
jn(n) {
assertInt32(n);
const rop = FloatFn(null, this.options);
gmp.mpfr_jn(rop.mpfr_t, n, this.mpfr_t, this.rndMode);
return rop;
},
/** Calculate the value of the second kind Bessel function of order 0 */
y0() {
const n = FloatFn(null, this.options);
gmp.mpfr_y0(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Calculate the value of the second kind Bessel function of order 1 */
y1() {
const n = FloatFn(null, this.options);
gmp.mpfr_y1(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Calculate the value of the second kind Bessel function of order n */
yn(n) {
assertInt32(n);
const rop = FloatFn(null, this.options);
gmp.mpfr_yn(rop.mpfr_t, n, this.mpfr_t, this.rndMode);
return rop;
},
/** Calculate the arithmetic-geometric mean */
agm(op2) {
if (!isFloat(op2)) {
throw new Error('Only floats parameters are supported!');
}
const n = FloatFn(null, this.options);
gmp.mpfr_agm(n.mpfr_t, this.mpfr_t, op2.mpfr_t, this.rndMode);
return n;
},
/** Calculate the value of the Airy function Ai on x */
ai() {
const n = FloatFn(null, this.options);
gmp.mpfr_ai(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Returns the sign of the current value (-1 or 0 or 1) */
sign() {
return gmp.mpfr_sgn(this.mpfr_t);
},
/** Converts current value into a JavaScript number */
toNumber() {
return gmp.mpfr_get_d(this.mpfr_t, this.rndMode);
},
/** Rounds to the next higher or equal representable integer */
ceil() {
const n = FloatFn(null, this.options);
gmp.mpfr_ceil(n.mpfr_t, this.mpfr_t);
return n;
},
/** Rounds to the next lower or equal representable integer */
floor() {
const n = FloatFn(null, this.options);
gmp.mpfr_floor(n.mpfr_t, this.mpfr_t);
return n;
},
/** Rounds to the nearest representable integer, rounding halfway cases away from zero */
round() {
const n = FloatFn(null, this.options);
gmp.mpfr_round(n.mpfr_t, this.mpfr_t);
return n;
},
/** Rounds to the nearest representable integer, rounding halfway cases with the even-rounding rule */
roundEven() {
const n = FloatFn(null, this.options);
gmp.mpfr_roundeven(n.mpfr_t, this.mpfr_t);
return n;
},
/** Rounds to the next representable integer toward zero */
trunc() {
const n = FloatFn(null, this.options);
gmp.mpfr_trunc(n.mpfr_t, this.mpfr_t);
return n;
},
/** Round to precision */
roundTo(prec) {
assertUint32(prec);
const n = FloatFn(this, this.options);
gmp.mpfr_prec_round(this.mpfr_t, prec, this.rndMode);
return n;
},
/** Returns the fractional part */
frac() {
const n = FloatFn(null, this.options);
gmp.mpfr_frac(n.mpfr_t, this.mpfr_t, this.rndMode);
return n;
},
/** Calculate the value of x - ny, where n is the integer quotient of x divided by y; n is rounded toward zero */
fmod(y) {
if (!isFloat(y)) {
throw new Error('Only floats parameters are supported!');
}
const n = FloatFn(null, this.options);
gmp.mpfr_fmod(n.mpfr_t, this.mpfr_t, y.mpfr_t, this.rndMode);
return n;
},
/** Calculate the value of x - ny, where n is the integer quotient of x divided by y; n is rounded to the nearest integer (ties rounded to even) */
remainder(y) {
if (!isFloat(y)) {
throw new Error('Only floats parameters are supported!');
}
const n = FloatFn(null, this.options);
gmp.mpfr_remainder(n.mpfr_t, this.mpfr_t, y.mpfr_t, this.rndMode);
return n;
},
/** Return next value towards +∞ */
nextAbove() {
const n = FloatFn(this, this.options);
gmp.mpfr_nextabove(n.mpfr_t);
return n;
},
/** Return next value towards -∞ */
nextBelow() {
const n = FloatFn(this, this.options);
gmp.mpfr_nextbelow(n.mpfr_t);
return n;
},
/** Returns the exponent of x, assuming that x is a non-zero ordinary number and the significand is considered in [1/2, 1). */
exponent() {
return gmp.mpfr_get_exp(this.mpfr_t);
},
/** Converts the number to string */
toString(radix) {
radix = radix !== null && radix !== void 0 ? radix : this.options.radix;
assertValidRadix(radix);
const str = gmp.mpfr_to_string(this.mpfr_t, radix, this.rndMode);
return str;
},
/** Formats the number using fixed-point notation */
toFixed(digits = 0, radix) {
assertUint32(digits);
radix = radix !== null && radix !== void 0 ? radix : this.options.radix;
assertValidRadix(radix);
const str = this.toString(radix);
if (Object.values(FLOAT_SPECIAL_VALUES).includes(str)) {
return str;
}
if (digits === 0) {
return ctx.intContext.Integer(this).toString(radix);
}
let multiplier = null;
if (radix === 2) {
multiplier = FloatFn(digits).exp2();
}
else if (radix === 10) {
multiplier = FloatFn(digits).exp10();
}
else {
multiplier = FloatFn(radix).pow(digits);
}
const multiplied = this.mul(multiplier);
const int = ctx.intContext.Integer(multiplied);
const isNegative = int.sign() === -1;
let intStr = int.abs().toString(radix);
if (intStr.length < digits + 1) {
intStr = '0'.repeat(digits + 1 - intStr.length) + intStr;
}
return `${isNegative ? '-' : ''}${intStr.slice(0, -digits)}.${intStr.slice(-digits)}`;
},
/** Converts the number to an integer */
toInteger() {
return ctx.intContext.Integer(this);
},
/** Converts the number to a rational number */
toRational() {
return ctx.rationalContext.Rational(this);
},
};
const setValue = (mpfr_t, rndMode, radix, val) => {
if (typeof val === 'string') {
const res = gmp.mpfr_set_string(mpfr_t, val, radix, rndMode);
if (res !== 0) {
throw new Error('Invalid number provided!');
}
return;
}
if (typeof val === 'number') {
if (isInt32(val)) {
gmp.mpfr_set_si(mpfr_t, val, rndMode);
if (Object.is(val, -0)) {
gmp.mpfr_neg(mpfr_t, mpfr_t, rndMode);
}
}
else {
gmp.mpfr_set_d(mpfr_t, val, rndMode);
}
return;
}
if (isFloat(val)) {
gmp.mpfr_set(mpfr_t, val.mpfr_t, rndMode);
return;
}
if (isRational(val)) {
gmp.mpfr_set_q(mpfr_t, val.mpq_t, rndMode);
return;
}
if (isInteger(val)) {
gmp.mpfr_set_z(mpfr_t, val.mpz_t, rndMode);
return;
}
throw new Error(INVALID_PARAMETER_ERROR$2);
};
const FloatFn = (val, options) => {
var _a, _b, _c;
const rndMode = ((_a = options === null || options === void 0 ? void 0 : options.roundingMode) !== null && _a !== void 0 ? _a : globalRndMode);
const precisionBits = (_b = options === null || options === void 0 ? void 0 : options.precisionBits) !== null && _b !== void 0 ? _b : globalPrecisionBits;
const radix = (_c = options === null || options === void 0 ? void 0 : options.radix) !== null && _c !== void 0 ? _c : globalRadix;
assertValidRadix(radix);
const instance = Object.create(FloatPrototype);
instance.rndMode = rndMode;
instance.precisionBits = precisionBits;
instance.radix = radix;
instance.mpfr_t = gmp.mpfr_t();
gmp.mpfr_init2(instance.mpfr_t, precisionBits);
if (val !== undefined && val !== null) {
setValue(instance.mpfr_t, rndMode, radix, val);
}
mpfr_t_arr.push(instance.mpfr_t);
return instance;
};
return {
Float: FloatFn,
isFloat: (val) => FloatPrototype.isPrototypeOf(val),
Pi: (options) => {
var _a;
const n = FloatFn(null, options);
gmp.mpfr_const_pi(n.mpfr_t, ((_a = options === null || options === void 0 ? void 0 : options.roundingMode) !== null && _a !== void 0 ? _a : globalRndMode));
return n;
},
EulerConstant: (options) => {
var _a;
const n = FloatFn(null, options);
gmp.mpfr_const_euler(n.mpfr_t, ((_a = options === null || options === void 0 ? void 0 : options.roundingMode) !== null && _a !== void 0 ? _a : globalRndMode));
return n;
},
EulerNumber: (options) => {
return FloatFn(1, options).exp();
},
Log2: (options) => {
var _a;
const n = FloatFn(null, options);
gmp.mpfr_const_log2(n.mpfr_t, ((_a = options === null || options === void 0 ? void 0 : options.roundingMode) !== null && _a !== void 0 ? _a : globalRndMode));
return n;
},
Catalan: (options) => {
var _a;
const n = FloatFn(null, options);
gmp.mpfr_const_catalan(n.mpfr_t, ((_a = options === null || options === void 0 ? void 0 : options.roundingMode) !== null && _a !== void 0 ? _a : globalRndMode));
return n;
},
destroy: () => {
for (let i = mpfr_t_arr.length - 1; i >= 0; i--) {
gmp.mpfr_clear(mpfr_t_arr[i]);
gmp.mpfr_t_free(mpfr_t_arr[i]);
}
mpfr_t_arr.length = 0;
}
};
}
// DEFLATE is a complex format; to read this code, you should probably check the RFC first:
// aliases for shorter compressed code (most minifers don't do this)
var u8 = Uint8Array, u16 = Uint16Array, u32 = Uint32Array;
// fixed length extra bits
var fleb = new u8([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, /* unused */ 0, 0, /* impossible */ 0]);
// fixed distance extra bits
// see fleb note
var fdeb = new u8([0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, /* unused */ 0, 0]);
// code length index map
var clim = new u8([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
// get base, reverse index map from extra bits
var freb = function (eb, start) {
var b = new u16(31);
for (var i = 0; i < 31; ++i) {
b[i] = start += 1 << eb[i - 1];
}
// numbers here are at max 18 bits
var r = new u32(b[30]);
for (var i = 1; i < 30; ++i) {
for (var j = b[i]; j < b[i + 1]; ++j) {
r[j] = ((j - b[i]) << 5) | i;
}
}
return [b, r];
};
var _a = freb(fleb, 2), fl = _a[0], revfl = _a[1];
// we can ignore the fact that the other numbers are wrong; they never happen anyway
fl[28] = 258, revfl[258] = 28;
var _b = freb(fdeb, 0), fd = _b[0];
// map of value to reverse (assuming 16 bits)
var rev = new u16(32768);
for (var i = 0; i < 32768; ++i) {
// reverse table algorithm from SO
var x = ((i & 0xAAAA) >>> 1) | ((i & 0x5555) << 1);
x = ((x & 0xCCCC) >>> 2) | ((x & 0x3333) << 2);
x = ((x & 0xF0F0) >>> 4) | ((x & 0x0F0F) << 4);
rev[i] = (((x & 0xFF00) >>> 8) | ((x & 0x00FF) << 8)) >>> 1;
}
// create huffman tree from u8 "map": index -> code length for code index
// mb (max bits) must be at most 15
// TODO: optimize/split up?
var hMap = (function (cd, mb, r) {
var s = cd.length;
// index
var i = 0;
// u16 "map": index -> # of codes with bit length = index
var l = new u16(mb);
// length of cd must be 288 (total # of codes)
for (; i < s; ++i) {
if (cd[i])
++l[cd[i] - 1];
}
// u16 "map": index -> minimum code for bit length = index
var le = new u16(mb);
for (i = 0; i < mb; ++i) {
le[i] = (le[i - 1] + l[i - 1]) << 1;
}
var co;
if (r) {
// u16 "map": index -> number of actual bits, symbol for code
co = new u16(1 << mb);
// bits to remove for reverser
var rvb = 15 - mb;
for (i = 0; i < s; ++i) {
// ignore 0 lengths
if (cd[i]) {
// num encoding both symbol and bits read
var sv = (i << 4) | cd[i];
// free bits
var r_1 = mb - cd[i];
// start value
var v = le[cd[i] - 1]++ << r_1;
// m is end value
for (var m = v | ((1 << r_1) - 1); v <= m; ++v) {
// every 16 bit value starting with the code yields the same result
co[rev[v] >>> rvb] = sv;
}
}
}
}
else {
co = new u16(s);
for (i = 0; i < s; ++i) {
if (cd[i]) {
co[i] = rev[le[cd[i] - 1]++] >>> (15 - cd[i]);
}
}
}
return co;
});