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