@xort/cga 0.1.2 → 0.1.3

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