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