@xort/cga 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/three.js CHANGED
@@ -746,1999 +746,246 @@ var BSP = class BSP {
746
746
  return new BSP(a.allPolygons());
747
747
  }
748
748
  };
749
- // node_modules/decimal.js/decimal.mjs
750
- /*!
751
- * decimal.js v10.6.0
752
- * An arbitrary-precision Decimal type for JavaScript.
753
- * https://github.com/MikeMcl/decimal.js
754
- * Copyright (c) 2025 Michael Mclaughlin <M8ch88l@gmail.com>
755
- * MIT Licence
756
- */
757
- var EXP_LIMIT = 9e15;
758
- var MAX_DIGITS = 1e9;
759
- var NUMERALS = "0123456789abcdef";
760
- var LN10 = "2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058";
761
- var PI = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789";
762
- var DEFAULTS = {
763
- precision: 20,
764
- rounding: 4,
765
- modulo: 1,
766
- toExpNeg: -7,
767
- toExpPos: 21,
768
- minE: -EXP_LIMIT,
769
- maxE: EXP_LIMIT,
770
- crypto: false
771
- };
772
- var inexact;
773
- var quadrant;
774
- var external = true;
775
- var decimalError = "[DecimalError] ";
776
- var invalidArgument = decimalError + "Invalid argument: ";
777
- var precisionLimitExceeded = decimalError + "Precision limit exceeded";
778
- var cryptoUnavailable = decimalError + "crypto unavailable";
779
- var tag = "[object Decimal]";
780
- var mathfloor = Math.floor;
781
- var mathpow = Math.pow;
782
- var isBinary = /^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i;
783
- var isHex = /^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i;
784
- var isOctal = /^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i;
785
- var isDecimal = /^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;
786
- var BASE = 1e7;
787
- var LOG_BASE = 7;
788
- var MAX_SAFE_INTEGER = 0x1fffffffffffff;
789
- var LN10_PRECISION = LN10.length - 1;
790
- var PI_PRECISION = PI.length - 1;
791
- var P = { toStringTag: tag };
792
- P.absoluteValue = P.abs = function() {
793
- var x = new this.constructor(this);
794
- if (x.s < 0) x.s = 1;
795
- return finalise(x);
796
- };
797
- P.ceil = function() {
798
- return finalise(new this.constructor(this), this.e + 1, 2);
799
- };
800
- P.clampedTo = P.clamp = function(min, max) {
801
- var k, x = this, Ctor = x.constructor;
802
- min = new Ctor(min);
803
- max = new Ctor(max);
804
- if (!min.s || !max.s) return new Ctor(NaN);
805
- if (min.gt(max)) throw Error(invalidArgument + max);
806
- k = x.cmp(min);
807
- return k < 0 ? min : x.cmp(max) > 0 ? max : new Ctor(x);
808
- };
809
- P.comparedTo = P.cmp = function(y) {
810
- var i, j, xdL, ydL, x = this, xd = x.d, yd = (y = new x.constructor(y)).d, xs = x.s, ys = y.s;
811
- if (!xd || !yd) return !xs || !ys ? NaN : xs !== ys ? xs : xd === yd ? 0 : !xd ^ xs < 0 ? 1 : -1;
812
- if (!xd[0] || !yd[0]) return xd[0] ? xs : yd[0] ? -ys : 0;
813
- if (xs !== ys) return xs;
814
- if (x.e !== y.e) return x.e > y.e ^ xs < 0 ? 1 : -1;
815
- xdL = xd.length;
816
- ydL = yd.length;
817
- for (i = 0, j = xdL < ydL ? xdL : ydL; i < j; ++i) if (xd[i] !== yd[i]) return xd[i] > yd[i] ^ xs < 0 ? 1 : -1;
818
- return xdL === ydL ? 0 : xdL > ydL ^ xs < 0 ? 1 : -1;
819
- };
820
- P.cosine = P.cos = function() {
821
- var pr, rm, x = this, Ctor = x.constructor;
822
- if (!x.d) return new Ctor(NaN);
823
- if (!x.d[0]) return new Ctor(1);
824
- pr = Ctor.precision;
825
- rm = Ctor.rounding;
826
- Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
827
- Ctor.rounding = 1;
828
- x = cosine(Ctor, toLessThanHalfPi(Ctor, x));
829
- Ctor.precision = pr;
830
- Ctor.rounding = rm;
831
- return finalise(quadrant == 2 || quadrant == 3 ? x.neg() : x, pr, rm, true);
832
- };
833
- P.cubeRoot = P.cbrt = function() {
834
- var e, m, n, r, rep, s, sd, t, t3, t3plusx, x = this, Ctor = x.constructor;
835
- if (!x.isFinite() || x.isZero()) return new Ctor(x);
836
- external = false;
837
- s = x.s * mathpow(x.s * x, 1 / 3);
838
- if (!s || Math.abs(s) == 1 / 0) {
839
- n = digitsToString(x.d);
840
- e = x.e;
841
- if (s = (e - n.length + 1) % 3) n += s == 1 || s == -2 ? "0" : "00";
842
- s = mathpow(n, 1 / 3);
843
- e = mathfloor((e + 1) / 3) - (e % 3 == (e < 0 ? -1 : 2));
844
- if (s == 1 / 0) n = "5e" + e;
845
- else {
846
- n = s.toExponential();
847
- n = n.slice(0, n.indexOf("e") + 1) + e;
848
- }
849
- r = new Ctor(n);
850
- r.s = x.s;
851
- } else r = new Ctor(s.toString());
852
- sd = (e = Ctor.precision) + 3;
749
+ // src/math/Decimal.ts
750
+ var MAX_EXPONENT = 1e6;
751
+ var MAX_INPUT_DIGITS = 2e4;
752
+ var abs = (n) => n < 0n ? -n : n;
753
+ var digits = (n) => abs(n).toString().length;
754
+ function power(n) {
755
+ if (!Number.isSafeInteger(n) || n < 0 || n > 202e4) throw new RangeError("Decimal exponent exceeds the supported range");
756
+ return 10n ** BigInt(n);
757
+ }
758
+ function increment(q, remainder, divisor, sign, mode, sticky = false) {
759
+ if (remainder === 0n && !sticky) return false;
760
+ if (mode === 0) return true;
761
+ if (mode === 1) return false;
762
+ if (mode === 2) return sign > 0n;
763
+ if (mode === 3) return sign < 0n;
764
+ const compare = 2n * remainder - divisor;
765
+ if (compare !== 0n) return compare > 0n;
766
+ if (sticky) return true;
767
+ return mode === 4 || mode === 6 && abs(q) % 2n === 1n || mode === 7 && sign > 0n || mode === 8 && sign < 0n;
768
+ }
769
+ function integerSqrt(n) {
770
+ if (n < 2n) return n;
771
+ let x = 1n << BigInt(Math.ceil(n.toString(2).length / 2));
853
772
  for (;;) {
854
- t = r;
855
- t3 = t.times(t).times(t);
856
- t3plusx = t3.plus(x);
857
- r = divide(t3plusx.plus(x).times(t), t3plusx.plus(t3), sd + 2, 1);
858
- if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
859
- n = n.slice(sd - 3, sd + 1);
860
- if (n == "9999" || !rep && n == "4999") {
861
- if (!rep) {
862
- finalise(t, e + 1, 0);
863
- if (t.times(t).times(t).eq(x)) {
864
- r = t;
865
- break;
866
- }
867
- }
868
- sd += 4;
869
- rep = 1;
870
- } else {
871
- if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
872
- finalise(r, e + 1, 1);
873
- m = !r.times(r).times(r).eq(x);
874
- }
875
- break;
876
- }
877
- }
773
+ const next = (x + n / x) / 2n;
774
+ if (next >= x) return x;
775
+ x = next;
878
776
  }
879
- external = true;
880
- return finalise(r, e, Ctor.rounding, m);
881
- };
882
- P.decimalPlaces = P.dp = function() {
883
- var w, d = this.d, n = NaN;
884
- if (d) {
885
- w = d.length - 1;
886
- n = (w - mathfloor(this.e / LOG_BASE)) * LOG_BASE;
887
- w = d[w];
888
- if (w) for (; w % 10 == 0; w /= 10) n--;
889
- if (n < 0) n = 0;
890
- }
891
- return n;
892
- };
893
- P.dividedBy = P.div = function(y) {
894
- return divide(this, new this.constructor(y));
895
- };
896
- P.dividedToIntegerBy = P.divToInt = function(y) {
897
- var x = this, Ctor = x.constructor;
898
- return finalise(divide(x, new Ctor(y), 0, 1, 1), Ctor.precision, Ctor.rounding);
899
- };
900
- P.equals = P.eq = function(y) {
901
- return this.cmp(y) === 0;
902
- };
903
- P.floor = function() {
904
- return finalise(new this.constructor(this), this.e + 1, 3);
905
- };
906
- P.greaterThan = P.gt = function(y) {
907
- return this.cmp(y) > 0;
908
- };
909
- P.greaterThanOrEqualTo = P.gte = function(y) {
910
- var k = this.cmp(y);
911
- return k == 1 || k === 0;
912
- };
913
- P.hyperbolicCosine = P.cosh = function() {
914
- var k, n, pr, rm, len, x = this, Ctor = x.constructor, one = new Ctor(1);
915
- if (!x.isFinite()) return new Ctor(x.s ? 1 / 0 : NaN);
916
- if (x.isZero()) return one;
917
- pr = Ctor.precision;
918
- rm = Ctor.rounding;
919
- Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
920
- Ctor.rounding = 1;
921
- len = x.d.length;
922
- if (len < 32) {
923
- k = Math.ceil(len / 3);
924
- n = (1 / tinyPow(4, k)).toString();
925
- } else {
926
- k = 16;
927
- n = "2.3283064365386962890625e-10";
928
- }
929
- x = taylorSeries(Ctor, 1, x.times(n), new Ctor(1), true);
930
- var cosh2_x, i = k, d8 = new Ctor(8);
931
- for (; i--;) {
932
- cosh2_x = x.times(x);
933
- x = one.minus(cosh2_x.times(d8.minus(cosh2_x.times(d8))));
934
- }
935
- return finalise(x, Ctor.precision = pr, Ctor.rounding = rm, true);
936
- };
937
- P.hyperbolicSine = P.sinh = function() {
938
- var k, pr, rm, len, x = this, Ctor = x.constructor;
939
- if (!x.isFinite() || x.isZero()) return new Ctor(x);
940
- pr = Ctor.precision;
941
- rm = Ctor.rounding;
942
- Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
943
- Ctor.rounding = 1;
944
- len = x.d.length;
945
- if (len < 3) x = taylorSeries(Ctor, 2, x, x, true);
946
- else {
947
- k = 1.4 * Math.sqrt(len);
948
- k = k > 16 ? 16 : k | 0;
949
- x = x.times(1 / tinyPow(5, k));
950
- x = taylorSeries(Ctor, 2, x, x, true);
951
- var sinh2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20);
952
- for (; k--;) {
953
- sinh2_x = x.times(x);
954
- x = x.times(d5.plus(sinh2_x.times(d16.times(sinh2_x).plus(d20))));
777
+ }
778
+ /** Finite decimal subset used by HVec3, Robust and CSG.
779
+ * Constructors preserve input digits; arithmetic rounds to configured significant digits.
780
+ * Instances are immutable. Unsupported/non-finite inputs and division by zero throw.
781
+ */
782
+ var Decimal = class Decimal {
783
+ static precision = 20;
784
+ static rounding = 4;
785
+ static ROUND_UP = 0;
786
+ static ROUND_DOWN = 1;
787
+ static ROUND_CEIL = 2;
788
+ static ROUND_FLOOR = 3;
789
+ static ROUND_HALF_UP = 4;
790
+ static ROUND_HALF_DOWN = 5;
791
+ static ROUND_HALF_EVEN = 6;
792
+ static ROUND_HALF_CEIL = 7;
793
+ static ROUND_HALF_FLOOR = 8;
794
+ coefficient;
795
+ exponent;
796
+ constructor(value = 0) {
797
+ if (value instanceof Decimal) {
798
+ this.coefficient = value.coefficient;
799
+ this.exponent = value.exponent;
800
+ return;
955
801
  }
802
+ const text = String(value).trim();
803
+ const match = /^([+-]?)(?:(\d+)(?:\.(\d*))?|\.(\d+))(?:e([+-]?\d+))?$/i.exec(text);
804
+ if (!match) throw new RangeError("Expected a finite decimal value");
805
+ const fraction = match[3] ?? match[4] ?? "";
806
+ const raw = ((match[2] ?? "") + fraction).replace(/^0+/, "") || "0";
807
+ const exponent = Number(match[5] ?? 0) - fraction.length;
808
+ if (raw.length > MAX_INPUT_DIGITS || !Number.isSafeInteger(exponent) || Math.abs(exponent) > MAX_EXPONENT) throw new RangeError("Decimal input exceeds the supported range");
809
+ this.coefficient = BigInt((match[1] === "-" ? "-" : "") + raw);
810
+ this.exponent = exponent;
811
+ this.normalize();
812
+ }
813
+ static set(config) {
814
+ const p = config.precision ?? this.precision, r = config.rounding ?? this.rounding;
815
+ if (!Number.isInteger(p) || p < 1 || p > 1e3) throw new RangeError("Decimal precision must be an integer from 1 to 1000");
816
+ if (!Number.isInteger(r) || r < 0 || r > 8) throw new RangeError("Decimal rounding must be an integer from 0 to 8");
817
+ this.precision = p;
818
+ this.rounding = r;
819
+ return this;
956
820
  }
957
- Ctor.precision = pr;
958
- Ctor.rounding = rm;
959
- return finalise(x, pr, rm, true);
960
- };
961
- P.hyperbolicTangent = P.tanh = function() {
962
- var pr, rm, x = this, Ctor = x.constructor;
963
- if (!x.isFinite()) return new Ctor(x.s);
964
- if (x.isZero()) return new Ctor(x);
965
- pr = Ctor.precision;
966
- rm = Ctor.rounding;
967
- Ctor.precision = pr + 7;
968
- Ctor.rounding = 1;
969
- return divide(x.sinh(), x.cosh(), Ctor.precision = pr, Ctor.rounding = rm);
970
- };
971
- P.inverseCosine = P.acos = function() {
972
- var x = this, Ctor = x.constructor, k = x.abs().cmp(1), pr = Ctor.precision, rm = Ctor.rounding;
973
- if (k !== -1) return k === 0 ? x.isNeg() ? getPi(Ctor, pr, rm) : new Ctor(0) : new Ctor(NaN);
974
- if (x.isZero()) return getPi(Ctor, pr + 4, rm).times(.5);
975
- Ctor.precision = pr + 6;
976
- Ctor.rounding = 1;
977
- x = new Ctor(1).minus(x).div(x.plus(1)).sqrt().atan();
978
- Ctor.precision = pr;
979
- Ctor.rounding = rm;
980
- return x.times(2);
981
- };
982
- P.inverseHyperbolicCosine = P.acosh = function() {
983
- var pr, rm, x = this, Ctor = x.constructor;
984
- if (x.lte(1)) return new Ctor(x.eq(1) ? 0 : NaN);
985
- if (!x.isFinite()) return new Ctor(x);
986
- pr = Ctor.precision;
987
- rm = Ctor.rounding;
988
- Ctor.precision = pr + Math.max(Math.abs(x.e), x.sd()) + 4;
989
- Ctor.rounding = 1;
990
- external = false;
991
- x = x.times(x).minus(1).sqrt().plus(x);
992
- external = true;
993
- Ctor.precision = pr;
994
- Ctor.rounding = rm;
995
- return x.ln();
996
- };
997
- P.inverseHyperbolicSine = P.asinh = function() {
998
- var pr, rm, x = this, Ctor = x.constructor;
999
- if (!x.isFinite() || x.isZero()) return new Ctor(x);
1000
- pr = Ctor.precision;
1001
- rm = Ctor.rounding;
1002
- Ctor.precision = pr + 2 * Math.max(Math.abs(x.e), x.sd()) + 6;
1003
- Ctor.rounding = 1;
1004
- external = false;
1005
- x = x.times(x).plus(1).sqrt().plus(x);
1006
- external = true;
1007
- Ctor.precision = pr;
1008
- Ctor.rounding = rm;
1009
- return x.ln();
1010
- };
1011
- P.inverseHyperbolicTangent = P.atanh = function() {
1012
- var pr, rm, wpr, xsd, x = this, Ctor = x.constructor;
1013
- if (!x.isFinite()) return new Ctor(NaN);
1014
- if (x.e >= 0) return new Ctor(x.abs().eq(1) ? x.s / 0 : x.isZero() ? x : NaN);
1015
- pr = Ctor.precision;
1016
- rm = Ctor.rounding;
1017
- xsd = x.sd();
1018
- if (Math.max(xsd, pr) < 2 * -x.e - 1) return finalise(new Ctor(x), pr, rm, true);
1019
- Ctor.precision = wpr = xsd - x.e;
1020
- x = divide(x.plus(1), new Ctor(1).minus(x), wpr + pr, 1);
1021
- Ctor.precision = pr + 4;
1022
- Ctor.rounding = 1;
1023
- x = x.ln();
1024
- Ctor.precision = pr;
1025
- Ctor.rounding = rm;
1026
- return x.times(.5);
1027
- };
1028
- P.inverseSine = P.asin = function() {
1029
- var halfPi, k, pr, rm, x = this, Ctor = x.constructor;
1030
- if (x.isZero()) return new Ctor(x);
1031
- k = x.abs().cmp(1);
1032
- pr = Ctor.precision;
1033
- rm = Ctor.rounding;
1034
- if (k !== -1) {
1035
- if (k === 0) {
1036
- halfPi = getPi(Ctor, pr + 4, rm).times(.5);
1037
- halfPi.s = x.s;
1038
- return halfPi;
1039
- }
1040
- return new Ctor(NaN);
1041
- }
1042
- Ctor.precision = pr + 6;
1043
- Ctor.rounding = 1;
1044
- x = x.div(new Ctor(1).minus(x.times(x)).sqrt().plus(1)).atan();
1045
- Ctor.precision = pr;
1046
- Ctor.rounding = rm;
1047
- return x.times(2);
1048
- };
1049
- P.inverseTangent = P.atan = function() {
1050
- var i, j, k, n, px, t, r, wpr, x2, x = this, Ctor = x.constructor, pr = Ctor.precision, rm = Ctor.rounding;
1051
- if (!x.isFinite()) {
1052
- if (!x.s) return new Ctor(NaN);
1053
- if (pr + 4 <= PI_PRECISION) {
1054
- r = getPi(Ctor, pr + 4, rm).times(.5);
1055
- r.s = x.s;
1056
- return r;
1057
- }
1058
- } else if (x.isZero()) return new Ctor(x);
1059
- else if (x.abs().eq(1) && pr + 4 <= PI_PRECISION) {
1060
- r = getPi(Ctor, pr + 4, rm).times(.25);
1061
- r.s = x.s;
1062
- return r;
1063
- }
1064
- Ctor.precision = wpr = pr + 10;
1065
- Ctor.rounding = 1;
1066
- k = Math.min(28, wpr / LOG_BASE + 2 | 0);
1067
- for (i = k; i; --i) x = x.div(x.times(x).plus(1).sqrt().plus(1));
1068
- external = false;
1069
- j = Math.ceil(wpr / LOG_BASE);
1070
- n = 1;
1071
- x2 = x.times(x);
1072
- r = new Ctor(x);
1073
- px = x;
1074
- for (; i !== -1;) {
1075
- px = px.times(x2);
1076
- t = r.minus(px.div(n += 2));
1077
- px = px.times(x2);
1078
- r = t.plus(px.div(n += 2));
1079
- if (r.d[j] !== void 0) for (i = j; r.d[i] === t.d[i] && i--;);
1080
- }
1081
- if (k) r = r.times(2 << k - 1);
1082
- external = true;
1083
- return finalise(r, Ctor.precision = pr, Ctor.rounding = rm, true);
1084
- };
1085
- P.isFinite = function() {
1086
- return !!this.d;
1087
- };
1088
- P.isInteger = P.isInt = function() {
1089
- return !!this.d && mathfloor(this.e / LOG_BASE) > this.d.length - 2;
1090
- };
1091
- P.isNaN = function() {
1092
- return !this.s;
1093
- };
1094
- P.isNegative = P.isNeg = function() {
1095
- return this.s < 0;
1096
- };
1097
- P.isPositive = P.isPos = function() {
1098
- return this.s > 0;
1099
- };
1100
- P.isZero = function() {
1101
- return !!this.d && this.d[0] === 0;
1102
- };
1103
- P.lessThan = P.lt = function(y) {
1104
- return this.cmp(y) < 0;
1105
- };
1106
- P.lessThanOrEqualTo = P.lte = function(y) {
1107
- return this.cmp(y) < 1;
1108
- };
1109
- P.logarithm = P.log = function(base) {
1110
- var isBase10, d, denominator, k, inf, num, sd, r, arg = this, Ctor = arg.constructor, pr = Ctor.precision, rm = Ctor.rounding, guard = 5;
1111
- if (base == null) {
1112
- base = new Ctor(10);
1113
- isBase10 = true;
1114
- } else {
1115
- base = new Ctor(base);
1116
- d = base.d;
1117
- if (base.s < 0 || !d || !d[0] || base.eq(1)) return new Ctor(NaN);
1118
- isBase10 = base.eq(10);
1119
- }
1120
- d = arg.d;
1121
- if (arg.s < 0 || !d || !d[0] || arg.eq(1)) return new Ctor(d && !d[0] ? -1 / 0 : arg.s != 1 ? NaN : d ? 0 : 1 / 0);
1122
- if (isBase10) {
1123
- if (d.length > 1) inf = true;
1124
- else {
1125
- for (k = d[0]; k % 10 === 0;) k /= 10;
1126
- inf = k !== 1;
1127
- }
821
+ static config(config) {
822
+ return this.set(config);
1128
823
  }
1129
- external = false;
1130
- sd = pr + guard;
1131
- num = naturalLogarithm(arg, sd);
1132
- denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
1133
- r = divide(num, denominator, sd, 1);
1134
- if (checkRoundingDigits(r.d, k = pr, rm)) do {
1135
- sd += 10;
1136
- num = naturalLogarithm(arg, sd);
1137
- denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
1138
- r = divide(num, denominator, sd, 1);
1139
- if (!inf) {
1140
- if (+digitsToString(r.d).slice(k + 1, k + 15) + 1 == 1e14) r = finalise(r, pr + 1, 0);
1141
- break;
1142
- }
1143
- } while (checkRoundingDigits(r.d, k += 10, rm));
1144
- external = true;
1145
- return finalise(r, pr, rm);
1146
- };
1147
- P.minus = P.sub = function(y) {
1148
- var d, e, i, j, k, len, pr, rm, xd, xe, xLTy, yd, x = this, Ctor = x.constructor;
1149
- y = new Ctor(y);
1150
- if (!x.d || !y.d) {
1151
- if (!x.s || !y.s) y = new Ctor(NaN);
1152
- else if (x.d) y.s = -y.s;
1153
- else y = new Ctor(y.d || x.s !== y.s ? x : NaN);
1154
- return y;
1155
- }
1156
- if (x.s != y.s) {
1157
- y.s = -y.s;
1158
- return x.plus(y);
1159
- }
1160
- xd = x.d;
1161
- yd = y.d;
1162
- pr = Ctor.precision;
1163
- rm = Ctor.rounding;
1164
- if (!xd[0] || !yd[0]) {
1165
- if (yd[0]) y.s = -y.s;
1166
- else if (xd[0]) y = new Ctor(x);
1167
- else return new Ctor(rm === 3 ? -0 : 0);
1168
- return external ? finalise(y, pr, rm) : y;
1169
- }
1170
- e = mathfloor(y.e / LOG_BASE);
1171
- xe = mathfloor(x.e / LOG_BASE);
1172
- xd = xd.slice();
1173
- k = xe - e;
1174
- if (k) {
1175
- xLTy = k < 0;
1176
- if (xLTy) {
1177
- d = xd;
1178
- k = -k;
1179
- len = yd.length;
1180
- } else {
1181
- d = yd;
1182
- e = xe;
1183
- len = xd.length;
1184
- }
1185
- i = Math.max(Math.ceil(pr / LOG_BASE), len) + 2;
1186
- if (k > i) {
1187
- k = i;
1188
- d.length = 1;
1189
- }
1190
- d.reverse();
1191
- for (i = k; i--;) d.push(0);
1192
- d.reverse();
1193
- } else {
1194
- i = xd.length;
1195
- len = yd.length;
1196
- xLTy = i < len;
1197
- if (xLTy) len = i;
1198
- for (i = 0; i < len; i++) if (xd[i] != yd[i]) {
1199
- xLTy = xd[i] < yd[i];
1200
- break;
1201
- }
1202
- k = 0;
1203
- }
1204
- if (xLTy) {
1205
- d = xd;
1206
- xd = yd;
1207
- yd = d;
1208
- y.s = -y.s;
1209
- }
1210
- len = xd.length;
1211
- for (i = yd.length - len; i > 0; --i) xd[len++] = 0;
1212
- for (i = yd.length; i > k;) {
1213
- if (xd[--i] < yd[i]) {
1214
- for (j = i; j && xd[--j] === 0;) xd[j] = BASE - 1;
1215
- --xd[j];
1216
- xd[i] += BASE;
1217
- }
1218
- xd[i] -= yd[i];
1219
- }
1220
- for (; xd[--len] === 0;) xd.pop();
1221
- for (; xd[0] === 0; xd.shift()) --e;
1222
- if (!xd[0]) return new Ctor(rm === 3 ? -0 : 0);
1223
- y.d = xd;
1224
- y.e = getBase10Exponent(xd, e);
1225
- return external ? finalise(y, pr, rm) : y;
1226
- };
1227
- P.modulo = P.mod = function(y) {
1228
- var q, x = this, Ctor = x.constructor;
1229
- y = new Ctor(y);
1230
- if (!x.d || !y.s || y.d && !y.d[0]) return new Ctor(NaN);
1231
- if (!y.d || x.d && !x.d[0]) return finalise(new Ctor(x), Ctor.precision, Ctor.rounding);
1232
- external = false;
1233
- if (Ctor.modulo == 9) {
1234
- q = divide(x, y.abs(), 0, 3, 1);
1235
- q.s *= y.s;
1236
- } else q = divide(x, y, 0, Ctor.modulo, 1);
1237
- q = q.times(y);
1238
- external = true;
1239
- return x.minus(q);
1240
- };
1241
- P.naturalExponential = P.exp = function() {
1242
- return naturalExponential(this);
1243
- };
1244
- P.naturalLogarithm = P.ln = function() {
1245
- return naturalLogarithm(this);
1246
- };
1247
- P.negated = P.neg = function() {
1248
- var x = new this.constructor(this);
1249
- x.s = -x.s;
1250
- return finalise(x);
1251
- };
1252
- P.plus = P.add = function(y) {
1253
- var carry, d, e, i, k, len, pr, rm, xd, yd, x = this, Ctor = x.constructor;
1254
- y = new Ctor(y);
1255
- if (!x.d || !y.d) {
1256
- if (!x.s || !y.s) y = new Ctor(NaN);
1257
- else if (!x.d) y = new Ctor(y.d || x.s === y.s ? x : NaN);
1258
- return y;
1259
- }
1260
- if (x.s != y.s) {
1261
- y.s = -y.s;
1262
- return x.minus(y);
1263
- }
1264
- xd = x.d;
1265
- yd = y.d;
1266
- pr = Ctor.precision;
1267
- rm = Ctor.rounding;
1268
- if (!xd[0] || !yd[0]) {
1269
- if (!yd[0]) y = new Ctor(x);
1270
- return external ? finalise(y, pr, rm) : y;
1271
- }
1272
- k = mathfloor(x.e / LOG_BASE);
1273
- e = mathfloor(y.e / LOG_BASE);
1274
- xd = xd.slice();
1275
- i = k - e;
1276
- if (i) {
1277
- if (i < 0) {
1278
- d = xd;
1279
- i = -i;
1280
- len = yd.length;
1281
- } else {
1282
- d = yd;
1283
- e = k;
1284
- len = xd.length;
1285
- }
1286
- k = Math.ceil(pr / LOG_BASE);
1287
- len = k > len ? k + 1 : len + 1;
1288
- if (i > len) {
1289
- i = len;
1290
- d.length = 1;
1291
- }
1292
- d.reverse();
1293
- for (; i--;) d.push(0);
1294
- d.reverse();
1295
- }
1296
- len = xd.length;
1297
- i = yd.length;
1298
- if (len - i < 0) {
1299
- i = len;
1300
- d = yd;
1301
- yd = xd;
1302
- xd = d;
1303
- }
1304
- for (carry = 0; i;) {
1305
- carry = (xd[--i] = xd[i] + yd[i] + carry) / BASE | 0;
1306
- xd[i] %= BASE;
1307
- }
1308
- if (carry) {
1309
- xd.unshift(carry);
1310
- ++e;
1311
- }
1312
- for (len = xd.length; xd[--len] == 0;) xd.pop();
1313
- y.d = xd;
1314
- y.e = getBase10Exponent(xd, e);
1315
- return external ? finalise(y, pr, rm) : y;
1316
- };
1317
- P.precision = P.sd = function(z) {
1318
- var k, x = this;
1319
- if (z !== void 0 && z !== !!z && z !== 1 && z !== 0) throw Error(invalidArgument + z);
1320
- if (x.d) {
1321
- k = getPrecision(x.d);
1322
- if (z && x.e + 1 > k) k = x.e + 1;
1323
- } else k = NaN;
1324
- return k;
1325
- };
1326
- P.round = function() {
1327
- var x = this, Ctor = x.constructor;
1328
- return finalise(new Ctor(x), x.e + 1, Ctor.rounding);
1329
- };
1330
- P.sine = P.sin = function() {
1331
- var pr, rm, x = this, Ctor = x.constructor;
1332
- if (!x.isFinite()) return new Ctor(NaN);
1333
- if (x.isZero()) return new Ctor(x);
1334
- pr = Ctor.precision;
1335
- rm = Ctor.rounding;
1336
- Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
1337
- Ctor.rounding = 1;
1338
- x = sine(Ctor, toLessThanHalfPi(Ctor, x));
1339
- Ctor.precision = pr;
1340
- Ctor.rounding = rm;
1341
- return finalise(quadrant > 2 ? x.neg() : x, pr, rm, true);
1342
- };
1343
- P.squareRoot = P.sqrt = function() {
1344
- var m, n, sd, r, rep, t, x = this, d = x.d, e = x.e, s = x.s, Ctor = x.constructor;
1345
- if (s !== 1 || !d || !d[0]) return new Ctor(!s || s < 0 && (!d || d[0]) ? NaN : d ? x : 1 / 0);
1346
- external = false;
1347
- s = Math.sqrt(+x);
1348
- if (s == 0 || s == 1 / 0) {
1349
- n = digitsToString(d);
1350
- if ((n.length + e) % 2 == 0) n += "0";
1351
- s = Math.sqrt(n);
1352
- e = mathfloor((e + 1) / 2) - (e < 0 || e % 2);
1353
- if (s == 1 / 0) n = "5e" + e;
1354
- else {
1355
- n = s.toExponential();
1356
- n = n.slice(0, n.indexOf("e") + 1) + e;
824
+ static isDecimal(value) {
825
+ return value instanceof Decimal;
826
+ }
827
+ normalize() {
828
+ if (this.coefficient === 0n) {
829
+ this.exponent = 0;
830
+ return;
1357
831
  }
1358
- r = new Ctor(n);
1359
- } else r = new Ctor(s.toString());
1360
- sd = (e = Ctor.precision) + 3;
1361
- for (;;) {
1362
- t = r;
1363
- r = t.plus(divide(x, t, sd + 2, 1)).times(.5);
1364
- if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
1365
- n = n.slice(sd - 3, sd + 1);
1366
- if (n == "9999" || !rep && n == "4999") {
1367
- if (!rep) {
1368
- finalise(t, e + 1, 0);
1369
- if (t.times(t).eq(x)) {
1370
- r = t;
1371
- break;
1372
- }
1373
- }
1374
- sd += 4;
1375
- rep = 1;
1376
- } else {
1377
- if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
1378
- finalise(r, e + 1, 1);
1379
- m = !r.times(r).eq(x);
1380
- }
1381
- break;
832
+ const text = abs(this.coefficient).toString();
833
+ const trimmed = text.replace(/0+$/, "");
834
+ this.exponent += text.length - trimmed.length;
835
+ this.coefficient = BigInt(trimmed) * (this.coefficient < 0n ? -1n : 1n);
836
+ if (Math.abs(this.exponent) > MAX_EXPONENT) throw new RangeError("Decimal result exceeds the supported exponent range");
837
+ }
838
+ static make(coefficient, exponent, round = true, sticky = false) {
839
+ const result = Object.create(Decimal.prototype);
840
+ if (round && coefficient !== 0n) {
841
+ const excess = digits(coefficient) - Decimal.precision;
842
+ if (excess > 0) {
843
+ const divisor = power(excess), sign = coefficient < 0n ? -1n : 1n;
844
+ const remainder = abs(coefficient % divisor);
845
+ let q = coefficient / divisor;
846
+ if (increment(q, remainder, divisor, sign, Decimal.rounding, sticky)) q += sign;
847
+ coefficient = q;
848
+ exponent += excess;
1382
849
  }
1383
850
  }
851
+ result.coefficient = coefficient;
852
+ result.exponent = exponent;
853
+ result.normalize();
854
+ return result;
1384
855
  }
1385
- external = true;
1386
- return finalise(r, e, Ctor.rounding, m);
1387
- };
1388
- P.tangent = P.tan = function() {
1389
- var pr, rm, x = this, Ctor = x.constructor;
1390
- if (!x.isFinite()) return new Ctor(NaN);
1391
- if (x.isZero()) return new Ctor(x);
1392
- pr = Ctor.precision;
1393
- rm = Ctor.rounding;
1394
- Ctor.precision = pr + 10;
1395
- Ctor.rounding = 1;
1396
- x = x.sin();
1397
- x.s = 1;
1398
- x = divide(x, new Ctor(1).minus(x.times(x)).sqrt(), pr + 10, 0);
1399
- Ctor.precision = pr;
1400
- Ctor.rounding = rm;
1401
- return finalise(quadrant == 2 || quadrant == 4 ? x.neg() : x, pr, rm, true);
1402
- };
1403
- P.times = P.mul = function(y) {
1404
- var carry, e, i, k, r, rL, t, xdL, ydL, x = this, Ctor = x.constructor, xd = x.d, yd = (y = new Ctor(y)).d;
1405
- y.s *= x.s;
1406
- if (!xd || !xd[0] || !yd || !yd[0]) return new Ctor(!y.s || xd && !xd[0] && !yd || yd && !yd[0] && !xd ? NaN : !xd || !yd ? y.s / 0 : y.s * 0);
1407
- e = mathfloor(x.e / LOG_BASE) + mathfloor(y.e / LOG_BASE);
1408
- xdL = xd.length;
1409
- ydL = yd.length;
1410
- if (xdL < ydL) {
1411
- r = xd;
1412
- xd = yd;
1413
- yd = r;
1414
- rL = xdL;
1415
- xdL = ydL;
1416
- ydL = rL;
1417
- }
1418
- r = [];
1419
- rL = xdL + ydL;
1420
- for (i = rL; i--;) r.push(0);
1421
- for (i = ydL; --i >= 0;) {
1422
- carry = 0;
1423
- for (k = xdL + i; k > i;) {
1424
- t = r[k] + yd[i] * xd[k - i - 1] + carry;
1425
- r[k--] = t % BASE | 0;
1426
- carry = t / BASE | 0;
1427
- }
1428
- r[k] = (r[k] + carry) % BASE | 0;
1429
- }
1430
- for (; !r[--rL];) r.pop();
1431
- if (carry) ++e;
1432
- else r.shift();
1433
- y.d = r;
1434
- y.e = getBase10Exponent(r, e);
1435
- return external ? finalise(y, Ctor.precision, Ctor.rounding) : y;
1436
- };
1437
- P.toBinary = function(sd, rm) {
1438
- return toStringBinary(this, 2, sd, rm);
1439
- };
1440
- P.toDecimalPlaces = P.toDP = function(dp, rm) {
1441
- var x = this, Ctor = x.constructor;
1442
- x = new Ctor(x);
1443
- if (dp === void 0) return x;
1444
- checkInt32(dp, 0, MAX_DIGITS);
1445
- if (rm === void 0) rm = Ctor.rounding;
1446
- else checkInt32(rm, 0, 8);
1447
- return finalise(x, dp + x.e + 1, rm);
1448
- };
1449
- P.toExponential = function(dp, rm) {
1450
- var str, x = this, Ctor = x.constructor;
1451
- if (dp === void 0) str = finiteToString(x, true);
1452
- else {
1453
- checkInt32(dp, 0, MAX_DIGITS);
1454
- if (rm === void 0) rm = Ctor.rounding;
1455
- else checkInt32(rm, 0, 8);
1456
- x = finalise(new Ctor(x), dp + 1, rm);
1457
- str = finiteToString(x, true, dp + 1);
1458
- }
1459
- return x.isNeg() && !x.isZero() ? "-" + str : str;
1460
- };
1461
- P.toFixed = function(dp, rm) {
1462
- var str, y, x = this, Ctor = x.constructor;
1463
- if (dp === void 0) str = finiteToString(x);
1464
- else {
1465
- checkInt32(dp, 0, MAX_DIGITS);
1466
- if (rm === void 0) rm = Ctor.rounding;
1467
- else checkInt32(rm, 0, 8);
1468
- y = finalise(new Ctor(x), dp + x.e + 1, rm);
1469
- str = finiteToString(y, false, dp + y.e + 1);
1470
- }
1471
- return x.isNeg() && !x.isZero() ? "-" + str : str;
1472
- };
1473
- P.toFraction = function(maxD) {
1474
- var d, d0, d1, d2, e, k, n, n0, n1, pr, q, r, x = this, xd = x.d, Ctor = x.constructor;
1475
- if (!xd) return new Ctor(x);
1476
- n1 = d0 = new Ctor(1);
1477
- d1 = n0 = new Ctor(0);
1478
- d = new Ctor(d1);
1479
- e = d.e = getPrecision(xd) - x.e - 1;
1480
- k = e % LOG_BASE;
1481
- d.d[0] = mathpow(10, k < 0 ? LOG_BASE + k : k);
1482
- if (maxD == null) maxD = e > 0 ? d : n1;
1483
- else {
1484
- n = new Ctor(maxD);
1485
- if (!n.isInt() || n.lt(n1)) throw Error(invalidArgument + n);
1486
- maxD = n.gt(d) ? e > 0 ? d : n1 : n;
1487
- }
1488
- external = false;
1489
- n = new Ctor(digitsToString(xd));
1490
- pr = Ctor.precision;
1491
- Ctor.precision = e = xd.length * LOG_BASE * 2;
1492
- for (;;) {
1493
- q = divide(n, d, 0, 1, 1);
1494
- d2 = d0.plus(q.times(d1));
1495
- if (d2.cmp(maxD) == 1) break;
1496
- d0 = d1;
1497
- d1 = d2;
1498
- d2 = n1;
1499
- n1 = n0.plus(q.times(d2));
1500
- n0 = d2;
1501
- d2 = d;
1502
- d = n.minus(q.times(d2));
1503
- n = d2;
1504
- }
1505
- d2 = divide(maxD.minus(d0), d1, 0, 1, 1);
1506
- n0 = n0.plus(d2.times(n1));
1507
- d0 = d0.plus(d2.times(d1));
1508
- n0.s = n1.s = x.s;
1509
- r = divide(n1, d1, e, 1).minus(x).abs().cmp(divide(n0, d0, e, 1).minus(x).abs()) < 1 ? [n1, d1] : [n0, d0];
1510
- Ctor.precision = pr;
1511
- external = true;
1512
- return r;
1513
- };
1514
- P.toHexadecimal = P.toHex = function(sd, rm) {
1515
- return toStringBinary(this, 16, sd, rm);
1516
- };
1517
- P.toNearest = function(y, rm) {
1518
- var x = this, Ctor = x.constructor;
1519
- x = new Ctor(x);
1520
- if (y == null) {
1521
- if (!x.d) return x;
1522
- y = new Ctor(1);
1523
- rm = Ctor.rounding;
1524
- } else {
1525
- y = new Ctor(y);
1526
- if (rm === void 0) rm = Ctor.rounding;
1527
- else checkInt32(rm, 0, 8);
1528
- if (!x.d) return y.s ? x : y;
1529
- if (!y.d) {
1530
- if (y.s) y.s = x.s;
1531
- return y;
856
+ add(value) {
857
+ const b = new Decimal(value), e = Math.min(this.exponent, b.exponent);
858
+ return Decimal.make(this.coefficient * power(this.exponent - e) + b.coefficient * power(b.exponent - e), e);
859
+ }
860
+ plus(value) {
861
+ return this.add(value);
862
+ }
863
+ sub(value) {
864
+ return this.add(new Decimal(value).negated());
865
+ }
866
+ minus(value) {
867
+ return this.sub(value);
868
+ }
869
+ mul(value) {
870
+ const b = new Decimal(value);
871
+ return Decimal.make(this.coefficient * b.coefficient, this.exponent + b.exponent);
872
+ }
873
+ times(value) {
874
+ return this.mul(value);
875
+ }
876
+ div(value) {
877
+ const b = new Decimal(value);
878
+ if (b.isZero()) throw new RangeError("Division by zero");
879
+ if (this.isZero()) return new Decimal(0);
880
+ const scale = Decimal.precision + 2 + digits(b.coefficient) - digits(this.coefficient);
881
+ const numerator = this.coefficient * power(Math.max(0, scale));
882
+ const denominator = b.coefficient * power(Math.max(0, -scale));
883
+ return Decimal.make(numerator / denominator, this.exponent - b.exponent - scale, true, numerator % denominator !== 0n);
884
+ }
885
+ dividedBy(value) {
886
+ return this.div(value);
887
+ }
888
+ sqrt() {
889
+ if (this.coefficient < 0n) throw new RangeError("Square root requires a nonnegative value");
890
+ if (this.isZero()) return new Decimal(0);
891
+ let c = this.coefficient, e = this.exponent;
892
+ if (e % 2 !== 0) {
893
+ c *= 10n;
894
+ e--;
1532
895
  }
896
+ const scale = Math.max(0, Decimal.precision + 2 - Math.ceil(digits(c) / 2));
897
+ const radicand = c * power(2 * scale), root = integerSqrt(radicand);
898
+ return Decimal.make(root, e / 2 - scale, true, root * root !== radicand);
1533
899
  }
1534
- if (y.d[0]) {
1535
- external = false;
1536
- x = divide(x, y, 0, rm, 1).times(y);
1537
- external = true;
1538
- finalise(x);
1539
- } else {
1540
- y.s = x.s;
1541
- x = y;
900
+ negated() {
901
+ return Decimal.make(-this.coefficient, this.exponent, false);
1542
902
  }
1543
- return x;
1544
- };
1545
- P.toNumber = function() {
1546
- return +this;
1547
- };
1548
- P.toOctal = function(sd, rm) {
1549
- return toStringBinary(this, 8, sd, rm);
1550
- };
1551
- P.toPower = P.pow = function(y) {
1552
- var e, k, pr, r, rm, s, x = this, Ctor = x.constructor, yn = +(y = new Ctor(y));
1553
- if (!x.d || !y.d || !x.d[0] || !y.d[0]) return new Ctor(mathpow(+x, yn));
1554
- x = new Ctor(x);
1555
- if (x.eq(1)) return x;
1556
- pr = Ctor.precision;
1557
- rm = Ctor.rounding;
1558
- if (y.eq(1)) return finalise(x, pr, rm);
1559
- e = mathfloor(y.e / LOG_BASE);
1560
- if (e >= y.d.length - 1 && (k = yn < 0 ? -yn : yn) <= MAX_SAFE_INTEGER) {
1561
- r = intPow(Ctor, x, k, pr);
1562
- return y.s < 0 ? new Ctor(1).div(r) : finalise(r, pr, rm);
1563
- }
1564
- s = x.s;
1565
- if (s < 0) {
1566
- if (e < y.d.length - 1) return new Ctor(NaN);
1567
- if ((y.d[e] & 1) == 0) s = 1;
1568
- if (x.e == 0 && x.d[0] == 1 && x.d.length == 1) {
1569
- x.s = s;
1570
- return x;
1571
- }
903
+ neg() {
904
+ return this.negated();
1572
905
  }
1573
- k = mathpow(+x, yn);
1574
- e = k == 0 || !isFinite(k) ? mathfloor(yn * (Math.log("0." + digitsToString(x.d)) / Math.LN10 + x.e + 1)) : new Ctor(k + "").e;
1575
- if (e > Ctor.maxE + 1 || e < Ctor.minE - 1) return new Ctor(e > 0 ? s / 0 : 0);
1576
- external = false;
1577
- Ctor.rounding = x.s = 1;
1578
- k = Math.min(12, (e + "").length);
1579
- r = naturalExponential(y.times(naturalLogarithm(x, pr + k)), pr);
1580
- if (r.d) {
1581
- r = finalise(r, pr + 5, 1);
1582
- if (checkRoundingDigits(r.d, pr, rm)) {
1583
- e = pr + 10;
1584
- r = finalise(naturalExponential(y.times(naturalLogarithm(x, e + k)), e), e + 5, 1);
1585
- if (+digitsToString(r.d).slice(pr + 1, pr + 15) + 1 == 1e14) r = finalise(r, pr + 1, 0);
1586
- }
906
+ abs() {
907
+ return Decimal.make(abs(this.coefficient), this.exponent, false);
1587
908
  }
1588
- r.s = s;
1589
- external = true;
1590
- Ctor.rounding = rm;
1591
- return finalise(r, pr, rm);
1592
- };
1593
- P.toPrecision = function(sd, rm) {
1594
- var str, x = this, Ctor = x.constructor;
1595
- if (sd === void 0) str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
1596
- else {
1597
- checkInt32(sd, 1, MAX_DIGITS);
1598
- if (rm === void 0) rm = Ctor.rounding;
1599
- else checkInt32(rm, 0, 8);
1600
- x = finalise(new Ctor(x), sd, rm);
1601
- str = finiteToString(x, sd <= x.e || x.e <= Ctor.toExpNeg, sd);
1602
- }
1603
- return x.isNeg() && !x.isZero() ? "-" + str : str;
1604
- };
1605
- P.toSignificantDigits = P.toSD = function(sd, rm) {
1606
- var x = this, Ctor = x.constructor;
1607
- if (sd === void 0) {
1608
- sd = Ctor.precision;
1609
- rm = Ctor.rounding;
1610
- } else {
1611
- checkInt32(sd, 1, MAX_DIGITS);
1612
- if (rm === void 0) rm = Ctor.rounding;
1613
- else checkInt32(rm, 0, 8);
1614
- }
1615
- return finalise(new Ctor(x), sd, rm);
1616
- };
1617
- P.toString = function() {
1618
- var x = this, Ctor = x.constructor, str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
1619
- return x.isNeg() && !x.isZero() ? "-" + str : str;
1620
- };
1621
- P.truncated = P.trunc = function() {
1622
- return finalise(new this.constructor(this), this.e + 1, 1);
1623
- };
1624
- P.valueOf = P.toJSON = function() {
1625
- var x = this, Ctor = x.constructor, str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
1626
- return x.isNeg() ? "-" + str : str;
1627
- };
1628
- function digitsToString(d) {
1629
- var i, k, ws, indexOfLastWord = d.length - 1, str = "", w = d[0];
1630
- if (indexOfLastWord > 0) {
1631
- str += w;
1632
- for (i = 1; i < indexOfLastWord; i++) {
1633
- ws = d[i] + "";
1634
- k = LOG_BASE - ws.length;
1635
- if (k) str += getZeroString(k);
1636
- str += ws;
1637
- }
1638
- w = d[i];
1639
- ws = w + "";
1640
- k = LOG_BASE - ws.length;
1641
- if (k) str += getZeroString(k);
1642
- } else if (w === 0) return "0";
1643
- for (; w % 10 === 0;) w /= 10;
1644
- return str + w;
1645
- }
1646
- function checkInt32(i, min, max) {
1647
- if (i !== ~~i || i < min || i > max) throw Error(invalidArgument + i);
1648
- }
1649
- function checkRoundingDigits(d, i, rm, repeating) {
1650
- var di, k = d[0], r, rd;
1651
- for (; k >= 10; k /= 10) --i;
1652
- if (--i < 0) {
1653
- i += LOG_BASE;
1654
- di = 0;
1655
- } else {
1656
- di = Math.ceil((i + 1) / LOG_BASE);
1657
- i %= LOG_BASE;
1658
- }
1659
- k = mathpow(10, LOG_BASE - i);
1660
- rd = d[di] % k | 0;
1661
- if (repeating == null) {
1662
- if (i < 3) {
1663
- if (i == 0) rd = rd / 100 | 0;
1664
- else if (i == 1) rd = rd / 10 | 0;
1665
- r = rm < 4 && rd == 99999 || rm > 3 && rd == 49999 || rd == 5e4 || rd == 0;
1666
- } else r = (rm < 4 && rd + 1 == k || rm > 3 && rd + 1 == k / 2) && (d[di + 1] / k / 100 | 0) == mathpow(10, i - 2) - 1 || (rd == k / 2 || rd == 0) && (d[di + 1] / k / 100 | 0) == 0;
1667
- } else if (i < 4) {
1668
- if (i == 0) rd = rd / 1e3 | 0;
1669
- else if (i == 1) rd = rd / 100 | 0;
1670
- else if (i == 2) rd = rd / 10 | 0;
1671
- r = (repeating || rm < 4) && rd == 9999 || !repeating && rm > 3 && rd == 4999;
1672
- } else r = ((repeating || rm < 4) && rd + 1 == k || !repeating && rm > 3 && rd + 1 == k / 2) && (d[di + 1] / k / 1e3 | 0) == mathpow(10, i - 3) - 1;
1673
- return r;
1674
- }
1675
- function convertBase(str, baseIn, baseOut) {
1676
- var j, arr = [0], arrL, i = 0, strL = str.length;
1677
- for (; i < strL;) {
1678
- for (arrL = arr.length; arrL--;) arr[arrL] *= baseIn;
1679
- arr[0] += NUMERALS.indexOf(str.charAt(i++));
1680
- for (j = 0; j < arr.length; j++) if (arr[j] > baseOut - 1) {
1681
- if (arr[j + 1] === void 0) arr[j + 1] = 0;
1682
- arr[j + 1] += arr[j] / baseOut | 0;
1683
- arr[j] %= baseOut;
1684
- }
909
+ comparedTo(value) {
910
+ const b = new Decimal(value);
911
+ if (this.coefficient === b.coefficient && this.exponent === b.exponent) return 0;
912
+ if (this.coefficient < 0n && b.coefficient >= 0n) return -1;
913
+ if (this.coefficient >= 0n && b.coefficient < 0n) return 1;
914
+ if (this.isZero()) return b.isNegative() ? 1 : -1;
915
+ if (b.isZero()) return this.isNegative() ? -1 : 1;
916
+ const order = digits(this.coefficient) + this.exponent;
917
+ const otherOrder = digits(b.coefficient) + b.exponent;
918
+ if (order !== otherOrder) return (order < otherOrder ? -1 : 1) * (this.isNegative() ? -1 : 1);
919
+ const e = Math.min(this.exponent, b.exponent);
920
+ return this.coefficient * power(this.exponent - e) < b.coefficient * power(b.exponent - e) ? -1 : 1;
1685
921
  }
1686
- return arr.reverse();
1687
- }
1688
- function cosine(Ctor, x) {
1689
- var k, len, y;
1690
- if (x.isZero()) return x;
1691
- len = x.d.length;
1692
- if (len < 32) {
1693
- k = Math.ceil(len / 3);
1694
- y = (1 / tinyPow(4, k)).toString();
1695
- } else {
1696
- k = 16;
1697
- y = "2.3283064365386962890625e-10";
1698
- }
1699
- Ctor.precision += k;
1700
- x = taylorSeries(Ctor, 1, x.times(y), new Ctor(1));
1701
- for (var i = k; i--;) {
1702
- var cos2x = x.times(x);
1703
- x = cos2x.times(cos2x).minus(cos2x).times(8).plus(1);
1704
- }
1705
- Ctor.precision -= k;
1706
- return x;
1707
- }
1708
- var divide = (function() {
1709
- function multiplyInteger(x, k, base) {
1710
- var temp, carry = 0, i = x.length;
1711
- for (x = x.slice(); i--;) {
1712
- temp = x[i] * k + carry;
1713
- x[i] = temp % base | 0;
1714
- carry = temp / base | 0;
1715
- }
1716
- if (carry) x.unshift(carry);
1717
- return x;
1718
- }
1719
- function compare(a, b, aL, bL) {
1720
- var i, r;
1721
- if (aL != bL) r = aL > bL ? 1 : -1;
1722
- else for (i = r = 0; i < aL; i++) if (a[i] != b[i]) {
1723
- r = a[i] > b[i] ? 1 : -1;
1724
- break;
1725
- }
1726
- return r;
1727
- }
1728
- function subtract(a, b, aL, base) {
1729
- var i = 0;
1730
- for (; aL--;) {
1731
- a[aL] -= i;
1732
- i = a[aL] < b[aL] ? 1 : 0;
1733
- a[aL] = i * base + a[aL] - b[aL];
1734
- }
1735
- for (; !a[0] && a.length > 1;) a.shift();
1736
- }
1737
- return function(x, y, pr, rm, dp, base) {
1738
- var cmp, e, i, k, logBase, more, prod, prodL, q, qd, rem, remL, rem0, sd, t, xi, xL, yd0, yL, yz, Ctor = x.constructor, sign = x.s == y.s ? 1 : -1, xd = x.d, yd = y.d;
1739
- if (!xd || !xd[0] || !yd || !yd[0]) return new Ctor(!x.s || !y.s || (xd ? yd && xd[0] == yd[0] : !yd) ? NaN : xd && xd[0] == 0 || !yd ? sign * 0 : sign / 0);
1740
- if (base) {
1741
- logBase = 1;
1742
- e = x.e - y.e;
1743
- } else {
1744
- base = BASE;
1745
- logBase = LOG_BASE;
1746
- e = mathfloor(x.e / logBase) - mathfloor(y.e / logBase);
1747
- }
1748
- yL = yd.length;
1749
- xL = xd.length;
1750
- q = new Ctor(sign);
1751
- qd = q.d = [];
1752
- for (i = 0; yd[i] == (xd[i] || 0); i++);
1753
- if (yd[i] > (xd[i] || 0)) e--;
1754
- if (pr == null) {
1755
- sd = pr = Ctor.precision;
1756
- rm = Ctor.rounding;
1757
- } else if (dp) sd = pr + (x.e - y.e) + 1;
1758
- else sd = pr;
1759
- if (sd < 0) {
1760
- qd.push(1);
1761
- more = true;
1762
- } else {
1763
- sd = sd / logBase + 2 | 0;
1764
- i = 0;
1765
- if (yL == 1) {
1766
- k = 0;
1767
- yd = yd[0];
1768
- sd++;
1769
- for (; (i < xL || k) && sd--; i++) {
1770
- t = k * base + (xd[i] || 0);
1771
- qd[i] = t / yd | 0;
1772
- k = t % yd | 0;
1773
- }
1774
- more = k || i < xL;
1775
- } else {
1776
- k = base / (yd[0] + 1) | 0;
1777
- if (k > 1) {
1778
- yd = multiplyInteger(yd, k, base);
1779
- xd = multiplyInteger(xd, k, base);
1780
- yL = yd.length;
1781
- xL = xd.length;
1782
- }
1783
- xi = yL;
1784
- rem = xd.slice(0, yL);
1785
- remL = rem.length;
1786
- for (; remL < yL;) rem[remL++] = 0;
1787
- yz = yd.slice();
1788
- yz.unshift(0);
1789
- yd0 = yd[0];
1790
- if (yd[1] >= base / 2) ++yd0;
1791
- do {
1792
- k = 0;
1793
- cmp = compare(yd, rem, yL, remL);
1794
- if (cmp < 0) {
1795
- rem0 = rem[0];
1796
- if (yL != remL) rem0 = rem0 * base + (rem[1] || 0);
1797
- k = rem0 / yd0 | 0;
1798
- if (k > 1) {
1799
- if (k >= base) k = base - 1;
1800
- prod = multiplyInteger(yd, k, base);
1801
- prodL = prod.length;
1802
- remL = rem.length;
1803
- cmp = compare(prod, rem, prodL, remL);
1804
- if (cmp == 1) {
1805
- k--;
1806
- subtract(prod, yL < prodL ? yz : yd, prodL, base);
1807
- }
1808
- } else {
1809
- if (k == 0) cmp = k = 1;
1810
- prod = yd.slice();
1811
- }
1812
- prodL = prod.length;
1813
- if (prodL < remL) prod.unshift(0);
1814
- subtract(rem, prod, remL, base);
1815
- if (cmp == -1) {
1816
- remL = rem.length;
1817
- cmp = compare(yd, rem, yL, remL);
1818
- if (cmp < 1) {
1819
- k++;
1820
- subtract(rem, yL < remL ? yz : yd, remL, base);
1821
- }
1822
- }
1823
- remL = rem.length;
1824
- } else if (cmp === 0) {
1825
- k++;
1826
- rem = [0];
1827
- }
1828
- qd[i++] = k;
1829
- if (cmp && rem[0]) rem[remL++] = xd[xi] || 0;
1830
- else {
1831
- rem = [xd[xi]];
1832
- remL = 1;
1833
- }
1834
- } while ((xi++ < xL || rem[0] !== void 0) && sd--);
1835
- more = rem[0] !== void 0;
1836
- }
1837
- if (!qd[0]) qd.shift();
1838
- }
1839
- if (logBase == 1) {
1840
- q.e = e;
1841
- inexact = more;
1842
- } else {
1843
- for (i = 1, k = qd[0]; k >= 10; k /= 10) i++;
1844
- q.e = i + e * logBase - 1;
1845
- finalise(q, dp ? pr + q.e + 1 : pr, rm, more);
1846
- }
1847
- return q;
1848
- };
1849
- })();
1850
- function finalise(x, sd, rm, isTruncated) {
1851
- var digits, i, j, k, rd, roundUp, w, xd, xdi, Ctor = x.constructor;
1852
- out: if (sd != null) {
1853
- xd = x.d;
1854
- if (!xd) return x;
1855
- for (digits = 1, k = xd[0]; k >= 10; k /= 10) digits++;
1856
- i = sd - digits;
1857
- if (i < 0) {
1858
- i += LOG_BASE;
1859
- j = sd;
1860
- w = xd[xdi = 0];
1861
- rd = w / mathpow(10, digits - j - 1) % 10 | 0;
1862
- } else {
1863
- xdi = Math.ceil((i + 1) / LOG_BASE);
1864
- k = xd.length;
1865
- if (xdi >= k) {
1866
- if (isTruncated) {
1867
- for (; k++ <= xdi;) xd.push(0);
1868
- w = rd = 0;
1869
- digits = 1;
1870
- i %= LOG_BASE;
1871
- j = i - LOG_BASE + 1;
1872
- } else break out;
1873
- } else {
1874
- w = k = xd[xdi];
1875
- for (digits = 1; k >= 10; k /= 10) digits++;
1876
- i %= LOG_BASE;
1877
- j = i - LOG_BASE + digits;
1878
- rd = j < 0 ? 0 : w / mathpow(10, digits - j - 1) % 10 | 0;
1879
- }
1880
- }
1881
- isTruncated = isTruncated || sd < 0 || xd[xdi + 1] !== void 0 || (j < 0 ? w : w % mathpow(10, digits - j - 1));
1882
- roundUp = rm < 4 ? (rd || isTruncated) && (rm == 0 || rm == (x.s < 0 ? 3 : 2)) : rd > 5 || rd == 5 && (rm == 4 || isTruncated || rm == 6 && (i > 0 ? j > 0 ? w / mathpow(10, digits - j) : 0 : xd[xdi - 1]) % 10 & 1 || rm == (x.s < 0 ? 8 : 7));
1883
- if (sd < 1 || !xd[0]) {
1884
- xd.length = 0;
1885
- if (roundUp) {
1886
- sd -= x.e + 1;
1887
- xd[0] = mathpow(10, (LOG_BASE - sd % LOG_BASE) % LOG_BASE);
1888
- x.e = -sd || 0;
1889
- } else xd[0] = x.e = 0;
1890
- return x;
1891
- }
1892
- if (i == 0) {
1893
- xd.length = xdi;
1894
- k = 1;
1895
- xdi--;
1896
- } else {
1897
- xd.length = xdi + 1;
1898
- k = mathpow(10, LOG_BASE - i);
1899
- xd[xdi] = j > 0 ? (w / mathpow(10, digits - j) % mathpow(10, j) | 0) * k : 0;
1900
- }
1901
- if (roundUp) for (;;) if (xdi == 0) {
1902
- for (i = 1, j = xd[0]; j >= 10; j /= 10) i++;
1903
- j = xd[0] += k;
1904
- for (k = 1; j >= 10; j /= 10) k++;
1905
- if (i != k) {
1906
- x.e++;
1907
- if (xd[0] == BASE) xd[0] = 1;
1908
- }
1909
- break;
1910
- } else {
1911
- xd[xdi] += k;
1912
- if (xd[xdi] != BASE) break;
1913
- xd[xdi--] = 0;
1914
- k = 1;
1915
- }
1916
- for (i = xd.length; xd[--i] === 0;) xd.pop();
1917
- }
1918
- if (external) {
1919
- if (x.e > Ctor.maxE) {
1920
- x.d = null;
1921
- x.e = NaN;
1922
- } else if (x.e < Ctor.minE) {
1923
- x.e = 0;
1924
- x.d = [0];
1925
- }
922
+ cmp(value) {
923
+ return this.comparedTo(value);
1926
924
  }
1927
- return x;
1928
- }
1929
- function finiteToString(x, isExp, sd) {
1930
- if (!x.isFinite()) return nonFiniteToString(x);
1931
- var k, e = x.e, str = digitsToString(x.d), len = str.length;
1932
- if (isExp) {
1933
- if (sd && (k = sd - len) > 0) str = str.charAt(0) + "." + str.slice(1) + getZeroString(k);
1934
- else if (len > 1) str = str.charAt(0) + "." + str.slice(1);
1935
- str = str + (x.e < 0 ? "e" : "e+") + x.e;
1936
- } else if (e < 0) {
1937
- str = "0." + getZeroString(-e - 1) + str;
1938
- if (sd && (k = sd - len) > 0) str += getZeroString(k);
1939
- } else if (e >= len) {
1940
- str += getZeroString(e + 1 - len);
1941
- if (sd && (k = sd - e - 1) > 0) str = str + "." + getZeroString(k);
1942
- } else {
1943
- if ((k = e + 1) < len) str = str.slice(0, k) + "." + str.slice(k);
1944
- if (sd && (k = sd - len) > 0) {
1945
- if (e + 1 === len) str += ".";
1946
- str += getZeroString(k);
1947
- }
925
+ eq(value) {
926
+ return this.comparedTo(value) === 0;
1948
927
  }
1949
- return str;
1950
- }
1951
- function getBase10Exponent(digits, e) {
1952
- var w = digits[0];
1953
- for (e *= LOG_BASE; w >= 10; w /= 10) e++;
1954
- return e;
1955
- }
1956
- function getLn10(Ctor, sd, pr) {
1957
- if (sd > LN10_PRECISION) {
1958
- external = true;
1959
- if (pr) Ctor.precision = pr;
1960
- throw Error(precisionLimitExceeded);
928
+ equals(value) {
929
+ return this.eq(value);
1961
930
  }
1962
- return finalise(new Ctor(LN10), sd, 1, true);
1963
- }
1964
- function getPi(Ctor, sd, rm) {
1965
- if (sd > PI_PRECISION) throw Error(precisionLimitExceeded);
1966
- return finalise(new Ctor(PI), sd, rm, true);
1967
- }
1968
- function getPrecision(digits) {
1969
- var w = digits.length - 1, len = w * LOG_BASE + 1;
1970
- w = digits[w];
1971
- if (w) {
1972
- for (; w % 10 == 0; w /= 10) len--;
1973
- for (w = digits[0]; w >= 10; w /= 10) len++;
1974
- }
1975
- return len;
1976
- }
1977
- function getZeroString(k) {
1978
- var zs = "";
1979
- for (; k--;) zs += "0";
1980
- return zs;
1981
- }
1982
- function intPow(Ctor, x, n, pr) {
1983
- var isTruncated, r = new Ctor(1), k = Math.ceil(pr / LOG_BASE + 4);
1984
- external = false;
1985
- for (;;) {
1986
- if (n % 2) {
1987
- r = r.times(x);
1988
- if (truncate(r.d, k)) isTruncated = true;
1989
- }
1990
- n = mathfloor(n / 2);
1991
- if (n === 0) {
1992
- n = r.d.length - 1;
1993
- if (isTruncated && r.d[n] === 0) ++r.d[n];
1994
- break;
1995
- }
1996
- x = x.times(x);
1997
- truncate(x.d, k);
931
+ lt(value) {
932
+ return this.comparedTo(value) < 0;
1998
933
  }
1999
- external = true;
2000
- return r;
2001
- }
2002
- function isOdd(n) {
2003
- return n.d[n.d.length - 1] & 1;
2004
- }
2005
- function maxOrMin(Ctor, args, n) {
2006
- var k, y, x = new Ctor(args[0]), i = 0;
2007
- for (; ++i < args.length;) {
2008
- y = new Ctor(args[i]);
2009
- if (!y.s) {
2010
- x = y;
2011
- break;
2012
- }
2013
- k = x.cmp(y);
2014
- if (k === n || k === 0 && x.s === n) x = y;
934
+ lte(value) {
935
+ return this.comparedTo(value) <= 0;
2015
936
  }
2016
- return x;
2017
- }
2018
- function naturalExponential(x, sd) {
2019
- var denominator, guard, j, pow, sum, t, wpr, rep = 0, i = 0, k = 0, Ctor = x.constructor, rm = Ctor.rounding, pr = Ctor.precision;
2020
- if (!x.d || !x.d[0] || x.e > 17) return new Ctor(x.d ? !x.d[0] ? 1 : x.s < 0 ? 0 : 1 / 0 : x.s ? x.s < 0 ? 0 : x : NaN);
2021
- if (sd == null) {
2022
- external = false;
2023
- wpr = pr;
2024
- } else wpr = sd;
2025
- t = new Ctor(.03125);
2026
- while (x.e > -2) {
2027
- x = x.times(t);
2028
- k += 5;
2029
- }
2030
- guard = Math.log(mathpow(2, k)) / Math.LN10 * 2 + 5 | 0;
2031
- wpr += guard;
2032
- denominator = pow = sum = new Ctor(1);
2033
- Ctor.precision = wpr;
2034
- for (;;) {
2035
- pow = finalise(pow.times(x), wpr, 1);
2036
- denominator = denominator.times(++i);
2037
- t = sum.plus(divide(pow, denominator, wpr, 1));
2038
- if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) {
2039
- j = k;
2040
- while (j--) sum = finalise(sum.times(sum), wpr, 1);
2041
- if (sd == null) {
2042
- if (rep < 3 && checkRoundingDigits(sum.d, wpr - guard, rm, rep)) {
2043
- Ctor.precision = wpr += 10;
2044
- denominator = pow = t = new Ctor(1);
2045
- i = 0;
2046
- rep++;
2047
- } else return finalise(sum, Ctor.precision = pr, rm, external = true);
2048
- } else {
2049
- Ctor.precision = pr;
2050
- return sum;
2051
- }
2052
- }
2053
- sum = t;
937
+ gt(value) {
938
+ return this.comparedTo(value) > 0;
2054
939
  }
2055
- }
2056
- function naturalLogarithm(y, sd) {
2057
- var c, c0, denominator, e, numerator, rep, sum, t, wpr, x1, x2, n = 1, guard = 10, x = y, xd = x.d, Ctor = x.constructor, rm = Ctor.rounding, pr = Ctor.precision;
2058
- if (x.s < 0 || !xd || !xd[0] || !x.e && xd[0] == 1 && xd.length == 1) return new Ctor(xd && !xd[0] ? -1 / 0 : x.s != 1 ? NaN : xd ? 0 : x);
2059
- if (sd == null) {
2060
- external = false;
2061
- wpr = pr;
2062
- } else wpr = sd;
2063
- Ctor.precision = wpr += guard;
2064
- c = digitsToString(xd);
2065
- c0 = c.charAt(0);
2066
- if (Math.abs(e = x.e) < 15e14) {
2067
- while (c0 < 7 && c0 != 1 || c0 == 1 && c.charAt(1) > 3) {
2068
- x = x.times(y);
2069
- c = digitsToString(x.d);
2070
- c0 = c.charAt(0);
2071
- n++;
2072
- }
2073
- e = x.e;
2074
- if (c0 > 1) {
2075
- x = new Ctor("0." + c);
2076
- e++;
2077
- } else x = new Ctor(c0 + "." + c.slice(1));
2078
- } else {
2079
- t = getLn10(Ctor, wpr + 2, pr).times(e + "");
2080
- x = naturalLogarithm(new Ctor(c0 + "." + c.slice(1)), wpr - guard).plus(t);
2081
- Ctor.precision = pr;
2082
- return sd == null ? finalise(x, pr, rm, external = true) : x;
2083
- }
2084
- x1 = x;
2085
- sum = numerator = x = divide(x.minus(1), x.plus(1), wpr, 1);
2086
- x2 = finalise(x.times(x), wpr, 1);
2087
- denominator = 3;
2088
- for (;;) {
2089
- numerator = finalise(numerator.times(x2), wpr, 1);
2090
- t = sum.plus(divide(numerator, new Ctor(denominator), wpr, 1));
2091
- if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) {
2092
- sum = sum.times(2);
2093
- if (e !== 0) sum = sum.plus(getLn10(Ctor, wpr + 2, pr).times(e + ""));
2094
- sum = divide(sum, new Ctor(n), wpr, 1);
2095
- if (sd == null) {
2096
- if (checkRoundingDigits(sum.d, wpr - guard, rm, rep)) {
2097
- Ctor.precision = wpr += guard;
2098
- t = numerator = x = divide(x1.minus(1), x1.plus(1), wpr, 1);
2099
- x2 = finalise(x.times(x), wpr, 1);
2100
- denominator = rep = 1;
2101
- } else return finalise(sum, Ctor.precision = pr, rm, external = true);
2102
- } else {
2103
- Ctor.precision = pr;
2104
- return sum;
2105
- }
2106
- }
2107
- sum = t;
2108
- denominator += 2;
940
+ gte(value) {
941
+ return this.comparedTo(value) >= 0;
2109
942
  }
2110
- }
2111
- function nonFiniteToString(x) {
2112
- return String(x.s * x.s / 0);
2113
- }
2114
- function parseDecimal(x, str) {
2115
- var e, i, len;
2116
- if ((e = str.indexOf(".")) > -1) str = str.replace(".", "");
2117
- if ((i = str.search(/e/i)) > 0) {
2118
- if (e < 0) e = i;
2119
- e += +str.slice(i + 1);
2120
- str = str.substring(0, i);
2121
- } else if (e < 0) e = str.length;
2122
- for (i = 0; str.charCodeAt(i) === 48; i++);
2123
- for (len = str.length; str.charCodeAt(len - 1) === 48; --len);
2124
- str = str.slice(i, len);
2125
- if (str) {
2126
- len -= i;
2127
- x.e = e = e - i - 1;
2128
- x.d = [];
2129
- i = (e + 1) % LOG_BASE;
2130
- if (e < 0) i += LOG_BASE;
2131
- if (i < len) {
2132
- if (i) x.d.push(+str.slice(0, i));
2133
- for (len -= LOG_BASE; i < len;) x.d.push(+str.slice(i, i += LOG_BASE));
2134
- str = str.slice(i);
2135
- i = LOG_BASE - str.length;
2136
- } else i -= len;
2137
- for (; i--;) str += "0";
2138
- x.d.push(+str);
2139
- if (external) {
2140
- if (x.e > x.constructor.maxE) {
2141
- x.d = null;
2142
- x.e = NaN;
2143
- } else if (x.e < x.constructor.minE) {
2144
- x.e = 0;
2145
- x.d = [0];
2146
- }
2147
- }
2148
- } else {
2149
- x.e = 0;
2150
- x.d = [0];
943
+ isZero() {
944
+ return this.coefficient === 0n;
2151
945
  }
2152
- return x;
2153
- }
2154
- function parseOther(x, str) {
2155
- var base, Ctor, divisor, i, isFloat, len, p, xd, xe;
2156
- if (str.indexOf("_") > -1) {
2157
- str = str.replace(/(\d)_(?=\d)/g, "$1");
2158
- if (isDecimal.test(str)) return parseDecimal(x, str);
2159
- } else if (str === "Infinity" || str === "NaN") {
2160
- if (!+str) x.s = NaN;
2161
- x.e = NaN;
2162
- x.d = null;
2163
- return x;
2164
- }
2165
- if (isHex.test(str)) {
2166
- base = 16;
2167
- str = str.toLowerCase();
2168
- } else if (isBinary.test(str)) base = 2;
2169
- else if (isOctal.test(str)) base = 8;
2170
- else throw Error(invalidArgument + str);
2171
- i = str.search(/p/i);
2172
- if (i > 0) {
2173
- p = +str.slice(i + 1);
2174
- str = str.substring(2, i);
2175
- } else str = str.slice(2);
2176
- i = str.indexOf(".");
2177
- isFloat = i >= 0;
2178
- Ctor = x.constructor;
2179
- if (isFloat) {
2180
- str = str.replace(".", "");
2181
- len = str.length;
2182
- i = len - i;
2183
- divisor = intPow(Ctor, new Ctor(base), i, i * 2);
2184
- }
2185
- xd = convertBase(str, base, BASE);
2186
- xe = xd.length - 1;
2187
- for (i = xe; xd[i] === 0; --i) xd.pop();
2188
- if (i < 0) return new Ctor(x.s * 0);
2189
- x.e = getBase10Exponent(xd, xe);
2190
- x.d = xd;
2191
- external = false;
2192
- if (isFloat) x = divide(x, divisor, len * 4);
2193
- if (p) x = x.times(Math.abs(p) < 54 ? mathpow(2, p) : Decimal.pow(2, p));
2194
- external = true;
2195
- return x;
2196
- }
2197
- function sine(Ctor, x) {
2198
- var k, len = x.d.length;
2199
- if (len < 3) return x.isZero() ? x : taylorSeries(Ctor, 2, x, x);
2200
- k = 1.4 * Math.sqrt(len);
2201
- k = k > 16 ? 16 : k | 0;
2202
- x = x.times(1 / tinyPow(5, k));
2203
- x = taylorSeries(Ctor, 2, x, x);
2204
- var sin2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20);
2205
- for (; k--;) {
2206
- sin2_x = x.times(x);
2207
- x = x.times(d5.plus(sin2_x.times(d16.times(sin2_x).minus(d20))));
2208
- }
2209
- return x;
2210
- }
2211
- function taylorSeries(Ctor, n, x, y, isHyperbolic) {
2212
- var j, t, u, x2, i = 1, pr = Ctor.precision, k = Math.ceil(pr / LOG_BASE);
2213
- external = false;
2214
- x2 = x.times(x);
2215
- u = new Ctor(y);
2216
- for (;;) {
2217
- t = divide(u.times(x2), new Ctor(n++ * n++), pr, 1);
2218
- u = isHyperbolic ? y.plus(t) : y.minus(t);
2219
- y = divide(t.times(x2), new Ctor(n++ * n++), pr, 1);
2220
- t = u.plus(y);
2221
- if (t.d[k] !== void 0) {
2222
- for (j = k; t.d[j] === u.d[j] && j--;);
2223
- if (j == -1) break;
2224
- }
2225
- j = u;
2226
- u = y;
2227
- y = t;
2228
- t = j;
2229
- i++;
2230
- }
2231
- external = true;
2232
- t.d.length = k + 1;
2233
- return t;
2234
- }
2235
- function tinyPow(b, e) {
2236
- var n = b;
2237
- while (--e) n *= b;
2238
- return n;
2239
- }
2240
- function toLessThanHalfPi(Ctor, x) {
2241
- var t, isNeg = x.s < 0, pi = getPi(Ctor, Ctor.precision, 1), halfPi = pi.times(.5);
2242
- x = x.abs();
2243
- if (x.lte(halfPi)) {
2244
- quadrant = isNeg ? 4 : 1;
2245
- return x;
2246
- }
2247
- t = x.divToInt(pi);
2248
- if (t.isZero()) quadrant = isNeg ? 3 : 2;
2249
- else {
2250
- x = x.minus(t.times(pi));
2251
- if (x.lte(halfPi)) {
2252
- quadrant = isOdd(t) ? isNeg ? 2 : 3 : isNeg ? 4 : 1;
2253
- return x;
2254
- }
2255
- quadrant = isOdd(t) ? isNeg ? 1 : 4 : isNeg ? 3 : 2;
946
+ isNegative() {
947
+ return this.coefficient < 0n;
2256
948
  }
2257
- return x.minus(pi).abs();
2258
- }
2259
- function toStringBinary(x, baseOut, sd, rm) {
2260
- var base, e, i, k, len, roundUp, str, xd, y, Ctor = x.constructor, isExp = sd !== void 0;
2261
- if (isExp) {
2262
- checkInt32(sd, 1, MAX_DIGITS);
2263
- if (rm === void 0) rm = Ctor.rounding;
2264
- else checkInt32(rm, 0, 8);
2265
- } else {
2266
- sd = Ctor.precision;
2267
- rm = Ctor.rounding;
2268
- }
2269
- if (!x.isFinite()) str = nonFiniteToString(x);
2270
- else {
2271
- str = finiteToString(x);
2272
- i = str.indexOf(".");
2273
- if (isExp) {
2274
- base = 2;
2275
- if (baseOut == 16) sd = sd * 4 - 3;
2276
- else if (baseOut == 8) sd = sd * 3 - 2;
2277
- } else base = baseOut;
2278
- if (i >= 0) {
2279
- str = str.replace(".", "");
2280
- y = new Ctor(1);
2281
- y.e = str.length - i;
2282
- y.d = convertBase(finiteToString(y), 10, base);
2283
- y.e = y.d.length;
2284
- }
2285
- xd = convertBase(str, 10, base);
2286
- e = len = xd.length;
2287
- for (; xd[--len] == 0;) xd.pop();
2288
- if (!xd[0]) str = isExp ? "0p+0" : "0";
2289
- else {
2290
- if (i < 0) e--;
2291
- else {
2292
- x = new Ctor(x);
2293
- x.d = xd;
2294
- x.e = e;
2295
- x = divide(x, y, sd, rm, 0, base);
2296
- xd = x.d;
2297
- e = x.e;
2298
- roundUp = inexact;
2299
- }
2300
- i = xd[sd];
2301
- k = base / 2;
2302
- roundUp = roundUp || xd[sd + 1] !== void 0;
2303
- roundUp = rm < 4 ? (i !== void 0 || roundUp) && (rm === 0 || rm === (x.s < 0 ? 3 : 2)) : i > k || i === k && (rm === 4 || roundUp || rm === 6 && xd[sd - 1] & 1 || rm === (x.s < 0 ? 8 : 7));
2304
- xd.length = sd;
2305
- if (roundUp) for (; ++xd[--sd] > base - 1;) {
2306
- xd[sd] = 0;
2307
- if (!sd) {
2308
- ++e;
2309
- xd.unshift(1);
2310
- }
2311
- }
2312
- for (len = xd.length; !xd[len - 1]; --len);
2313
- for (i = 0, str = ""; i < len; i++) str += NUMERALS.charAt(xd[i]);
2314
- if (isExp) {
2315
- if (len > 1) {
2316
- if (baseOut == 16 || baseOut == 8) {
2317
- i = baseOut == 16 ? 4 : 3;
2318
- for (--len; len % i; len++) str += "0";
2319
- xd = convertBase(str, base, baseOut);
2320
- for (len = xd.length; !xd[len - 1]; --len);
2321
- for (i = 1, str = "1."; i < len; i++) str += NUMERALS.charAt(xd[i]);
2322
- } else str = str.charAt(0) + "." + str.slice(1);
2323
- }
2324
- str = str + (e < 0 ? "p" : "p+") + e;
2325
- } else if (e < 0) {
2326
- for (; ++e;) str = "0" + str;
2327
- str = "0." + str;
2328
- } else if (++e > len) for (e -= len; e--;) str += "0";
2329
- else if (e < len) str = str.slice(0, e) + "." + str.slice(e);
2330
- }
2331
- str = (baseOut == 16 ? "0x" : baseOut == 2 ? "0b" : baseOut == 8 ? "0o" : "") + str;
949
+ isPositive() {
950
+ return this.coefficient > 0n;
2332
951
  }
2333
- return x.s < 0 ? "-" + str : str;
2334
- }
2335
- function truncate(arr, len) {
2336
- if (arr.length > len) {
2337
- arr.length = len;
952
+ isFinite() {
2338
953
  return true;
2339
954
  }
2340
- }
2341
- function abs(x) {
2342
- return new this(x).abs();
2343
- }
2344
- function acos(x) {
2345
- return new this(x).acos();
2346
- }
2347
- function acosh(x) {
2348
- return new this(x).acosh();
2349
- }
2350
- function add(x, y) {
2351
- return new this(x).plus(y);
2352
- }
2353
- function asin(x) {
2354
- return new this(x).asin();
2355
- }
2356
- function asinh(x) {
2357
- return new this(x).asinh();
2358
- }
2359
- function atan(x) {
2360
- return new this(x).atan();
2361
- }
2362
- function atanh(x) {
2363
- return new this(x).atanh();
2364
- }
2365
- function atan2(y, x) {
2366
- y = new this(y);
2367
- x = new this(x);
2368
- var r, pr = this.precision, rm = this.rounding, wpr = pr + 4;
2369
- if (!y.s || !x.s) r = new this(NaN);
2370
- else if (!y.d && !x.d) {
2371
- r = getPi(this, wpr, 1).times(x.s > 0 ? .25 : .75);
2372
- r.s = y.s;
2373
- } else if (!x.d || y.isZero()) {
2374
- r = x.s < 0 ? getPi(this, pr, rm) : new this(0);
2375
- r.s = y.s;
2376
- } else if (!y.d || x.isZero()) {
2377
- r = getPi(this, wpr, 1).times(.5);
2378
- r.s = y.s;
2379
- } else if (x.s < 0) {
2380
- this.precision = wpr;
2381
- this.rounding = 1;
2382
- r = this.atan(divide(y, x, wpr, 1));
2383
- x = getPi(this, wpr, 1);
2384
- this.precision = pr;
2385
- this.rounding = rm;
2386
- r = y.s < 0 ? r.minus(x) : r.plus(x);
2387
- } else r = this.atan(divide(y, x, wpr, 1));
2388
- return r;
2389
- }
2390
- function cbrt(x) {
2391
- return new this(x).cbrt();
2392
- }
2393
- function ceil(x) {
2394
- return finalise(x = new this(x), x.e + 1, 2);
2395
- }
2396
- function clamp(x, min, max) {
2397
- return new this(x).clamp(min, max);
2398
- }
2399
- function config(obj) {
2400
- if (!obj || typeof obj !== "object") throw Error(decimalError + "Object expected");
2401
- var i, p, v, useDefaults = obj.defaults === true, ps = [
2402
- "precision",
2403
- 1,
2404
- MAX_DIGITS,
2405
- "rounding",
2406
- 0,
2407
- 8,
2408
- "toExpNeg",
2409
- -EXP_LIMIT,
2410
- 0,
2411
- "toExpPos",
2412
- 0,
2413
- EXP_LIMIT,
2414
- "maxE",
2415
- 0,
2416
- EXP_LIMIT,
2417
- "minE",
2418
- -EXP_LIMIT,
2419
- 0,
2420
- "modulo",
2421
- 0,
2422
- 9
2423
- ];
2424
- for (i = 0; i < ps.length; i += 3) {
2425
- if (p = ps[i], useDefaults) this[p] = DEFAULTS[p];
2426
- if ((v = obj[p]) !== void 0) {
2427
- if (mathfloor(v) === v && v >= ps[i + 1] && v <= ps[i + 2]) this[p] = v;
2428
- else throw Error(invalidArgument + p + ": " + v);
955
+ isNaN() {
956
+ return false;
957
+ }
958
+ toNumber() {
959
+ return Number(this.toString());
960
+ }
961
+ toString() {
962
+ const c = abs(this.coefficient).toString(), sign = this.isNegative() ? "-" : "";
963
+ const order = c.length + this.exponent - 1;
964
+ if (this.isZero()) return "0";
965
+ if (order <= -7 || order >= 21) return sign + c[0] + (c.length > 1 ? "." + c.slice(1) : "") + "e" + (order >= 0 ? "+" : "") + order;
966
+ const point = c.length + this.exponent;
967
+ return sign + (point <= 0 ? "0." + "0".repeat(-point) + c : point >= c.length ? c + "0".repeat(point - c.length) : c.slice(0, point) + "." + c.slice(point));
968
+ }
969
+ toFixed(decimalPlaces = 0, rounding = Decimal.rounding) {
970
+ if (!Number.isInteger(decimalPlaces) || decimalPlaces < 0 || decimalPlaces > 1e3 || !Number.isInteger(rounding) || rounding < 0 || rounding > 8) throw new RangeError("Invalid decimal places or rounding");
971
+ const shift = this.exponent + decimalPlaces;
972
+ let q = this.coefficient;
973
+ if (shift >= 0) q *= power(shift);
974
+ else {
975
+ const divisor = power(-shift), sign = q < 0n ? -1n : 1n, remainder = abs(q % divisor);
976
+ q /= divisor;
977
+ if (increment(q, remainder, divisor, sign, rounding)) q += sign;
2429
978
  }
979
+ const text = abs(q).toString().padStart(decimalPlaces + 1, "0");
980
+ return (q < 0n ? "-" : "") + (decimalPlaces === 0 ? text : text.slice(0, -decimalPlaces) + "." + text.slice(-decimalPlaces));
2430
981
  }
2431
- if (p = "crypto", useDefaults) this[p] = DEFAULTS[p];
2432
- if ((v = obj[p]) !== void 0) {
2433
- if (v === true || v === false || v === 0 || v === 1) {
2434
- if (v) {
2435
- if (typeof crypto != "undefined" && crypto && (crypto.getRandomValues || crypto.randomBytes)) this[p] = true;
2436
- else throw Error(cryptoUnavailable);
2437
- } else this[p] = false;
2438
- } else throw Error(invalidArgument + p + ": " + v);
982
+ toJSON() {
983
+ return this.toString();
2439
984
  }
2440
- return this;
2441
- }
2442
- function cos(x) {
2443
- return new this(x).cos();
2444
- }
2445
- function cosh(x) {
2446
- return new this(x).cosh();
2447
- }
2448
- function clone(obj) {
2449
- var i, p, ps;
2450
- function Decimal(v) {
2451
- var e, i, t, x = this;
2452
- if (!(x instanceof Decimal)) return new Decimal(v);
2453
- x.constructor = Decimal;
2454
- if (isDecimalInstance(v)) {
2455
- x.s = v.s;
2456
- if (external) {
2457
- if (!v.d || v.e > Decimal.maxE) {
2458
- x.e = NaN;
2459
- x.d = null;
2460
- } else if (v.e < Decimal.minE) {
2461
- x.e = 0;
2462
- x.d = [0];
2463
- } else {
2464
- x.e = v.e;
2465
- x.d = v.d.slice();
2466
- }
2467
- } else {
2468
- x.e = v.e;
2469
- x.d = v.d ? v.d.slice() : v.d;
2470
- }
2471
- return;
2472
- }
2473
- t = typeof v;
2474
- if (t === "number") {
2475
- if (v === 0) {
2476
- x.s = 1 / v < 0 ? -1 : 1;
2477
- x.e = 0;
2478
- x.d = [0];
2479
- return;
2480
- }
2481
- if (v < 0) {
2482
- v = -v;
2483
- x.s = -1;
2484
- } else x.s = 1;
2485
- if (v === ~~v && v < 1e7) {
2486
- for (e = 0, i = v; i >= 10; i /= 10) e++;
2487
- if (external) {
2488
- if (e > Decimal.maxE) {
2489
- x.e = NaN;
2490
- x.d = null;
2491
- } else if (e < Decimal.minE) {
2492
- x.e = 0;
2493
- x.d = [0];
2494
- } else {
2495
- x.e = e;
2496
- x.d = [v];
2497
- }
2498
- } else {
2499
- x.e = e;
2500
- x.d = [v];
2501
- }
2502
- return;
2503
- }
2504
- if (v * 0 !== 0) {
2505
- if (!v) x.s = NaN;
2506
- x.e = NaN;
2507
- x.d = null;
2508
- return;
2509
- }
2510
- return parseDecimal(x, v.toString());
2511
- }
2512
- if (t === "string") {
2513
- if ((i = v.charCodeAt(0)) === 45) {
2514
- v = v.slice(1);
2515
- x.s = -1;
2516
- } else {
2517
- if (i === 43) v = v.slice(1);
2518
- x.s = 1;
2519
- }
2520
- return isDecimal.test(v) ? parseDecimal(x, v) : parseOther(x, v);
2521
- }
2522
- if (t === "bigint") {
2523
- if (v < 0) {
2524
- v = -v;
2525
- x.s = -1;
2526
- } else x.s = 1;
2527
- return parseDecimal(x, v.toString());
2528
- }
2529
- throw Error(invalidArgument + v);
2530
- }
2531
- Decimal.prototype = P;
2532
- Decimal.ROUND_UP = 0;
2533
- Decimal.ROUND_DOWN = 1;
2534
- Decimal.ROUND_CEIL = 2;
2535
- Decimal.ROUND_FLOOR = 3;
2536
- Decimal.ROUND_HALF_UP = 4;
2537
- Decimal.ROUND_HALF_DOWN = 5;
2538
- Decimal.ROUND_HALF_EVEN = 6;
2539
- Decimal.ROUND_HALF_CEIL = 7;
2540
- Decimal.ROUND_HALF_FLOOR = 8;
2541
- Decimal.EUCLID = 9;
2542
- Decimal.config = Decimal.set = config;
2543
- Decimal.clone = clone;
2544
- Decimal.isDecimal = isDecimalInstance;
2545
- Decimal.abs = abs;
2546
- Decimal.acos = acos;
2547
- Decimal.acosh = acosh;
2548
- Decimal.add = add;
2549
- Decimal.asin = asin;
2550
- Decimal.asinh = asinh;
2551
- Decimal.atan = atan;
2552
- Decimal.atanh = atanh;
2553
- Decimal.atan2 = atan2;
2554
- Decimal.cbrt = cbrt;
2555
- Decimal.ceil = ceil;
2556
- Decimal.clamp = clamp;
2557
- Decimal.cos = cos;
2558
- Decimal.cosh = cosh;
2559
- Decimal.div = div;
2560
- Decimal.exp = exp;
2561
- Decimal.floor = floor;
2562
- Decimal.hypot = hypot;
2563
- Decimal.ln = ln;
2564
- Decimal.log = log;
2565
- Decimal.log10 = log10;
2566
- Decimal.log2 = log2;
2567
- Decimal.max = max;
2568
- Decimal.min = min;
2569
- Decimal.mod = mod;
2570
- Decimal.mul = mul;
2571
- Decimal.pow = pow;
2572
- Decimal.random = random;
2573
- Decimal.round = round;
2574
- Decimal.sign = sign;
2575
- Decimal.sin = sin;
2576
- Decimal.sinh = sinh;
2577
- Decimal.sqrt = sqrt;
2578
- Decimal.sub = sub;
2579
- Decimal.sum = sum;
2580
- Decimal.tan = tan;
2581
- Decimal.tanh = tanh;
2582
- Decimal.trunc = trunc;
2583
- if (obj === void 0) obj = {};
2584
- if (obj) {
2585
- if (obj.defaults !== true) {
2586
- ps = [
2587
- "precision",
2588
- "rounding",
2589
- "toExpNeg",
2590
- "toExpPos",
2591
- "maxE",
2592
- "minE",
2593
- "modulo",
2594
- "crypto"
2595
- ];
2596
- for (i = 0; i < ps.length;) if (!obj.hasOwnProperty(p = ps[i++])) obj[p] = this[p];
2597
- }
985
+ valueOf() {
986
+ return this.toString();
2598
987
  }
2599
- Decimal.config(obj);
2600
- return Decimal;
2601
- }
2602
- function div(x, y) {
2603
- return new this(x).div(y);
2604
- }
2605
- function exp(x) {
2606
- return new this(x).exp();
2607
- }
2608
- function floor(x) {
2609
- return finalise(x = new this(x), x.e + 1, 3);
2610
- }
2611
- function hypot() {
2612
- var i, n, t = new this(0);
2613
- external = false;
2614
- for (i = 0; i < arguments.length;) {
2615
- n = new this(arguments[i++]);
2616
- if (!n.d) {
2617
- if (n.s) {
2618
- external = true;
2619
- return new this(1 / 0);
2620
- }
2621
- t = n;
2622
- } else if (t.d) t = t.plus(n.times(n));
2623
- }
2624
- external = true;
2625
- return t.sqrt();
2626
- }
2627
- function isDecimalInstance(obj) {
2628
- return obj instanceof Decimal || obj && obj.toStringTag === tag || false;
2629
- }
2630
- function ln(x) {
2631
- return new this(x).ln();
2632
- }
2633
- function log(x, y) {
2634
- return new this(x).log(y);
2635
- }
2636
- function log2(x) {
2637
- return new this(x).log(2);
2638
- }
2639
- function log10(x) {
2640
- return new this(x).log(10);
2641
- }
2642
- function max() {
2643
- return maxOrMin(this, arguments, -1);
2644
- }
2645
- function min() {
2646
- return maxOrMin(this, arguments, 1);
2647
- }
2648
- function mod(x, y) {
2649
- return new this(x).mod(y);
2650
- }
2651
- function mul(x, y) {
2652
- return new this(x).mul(y);
2653
- }
2654
- function pow(x, y) {
2655
- return new this(x).pow(y);
2656
- }
2657
- function random(sd) {
2658
- var d, e, k, n, i = 0, r = new this(1), rd = [];
2659
- if (sd === void 0) sd = this.precision;
2660
- else checkInt32(sd, 1, MAX_DIGITS);
2661
- k = Math.ceil(sd / LOG_BASE);
2662
- if (!this.crypto) for (; i < k;) rd[i++] = Math.random() * 1e7 | 0;
2663
- else if (crypto.getRandomValues) {
2664
- d = crypto.getRandomValues(new Uint32Array(k));
2665
- for (; i < k;) {
2666
- n = d[i];
2667
- if (n >= 429e7) d[i] = crypto.getRandomValues(/* @__PURE__ */ new Uint32Array(1))[0];
2668
- else rd[i++] = n % 1e7;
2669
- }
2670
- } else if (crypto.randomBytes) {
2671
- d = crypto.randomBytes(k *= 4);
2672
- for (; i < k;) {
2673
- n = d[i] + (d[i + 1] << 8) + (d[i + 2] << 16) + ((d[i + 3] & 127) << 24);
2674
- if (n >= 214e7) crypto.randomBytes(4).copy(d, i);
2675
- else {
2676
- rd.push(n % 1e7);
2677
- i += 4;
2678
- }
2679
- }
2680
- i = k / 4;
2681
- } else throw Error(cryptoUnavailable);
2682
- k = rd[--i];
2683
- sd %= LOG_BASE;
2684
- if (k && sd) {
2685
- n = mathpow(10, LOG_BASE - sd);
2686
- rd[i] = (k / n | 0) * n;
2687
- }
2688
- for (; rd[i] === 0; i--) rd.pop();
2689
- if (i < 0) {
2690
- e = 0;
2691
- rd = [0];
2692
- } else {
2693
- e = -1;
2694
- for (; rd[0] === 0; e -= LOG_BASE) rd.shift();
2695
- for (k = 1, n = rd[0]; n >= 10; n /= 10) k++;
2696
- if (k < LOG_BASE) e -= LOG_BASE - k;
2697
- }
2698
- r.e = e;
2699
- r.d = rd;
2700
- return r;
2701
- }
2702
- function round(x) {
2703
- return finalise(x = new this(x), x.e + 1, this.rounding);
2704
- }
2705
- function sign(x) {
2706
- x = new this(x);
2707
- return x.d ? x.d[0] ? x.s : 0 * x.s : x.s || NaN;
2708
- }
2709
- function sin(x) {
2710
- return new this(x).sin();
2711
- }
2712
- function sinh(x) {
2713
- return new this(x).sinh();
2714
- }
2715
- function sqrt(x) {
2716
- return new this(x).sqrt();
2717
- }
2718
- function sub(x, y) {
2719
- return new this(x).sub(y);
2720
- }
2721
- function sum() {
2722
- var i = 0, args = arguments, x = new this(args[i]);
2723
- external = false;
2724
- for (; x.s && ++i < args.length;) x = x.plus(args[i]);
2725
- external = true;
2726
- return finalise(x, this.precision, this.rounding);
2727
- }
2728
- function tan(x) {
2729
- return new this(x).tan();
2730
- }
2731
- function tanh(x) {
2732
- return new this(x).tanh();
2733
- }
2734
- function trunc(x) {
2735
- return finalise(x = new this(x), x.e + 1, 1);
2736
- }
2737
- P[Symbol.for("nodejs.util.inspect.custom")] = P.toString;
2738
- P[Symbol.toStringTag] = "Decimal";
2739
- var Decimal = P.constructor = clone(DEFAULTS);
2740
- LN10 = new Decimal(LN10);
2741
- PI = new Decimal(PI);
988
+ };
2742
989
  // src/math/HVec3.ts
2743
990
  /** Mutable decimal vector. Pass strings to preserve more precision than a JS number. */
2744
991
  var HVec3 = class HVec3 {