@holgerengels/compute-engine
Version:
Symbolic computing and numeric evaluations for JavaScript and Node.js
1,635 lines (1,631 loc) • 1.11 MB
JavaScript
/** Compute Engine 0.26.0-alpha2 */
(function(global,factory){typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'],factory):(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ComputeEngine = {}));})(this, (function (exports) { 'use strict';
var ComputeEngine = (() => {
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod2) => __copyProps(__defProp({}, "__esModule", { value: true }), mod2);
// src/compute-engine.ts
var compute_engine_exports = {};
__export(compute_engine_exports, {
ComputeEngine: () => ComputeEngine,
highlightCodeBlock: () => highlightCodeBlock,
highlightCodeSpan: () => highlightCodeSpan,
isBoxedRule: () => isBoxedRule,
isRuleStep: () => isRuleStep,
terminal: () => terminal,
version: () => version
});
// src/compute-engine/boxed-expression/public.ts
function isRuleStep(x) {
return x && typeof x === "object" && "because" in x && "value" in x;
}
function isBoxedRule(x) {
return x && typeof x === "object" && x._tag === "boxed-rule";
}
// node_modules/complex-esm/dist/src/complex.js
var cosh = Math.cosh || function(x) {
return Math.abs(x) < 1e-9 ? 1 - x : (Math.exp(x) + Math.exp(-x)) * 0.5;
};
var sinh = Math.sinh || function(x) {
return Math.abs(x) < 1e-9 ? x : (Math.exp(x) - Math.exp(-x)) * 0.5;
};
var cosm1 = function(x) {
var b = Math.PI / 4;
if (-b > x || x > b) {
return Math.cos(x) - 1;
}
var xx = x * x;
return xx * (xx * (xx * (xx * (xx * (xx * (xx * (xx / 20922789888e3 - 1 / 87178291200) + 1 / 479001600) - 1 / 3628800) + 1 / 40320) - 1 / 720) + 1 / 24) - 1 / 2);
};
var hypot = function(x, y) {
var a = Math.abs(x);
var b = Math.abs(y);
if (a < 3e3 && b < 3e3) {
return Math.sqrt(a * a + b * b);
}
if (a < b) {
a = b;
b = x / y;
} else {
b = y / x;
}
return a * Math.sqrt(1 + b * b);
};
var parser_exit = function() {
throw SyntaxError("Invalid Param");
};
function logHypot(a, b) {
var _a = Math.abs(a);
var _b = Math.abs(b);
if (a === 0) {
return Math.log(_b);
}
if (b === 0) {
return Math.log(_a);
}
if (_a < 3e3 && _b < 3e3) {
return Math.log(a * a + b * b) * 0.5;
}
a = a / 2;
b = b / 2;
return 0.5 * Math.log(a * a + b * b) + Math.LN2;
}
var parse = function(a, b) {
var z = { "re": 0, "im": 0 };
if (a === void 0 || a === null) {
z["re"] = z["im"] = 0;
} else if (b !== void 0) {
z["re"] = a;
z["im"] = b;
} else
switch (typeof a) {
case "object":
if ("im" in a && "re" in a) {
z["re"] = a["re"];
z["im"] = a["im"];
} else if ("abs" in a && "arg" in a) {
if (!Number.isFinite(a["abs"]) && Number.isFinite(a["arg"])) {
return Complex["INFINITY"];
}
z["re"] = a["abs"] * Math.cos(a["arg"]);
z["im"] = a["abs"] * Math.sin(a["arg"]);
} else if ("r" in a && "phi" in a) {
if (!Number.isFinite(a["r"]) && Number.isFinite(a["phi"])) {
return Complex["INFINITY"];
}
z["re"] = a["r"] * Math.cos(a["phi"]);
z["im"] = a["r"] * Math.sin(a["phi"]);
} else if (a.length === 2) {
z["re"] = a[0];
z["im"] = a[1];
} else {
parser_exit();
}
break;
case "string":
z["im"] = /* void */
z["re"] = 0;
var tokens = a.match(/\d+\.?\d*e[+-]?\d+|\d+\.?\d*|\.\d+|./g);
var plus = 1;
var minus = 0;
if (tokens === null) {
parser_exit();
}
for (var i = 0; i < tokens.length; i++) {
var c = tokens[i];
if (c === " " || c === " " || c === "\n") {
} else if (c === "+") {
plus++;
} else if (c === "-") {
minus++;
} else if (c === "i" || c === "I") {
if (plus + minus === 0) {
parser_exit();
}
if (tokens[i + 1] !== " " && !isNaN(Number(tokens[i + 1]))) {
z["im"] += parseFloat((minus % 2 ? "-" : "") + tokens[i + 1]);
i++;
} else {
z["im"] += parseFloat((minus % 2 ? "-" : "") + "1");
}
plus = minus = 0;
} else {
if (plus + minus === 0 || isNaN(Number(c))) {
parser_exit();
}
if (tokens[i + 1] === "i" || tokens[i + 1] === "I") {
z["im"] += parseFloat((minus % 2 ? "-" : "") + c);
i++;
} else {
z["re"] += parseFloat((minus % 2 ? "-" : "") + c);
}
plus = minus = 0;
}
}
if (plus + minus > 0) {
parser_exit();
}
break;
case "number":
z["im"] = 0;
z["re"] = a;
break;
default:
parser_exit();
}
if (isNaN(z["re"]) || isNaN(z["im"])) {
}
return z;
};
var Complex = class _Complex {
constructor(a, b) {
this.re = 0;
this.im = 0;
var z = parse(a, b);
this["re"] = z["re"];
this["im"] = z["im"];
}
/**
* Calculates the sign of a complex number, which is a normalized complex
*
* @returns {Complex}
*/
sign() {
var abs2 = this["abs"]();
return new _Complex(this["re"] / abs2, this["im"] / abs2);
}
/**
* Adds two complex numbers
*
* @returns {Complex}
*/
add(a, b) {
var z = new _Complex(a, b);
if (this["isInfinite"]() && z["isInfinite"]()) {
return _Complex["NAN"];
}
if (this["isInfinite"]() || z["isInfinite"]()) {
return _Complex["INFINITY"];
}
return new _Complex(this["re"] + z["re"], this["im"] + z["im"]);
}
/**
* Subtracts two complex numbers
*
* @returns {Complex}
*/
sub(a, b) {
var z = new _Complex(a, b);
if (this["isInfinite"]() && z["isInfinite"]()) {
return _Complex["NAN"];
}
if (this["isInfinite"]() || z["isInfinite"]()) {
return _Complex["INFINITY"];
}
return new _Complex(this["re"] - z["re"], this["im"] - z["im"]);
}
/**
* Multiplies two complex numbers
*
* @returns {Complex}
*/
mul(a, b) {
var z = new _Complex(a, b);
if (this["isInfinite"]() && z["isZero"]() || this["isZero"]() && z["isInfinite"]()) {
return _Complex["NAN"];
}
if (this["isInfinite"]() || z["isInfinite"]()) {
return _Complex["INFINITY"];
}
if (z["im"] === 0 && this["im"] === 0) {
return new _Complex(this["re"] * z["re"], 0);
}
return new _Complex(this["re"] * z["re"] - this["im"] * z["im"], this["re"] * z["im"] + this["im"] * z["re"]);
}
/**
* Divides two complex numbers
*
* @returns {Complex}
*/
div(a, b) {
var z = new _Complex(a, b);
if (this["isZero"]() && z["isZero"]() || this["isInfinite"]() && z["isInfinite"]()) {
return _Complex["NAN"];
}
if (this["isInfinite"]() || z["isZero"]()) {
return _Complex["INFINITY"];
}
if (this["isZero"]() || z["isInfinite"]()) {
return _Complex["ZERO"];
}
a = this["re"];
b = this["im"];
var c = z["re"];
var d = z["im"];
var t, x;
if (0 === d) {
return new _Complex(a / c, b / c);
}
if (Math.abs(c) < Math.abs(d)) {
x = c / d;
t = c * x + d;
return new _Complex((a * x + b) / t, (b * x - a) / t);
} else {
x = d / c;
t = d * x + c;
return new _Complex((a + b * x) / t, (b - a * x) / t);
}
}
/**
* Calculate the power of two complex numbers
*
* @returns {Complex}
*/
pow(a, b) {
var z = new _Complex(a, b);
a = this["re"];
b = this["im"];
if (z["isZero"]()) {
return _Complex["ONE"];
}
if (z["im"] === 0) {
if (b === 0 && a > 0) {
return new _Complex(Math.pow(a, z["re"]), 0);
} else if (a === 0) {
switch ((z["re"] % 4 + 4) % 4) {
case 0:
return new _Complex(Math.pow(b, z["re"]), 0);
case 1:
return new _Complex(0, Math.pow(b, z["re"]));
case 2:
return new _Complex(-Math.pow(b, z["re"]), 0);
case 3:
return new _Complex(0, -Math.pow(b, z["re"]));
}
}
}
if (a === 0 && b === 0 && z["re"] > 0 && z["im"] >= 0) {
return _Complex["ZERO"];
}
var arg = Math.atan2(b, a);
var loh = logHypot(a, b);
a = Math.exp(z["re"] * loh - z["im"] * arg);
b = z["im"] * loh + z["re"] * arg;
return new _Complex(a * Math.cos(b), a * Math.sin(b));
}
/**
* Calculate the complex square root
*
* @returns {Complex}
*/
sqrt() {
var a = this["re"];
var b = this["im"];
var r = this["abs"]();
var re, im;
if (a >= 0) {
if (b === 0) {
return new _Complex(Math.sqrt(a), 0);
}
re = 0.5 * Math.sqrt(2 * (r + a));
} else {
re = Math.abs(b) / Math.sqrt(2 * (r - a));
}
if (a <= 0) {
im = 0.5 * Math.sqrt(2 * (r - a));
} else {
im = Math.abs(b) / Math.sqrt(2 * (r + a));
}
return new _Complex(re, b < 0 ? -im : im);
}
/**
* Calculate the complex exponent
*
* @returns {Complex}
*/
exp() {
var tmp = Math.exp(this["re"]);
if (this["im"] === 0) {
}
return new _Complex(tmp * Math.cos(this["im"]), tmp * Math.sin(this["im"]));
}
/**
* Calculate the complex exponent and subtracts one.
*
* This may be more accurate than `Complex(x).exp().sub(1)` if
* `x` is small.
*
* @returns {Complex}
*/
expm1() {
var a = this["re"];
var b = this["im"];
return new _Complex(Math.expm1(a) * Math.cos(b) + cosm1(b), Math.exp(a) * Math.sin(b));
}
/**
* Calculate the natural log
*
* @returns {Complex}
*/
log() {
var a = this["re"];
var b = this["im"];
if (b === 0 && a > 0) {
}
return new _Complex(logHypot(a, b), Math.atan2(b, a));
}
/**
* Calculate the magnitude of the complex number
*
* @returns {number}
*/
abs() {
return hypot(this["re"], this["im"]);
}
/**
* Calculate the angle of the complex number
*
* @returns {number}
*/
arg() {
return Math.atan2(this["im"], this["re"]);
}
/**
* Calculate the sine of the complex number
*
* @returns {Complex}
*/
sin() {
var a = this["re"];
var b = this["im"];
return new _Complex(Math.sin(a) * cosh(b), Math.cos(a) * sinh(b));
}
/**
* Calculate the cosine
*
* @returns {Complex}
*/
cos() {
var a = this["re"];
var b = this["im"];
return new _Complex(Math.cos(a) * cosh(b), -Math.sin(a) * sinh(b));
}
/**
* Calculate the tangent
*
* @returns {Complex}
*/
tan() {
var a = 2 * this["re"];
var b = 2 * this["im"];
var d = Math.cos(a) + cosh(b);
return new _Complex(Math.sin(a) / d, sinh(b) / d);
}
/**
* Calculate the cotangent
*
* @returns {Complex}
*/
cot() {
var a = 2 * this["re"];
var b = 2 * this["im"];
var d = Math.cos(a) - cosh(b);
return new _Complex(-Math.sin(a) / d, sinh(b) / d);
}
/**
* Calculate the secant
*
* @returns {Complex}
*/
sec() {
var a = this["re"];
var b = this["im"];
var d = 0.5 * cosh(2 * b) + 0.5 * Math.cos(2 * a);
return new _Complex(Math.cos(a) * cosh(b) / d, Math.sin(a) * sinh(b) / d);
}
/**
* Calculate the cosecans
*
* @returns {Complex}
*/
csc() {
var a = this["re"];
var b = this["im"];
var d = 0.5 * cosh(2 * b) - 0.5 * Math.cos(2 * a);
return new _Complex(Math.sin(a) * cosh(b) / d, -Math.cos(a) * sinh(b) / d);
}
/**
* Calculate the complex arcus sinus
*
* @returns {Complex}
*/
asin() {
var a = this["re"];
var b = this["im"];
var t1 = new _Complex(b * b - a * a + 1, -2 * a * b)["sqrt"]();
var t2 = new _Complex(t1["re"] - b, t1["im"] + a)["log"]();
return new _Complex(t2["im"], -t2["re"]);
}
/**
* Calculate the complex arcus cosinus
*
* @returns {Complex}
*/
acos() {
var a = this["re"];
var b = this["im"];
var t1 = new _Complex(b * b - a * a + 1, -2 * a * b)["sqrt"]();
var t2 = new _Complex(t1["re"] - b, t1["im"] + a)["log"]();
return new _Complex(Math.PI / 2 - t2["im"], t2["re"]);
}
/**
* Calculate the complex arcus tangent
*
* @returns {Complex}
*/
atan() {
var a = this["re"];
var b = this["im"];
if (a === 0) {
if (b === 1) {
return new _Complex(0, Infinity);
}
if (b === -1) {
return new _Complex(0, -Infinity);
}
}
var d = a * a + (1 - b) * (1 - b);
var t1 = new _Complex((1 - b * b - a * a) / d, -2 * a / d).log();
return new _Complex(-0.5 * t1["im"], 0.5 * t1["re"]);
}
/**
* Calculate the complex arcus cotangent
*
* @returns {Complex}
*/
acot() {
var a = this["re"];
var b = this["im"];
if (b === 0) {
return new _Complex(Math.atan2(1, a), 0);
}
var d = a * a + b * b;
return d !== 0 ? new _Complex(a / d, -b / d).atan() : new _Complex(a !== 0 ? a / 0 : 0, b !== 0 ? -b / 0 : 0).atan();
}
/**
* Calculate the complex arcus secant
*
* @returns {Complex}
*/
asec() {
var a = this["re"];
var b = this["im"];
if (a === 0 && b === 0) {
return new _Complex(0, Infinity);
}
var d = a * a + b * b;
return d !== 0 ? new _Complex(a / d, -b / d).acos() : new _Complex(a !== 0 ? a / 0 : 0, b !== 0 ? -b / 0 : 0).acos();
}
/**
* Calculate the complex arcus cosecans
*
* @returns {Complex}
*/
acsc() {
var a = this["re"];
var b = this["im"];
if (a === 0 && b === 0) {
return new _Complex(Math.PI / 2, Infinity);
}
var d = a * a + b * b;
return d !== 0 ? new _Complex(a / d, -b / d).asin() : new _Complex(a !== 0 ? a / 0 : 0, b !== 0 ? -b / 0 : 0).asin();
}
/**
* Calculate the complex sinh
*
* @returns {Complex}
*/
sinh() {
var a = this["re"];
var b = this["im"];
return new _Complex(sinh(a) * Math.cos(b), cosh(a) * Math.sin(b));
}
/**
* Calculate the complex cosh
*
* @returns {Complex}
*/
cosh() {
var a = this["re"];
var b = this["im"];
return new _Complex(cosh(a) * Math.cos(b), sinh(a) * Math.sin(b));
}
/**
* Calculate the complex tanh
*
* @returns {Complex}
*/
tanh() {
var a = 2 * this["re"];
var b = 2 * this["im"];
var d = cosh(a) + Math.cos(b);
return new _Complex(sinh(a) / d, Math.sin(b) / d);
}
/**
* Calculate the complex coth
*
* @returns {Complex}
*/
coth() {
var a = 2 * this["re"];
var b = 2 * this["im"];
var d = cosh(a) - Math.cos(b);
return new _Complex(sinh(a) / d, -Math.sin(b) / d);
}
/**
* Calculate the complex coth
*
* @returns {Complex}
*/
csch() {
var a = this["re"];
var b = this["im"];
var d = Math.cos(2 * b) - cosh(2 * a);
return new _Complex(-2 * sinh(a) * Math.cos(b) / d, 2 * cosh(a) * Math.sin(b) / d);
}
/**
* Calculate the complex sech
*
* @returns {Complex}
*/
sech() {
var a = this["re"];
var b = this["im"];
var d = Math.cos(2 * b) + cosh(2 * a);
return new _Complex(2 * cosh(a) * Math.cos(b) / d, -2 * sinh(a) * Math.sin(b) / d);
}
/**
* Calculate the complex asinh
*
* @returns {Complex}
*/
asinh() {
var tmp = this["im"];
this["im"] = -this["re"];
this["re"] = tmp;
var res = this["asin"]();
this["re"] = -this["im"];
this["im"] = tmp;
tmp = res["re"];
res["re"] = -res["im"];
res["im"] = tmp;
return res;
}
/**
* Calculate the complex acosh
*
* @returns {Complex}
*/
acosh() {
var res = this["acos"]();
if (res["im"] <= 0) {
var tmp = res["re"];
res["re"] = -res["im"];
res["im"] = tmp;
} else {
var tmp = res["im"];
res["im"] = -res["re"];
res["re"] = tmp;
}
return res;
}
/**
* Calculate the complex atanh
*
* @returns {Complex}
*/
atanh() {
var a = this["re"];
var b = this["im"];
var noIM = a > 1 && b === 0;
var oneMinus = 1 - a;
var onePlus = 1 + a;
var d = oneMinus * oneMinus + b * b;
var x = d !== 0 ? new _Complex((onePlus * oneMinus - b * b) / d, (b * oneMinus + onePlus * b) / d) : new _Complex(a !== -1 ? a / 0 : 0, b !== 0 ? b / 0 : 0);
var temp = x["re"];
x["re"] = logHypot(x["re"], x["im"]) / 2;
x["im"] = Math.atan2(x["im"], temp) / 2;
if (noIM) {
x["im"] = -x["im"];
}
return x;
}
/**
* Calculate the complex acoth
*
* @returns {Complex}
*/
acoth() {
var a = this["re"];
var b = this["im"];
if (a === 0 && b === 0) {
return new _Complex(0, Math.PI / 2);
}
var d = a * a + b * b;
return d !== 0 ? new _Complex(a / d, -b / d).atanh() : new _Complex(a !== 0 ? a / 0 : 0, b !== 0 ? -b / 0 : 0).atanh();
}
/**
* Calculate the complex acsch
*
* @returns {Complex}
*/
acsch() {
var a = this["re"];
var b = this["im"];
if (b === 0) {
return new _Complex(a !== 0 ? Math.log(a + Math.sqrt(a * a + 1)) : Infinity, 0);
}
var d = a * a + b * b;
return d !== 0 ? new _Complex(a / d, -b / d).asinh() : new _Complex(a !== 0 ? a / 0 : 0, b !== 0 ? -b / 0 : 0).asinh();
}
/**
* Calculate the complex asech
*
* @returns {Complex}
*/
asech() {
var a = this["re"];
var b = this["im"];
if (this["isZero"]()) {
return _Complex["INFINITY"];
}
var d = a * a + b * b;
return d !== 0 ? new _Complex(a / d, -b / d).acosh() : new _Complex(a !== 0 ? a / 0 : 0, b !== 0 ? -b / 0 : 0).acosh();
}
/**
* Calculate the complex inverse 1/z
*
* @returns {Complex}
*/
inverse() {
if (this["isZero"]()) {
return _Complex["INFINITY"];
}
if (this["isInfinite"]()) {
return _Complex["ZERO"];
}
var a = this["re"];
var b = this["im"];
var d = a * a + b * b;
return new _Complex(a / d, -b / d);
}
/**
* Returns the complex conjugate
*
* @returns {Complex}
*/
conjugate() {
return new _Complex(this["re"], -this["im"]);
}
/**
* Gets the negated complex number
*
* @returns {Complex}
*/
neg() {
return new _Complex(-this["re"], -this["im"]);
}
/**
* Ceils the actual complex number
*
* @returns {Complex}
*/
ceil(places) {
places = Math.pow(10, places || 0);
return new _Complex(Math.ceil(this["re"] * places) / places, Math.ceil(this["im"] * places) / places);
}
/**
* Floors the actual complex number
*
* @returns {Complex}
*/
floor(places) {
places = Math.pow(10, places || 0);
return new _Complex(Math.floor(this["re"] * places) / places, Math.floor(this["im"] * places) / places);
}
/**
* Ceils the actual complex number
*
* @returns {Complex}
*/
round(places) {
places = Math.pow(10, places || 0);
return new _Complex(Math.round(this["re"] * places) / places, Math.round(this["im"] * places) / places);
}
/**
* Compares two complex numbers
*
* **Note:** new Complex(Infinity).equals(Infinity) === false
*
* @returns {boolean}
*/
equals(a, b) {
var z = new _Complex(a, b);
return Math.abs(z["re"] - this["re"]) <= _Complex["EPSILON"] && Math.abs(z["im"] - this["im"]) <= _Complex["EPSILON"];
}
/**
* Clones the actual object
*
* @returns {Complex}
*/
clone() {
return new _Complex(this["re"], this["im"]);
}
/**
* Gets a string of the actual complex number
*
* @returns {string}
*/
toString() {
var a = this["re"];
var b = this["im"];
var ret = "";
if (this["isNaN"]()) {
return "NaN";
}
if (this["isInfinite"]()) {
return "Infinity";
}
if (Math.abs(a) < _Complex["EPSILON"]) {
a = 0;
}
if (Math.abs(b) < _Complex["EPSILON"]) {
b = 0;
}
if (b === 0) {
return ret + a;
}
if (a !== 0) {
ret += a;
ret += " ";
if (b < 0) {
b = -b;
ret += "-";
} else {
ret += "+";
}
ret += " ";
} else if (b < 0) {
b = -b;
ret += "-";
}
if (1 !== b) {
ret += b;
}
return ret + "i";
}
/**
* Returns the actual number as a vector
*
* @returns {Array}
*/
toVector() {
return [this["re"], this["im"]];
}
/**
* Returns the actual real value of the current object
*
* @returns {number|null}
*/
valueOf() {
if (this["im"] === 0) {
return this["re"];
}
return null;
}
/**
* Determines whether a complex number is not on the Riemann sphere.
*
* @returns {boolean}
*/
isNaN() {
return isNaN(this["re"]) || isNaN(this["im"]);
}
/**
* Determines whether or not a complex number is at the zero pole of the
* Riemann sphere.
*
* @returns {boolean}
*/
isZero() {
return this["im"] === 0 && this["re"] === 0;
}
/**
* Determines whether a complex number is not at the infinity pole of the
* Riemann sphere.
*
* @returns {boolean}
*/
isFinite() {
return isFinite(this["re"]) && isFinite(this["im"]);
}
/**
* Determines whether or not a complex number is at the infinity pole of the
* Riemann sphere.
*
* @returns {boolean}
*/
isInfinite() {
return !(this["isNaN"]() || this["isFinite"]());
}
};
Complex["ZERO"] = new Complex(0, 0);
Complex["ONE"] = new Complex(1, 0);
Complex["I"] = new Complex(0, 1);
Complex["PI"] = new Complex(Math.PI, 0);
Complex["E"] = new Complex(Math.E, 0);
Complex["INFINITY"] = new Complex(Infinity, Infinity);
Complex["NAN"] = new Complex(NaN, NaN);
Complex["EPSILON"] = 1e-15;
// node_modules/decimal.js/decimal.mjs
var EXP_LIMIT = 9e15;
var MAX_DIGITS = 1e9;
var NUMERALS = "0123456789abcdef";
var LN10 = "2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058";
var PI = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789";
var DEFAULTS = {
// These values must be integers within the stated ranges (inclusive).
// Most of these values can be changed at run-time using the `Decimal.config` method.
// The maximum number of significant digits of the result of a calculation or base conversion.
// E.g. `Decimal.config({ precision: 20 });`
precision: 20,
// 1 to MAX_DIGITS
// The rounding mode used when rounding to `precision`.
//
// ROUND_UP 0 Away from zero.
// ROUND_DOWN 1 Towards zero.
// ROUND_CEIL 2 Towards +Infinity.
// ROUND_FLOOR 3 Towards -Infinity.
// ROUND_HALF_UP 4 Towards nearest neighbour. If equidistant, up.
// ROUND_HALF_DOWN 5 Towards nearest neighbour. If equidistant, down.
// ROUND_HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour.
// ROUND_HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity.
// ROUND_HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity.
//
// E.g.
// `Decimal.rounding = 4;`
// `Decimal.rounding = Decimal.ROUND_HALF_UP;`
rounding: 4,
// 0 to 8
// The modulo mode used when calculating the modulus: a mod n.
// The quotient (q = a / n) is calculated according to the corresponding rounding mode.
// The remainder (r) is calculated as: r = a - n * q.
//
// UP 0 The remainder is positive if the dividend is negative, else is negative.
// DOWN 1 The remainder has the same sign as the dividend (JavaScript %).
// FLOOR 3 The remainder has the same sign as the divisor (Python %).
// HALF_EVEN 6 The IEEE 754 remainder function.
// EUCLID 9 Euclidian division. q = sign(n) * floor(a / abs(n)). Always positive.
//
// Truncated division (1), floored division (3), the IEEE 754 remainder (6), and Euclidian
// division (9) are commonly used for the modulus operation. The other rounding modes can also
// be used, but they may not give useful results.
modulo: 1,
// 0 to 9
// The exponent value at and beneath which `toString` returns exponential notation.
// JavaScript numbers: -7
toExpNeg: -7,
// 0 to -EXP_LIMIT
// The exponent value at and above which `toString` returns exponential notation.
// JavaScript numbers: 21
toExpPos: 21,
// 0 to EXP_LIMIT
// The minimum exponent value, beneath which underflow to zero occurs.
// JavaScript numbers: -324 (5e-324)
minE: -EXP_LIMIT,
// -1 to -EXP_LIMIT
// The maximum exponent value, above which overflow to Infinity occurs.
// JavaScript numbers: 308 (1.7976931348623157e+308)
maxE: EXP_LIMIT,
// 1 to EXP_LIMIT
// Whether to use cryptographically-secure random number generation, if available.
crypto: false
// true/false
};
var inexact;
var quadrant;
var external = true;
var decimalError = "[DecimalError] ";
var invalidArgument = decimalError + "Invalid argument: ";
var precisionLimitExceeded = decimalError + "Precision limit exceeded";
var cryptoUnavailable = decimalError + "crypto unavailable";
var tag = "[object Decimal]";
var mathfloor = Math.floor;
var mathpow = Math.pow;
var isBinary = /^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i;
var isHex = /^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i;
var isOctal = /^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i;
var isDecimal = /^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;
var BASE = 1e7;
var LOG_BASE = 7;
var MAX_SAFE_INTEGER = 9007199254740991;
var LN10_PRECISION = LN10.length - 1;
var PI_PRECISION = PI.length - 1;
var P = { toStringTag: tag };
P.absoluteValue = P.abs = function() {
var x = new this.constructor(this);
if (x.s < 0) x.s = 1;
return finalise(x);
};
P.ceil = function() {
return finalise(new this.constructor(this), this.e + 1, 2);
};
P.clampedTo = P.clamp = function(min2, max2) {
var k, x = this, Ctor = x.constructor;
min2 = new Ctor(min2);
max2 = new Ctor(max2);
if (!min2.s || !max2.s) return new Ctor(NaN);
if (min2.gt(max2)) throw Error(invalidArgument + max2);
k = x.cmp(min2);
return k < 0 ? min2 : x.cmp(max2) > 0 ? max2 : new Ctor(x);
};
P.comparedTo = P.cmp = function(y) {
var i, j, xdL, ydL, x = this, xd = x.d, yd = (y = new x.constructor(y)).d, xs = x.s, ys = y.s;
if (!xd || !yd) {
return !xs || !ys ? NaN : xs !== ys ? xs : xd === yd ? 0 : !xd ^ xs < 0 ? 1 : -1;
}
if (!xd[0] || !yd[0]) return xd[0] ? xs : yd[0] ? -ys : 0;
if (xs !== ys) return xs;
if (x.e !== y.e) return x.e > y.e ^ xs < 0 ? 1 : -1;
xdL = xd.length;
ydL = yd.length;
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;
}
return xdL === ydL ? 0 : xdL > ydL ^ xs < 0 ? 1 : -1;
};
P.cosine = P.cos = function() {
var pr, rm, x = this, Ctor = x.constructor;
if (!x.d) return new Ctor(NaN);
if (!x.d[0]) return new Ctor(1);
pr = Ctor.precision;
rm = Ctor.rounding;
Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
Ctor.rounding = 1;
x = cosine(Ctor, toLessThanHalfPi(Ctor, x));
Ctor.precision = pr;
Ctor.rounding = rm;
return finalise(quadrant == 2 || quadrant == 3 ? x.neg() : x, pr, rm, true);
};
P.cubeRoot = P.cbrt = function() {
var e, m, n, r, rep, s, sd, t, t3, t3plusx, x = this, Ctor = x.constructor;
if (!x.isFinite() || x.isZero()) return new Ctor(x);
external = false;
s = x.s * mathpow(x.s * x, 1 / 3);
if (!s || Math.abs(s) == 1 / 0) {
n = digitsToString(x.d);
e = x.e;
if (s = (e - n.length + 1) % 3) n += s == 1 || s == -2 ? "0" : "00";
s = mathpow(n, 1 / 3);
e = mathfloor((e + 1) / 3) - (e % 3 == (e < 0 ? -1 : 2));
if (s == 1 / 0) {
n = "5e" + e;
} else {
n = s.toExponential();
n = n.slice(0, n.indexOf("e") + 1) + e;
}
r = new Ctor(n);
r.s = x.s;
} else {
r = new Ctor(s.toString());
}
sd = (e = Ctor.precision) + 3;
for (; ; ) {
t = r;
t3 = t.times(t).times(t);
t3plusx = t3.plus(x);
r = divide(t3plusx.plus(x).times(t), t3plusx.plus(t3), sd + 2, 1);
if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
n = n.slice(sd - 3, sd + 1);
if (n == "9999" || !rep && n == "4999") {
if (!rep) {
finalise(t, e + 1, 0);
if (t.times(t).times(t).eq(x)) {
r = t;
break;
}
}
sd += 4;
rep = 1;
} else {
if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
finalise(r, e + 1, 1);
m = !r.times(r).times(r).eq(x);
}
break;
}
}
}
external = true;
return finalise(r, e, Ctor.rounding, m);
};
P.decimalPlaces = P.dp = function() {
var w, d = this.d, n = NaN;
if (d) {
w = d.length - 1;
n = (w - mathfloor(this.e / LOG_BASE)) * LOG_BASE;
w = d[w];
if (w) for (; w % 10 == 0; w /= 10) n--;
if (n < 0) n = 0;
}
return n;
};
P.dividedBy = P.div = function(y) {
return divide(this, new this.constructor(y));
};
P.dividedToIntegerBy = P.divToInt = function(y) {
var x = this, Ctor = x.constructor;
return finalise(divide(x, new Ctor(y), 0, 1, 1), Ctor.precision, Ctor.rounding);
};
P.equals = P.eq = function(y) {
return this.cmp(y) === 0;
};
P.floor = function() {
return finalise(new this.constructor(this), this.e + 1, 3);
};
P.greaterThan = P.gt = function(y) {
return this.cmp(y) > 0;
};
P.greaterThanOrEqualTo = P.gte = function(y) {
var k = this.cmp(y);
return k == 1 || k === 0;
};
P.hyperbolicCosine = P.cosh = function() {
var k, n, pr, rm, len, x = this, Ctor = x.constructor, one = new Ctor(1);
if (!x.isFinite()) return new Ctor(x.s ? 1 / 0 : NaN);
if (x.isZero()) return one;
pr = Ctor.precision;
rm = Ctor.rounding;
Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
Ctor.rounding = 1;
len = x.d.length;
if (len < 32) {
k = Math.ceil(len / 3);
n = (1 / tinyPow(4, k)).toString();
} else {
k = 16;
n = "2.3283064365386962890625e-10";
}
x = taylorSeries(Ctor, 1, x.times(n), new Ctor(1), true);
var cosh2_x, i = k, d8 = new Ctor(8);
for (; i--; ) {
cosh2_x = x.times(x);
x = one.minus(cosh2_x.times(d8.minus(cosh2_x.times(d8))));
}
return finalise(x, Ctor.precision = pr, Ctor.rounding = rm, true);
};
P.hyperbolicSine = P.sinh = function() {
var k, pr, rm, len, x = this, Ctor = x.constructor;
if (!x.isFinite() || x.isZero()) return new Ctor(x);
pr = Ctor.precision;
rm = Ctor.rounding;
Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
Ctor.rounding = 1;
len = x.d.length;
if (len < 3) {
x = taylorSeries(Ctor, 2, x, x, true);
} else {
k = 1.4 * Math.sqrt(len);
k = k > 16 ? 16 : k | 0;
x = x.times(1 / tinyPow(5, k));
x = taylorSeries(Ctor, 2, x, x, true);
var sinh2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20);
for (; k--; ) {
sinh2_x = x.times(x);
x = x.times(d5.plus(sinh2_x.times(d16.times(sinh2_x).plus(d20))));
}
}
Ctor.precision = pr;
Ctor.rounding = rm;
return finalise(x, pr, rm, true);
};
P.hyperbolicTangent = P.tanh = function() {
var pr, rm, x = this, Ctor = x.constructor;
if (!x.isFinite()) return new Ctor(x.s);
if (x.isZero()) return new Ctor(x);
pr = Ctor.precision;
rm = Ctor.rounding;
Ctor.precision = pr + 7;
Ctor.rounding = 1;
return divide(x.sinh(), x.cosh(), Ctor.precision = pr, Ctor.rounding = rm);
};
P.inverseCosine = P.acos = function() {
var halfPi, x = this, Ctor = x.constructor, k = x.abs().cmp(1), pr = Ctor.precision, rm = Ctor.rounding;
if (k !== -1) {
return k === 0 ? x.isNeg() ? getPi(Ctor, pr, rm) : new Ctor(0) : new Ctor(NaN);
}
if (x.isZero()) return getPi(Ctor, pr + 4, rm).times(0.5);
Ctor.precision = pr + 6;
Ctor.rounding = 1;
x = x.asin();
halfPi = getPi(Ctor, pr + 4, rm).times(0.5);
Ctor.precision = pr;
Ctor.rounding = rm;
return halfPi.minus(x);
};
P.inverseHyperbolicCosine = P.acosh = function() {
var pr, rm, x = this, Ctor = x.constructor;
if (x.lte(1)) return new Ctor(x.eq(1) ? 0 : NaN);
if (!x.isFinite()) return new Ctor(x);
pr = Ctor.precision;
rm = Ctor.rounding;
Ctor.precision = pr + Math.max(Math.abs(x.e), x.sd()) + 4;
Ctor.rounding = 1;
external = false;
x = x.times(x).minus(1).sqrt().plus(x);
external = true;
Ctor.precision = pr;
Ctor.rounding = rm;
return x.ln();
};
P.inverseHyperbolicSine = P.asinh = function() {
var pr, rm, x = this, Ctor = x.constructor;
if (!x.isFinite() || x.isZero()) return new Ctor(x);
pr = Ctor.precision;
rm = Ctor.rounding;
Ctor.precision = pr + 2 * Math.max(Math.abs(x.e), x.sd()) + 6;
Ctor.rounding = 1;
external = false;
x = x.times(x).plus(1).sqrt().plus(x);
external = true;
Ctor.precision = pr;
Ctor.rounding = rm;
return x.ln();
};
P.inverseHyperbolicTangent = P.atanh = function() {
var pr, rm, wpr, xsd, x = this, Ctor = x.constructor;
if (!x.isFinite()) return new Ctor(NaN);
if (x.e >= 0) return new Ctor(x.abs().eq(1) ? x.s / 0 : x.isZero() ? x : NaN);
pr = Ctor.precision;
rm = Ctor.rounding;
xsd = x.sd();
if (Math.max(xsd, pr) < 2 * -x.e - 1) return finalise(new Ctor(x), pr, rm, true);
Ctor.precision = wpr = xsd - x.e;
x = divide(x.plus(1), new Ctor(1).minus(x), wpr + pr, 1);
Ctor.precision = pr + 4;
Ctor.rounding = 1;
x = x.ln();
Ctor.precision = pr;
Ctor.rounding = rm;
return x.times(0.5);
};
P.inverseSine = P.asin = function() {
var halfPi, k, pr, rm, x = this, Ctor = x.constructor;
if (x.isZero()) return new Ctor(x);
k = x.abs().cmp(1);
pr = Ctor.precision;
rm = Ctor.rounding;
if (k !== -1) {
if (k === 0) {
halfPi = getPi(Ctor, pr + 4, rm).times(0.5);
halfPi.s = x.s;
return halfPi;
}
return new Ctor(NaN);
}
Ctor.precision = pr + 6;
Ctor.rounding = 1;
x = x.div(new Ctor(1).minus(x.times(x)).sqrt().plus(1)).atan();
Ctor.precision = pr;
Ctor.rounding = rm;
return x.times(2);
};
P.inverseTangent = P.atan = function() {
var i, j, k, n, px, t, r, wpr, x2, x = this, Ctor = x.constructor, pr = Ctor.precision, rm = Ctor.rounding;
if (!x.isFinite()) {
if (!x.s) return new Ctor(NaN);
if (pr + 4 <= PI_PRECISION) {
r = getPi(Ctor, pr + 4, rm).times(0.5);
r.s = x.s;
return r;
}
} else if (x.isZero()) {
return new Ctor(x);
} else if (x.abs().eq(1) && pr + 4 <= PI_PRECISION) {
r = getPi(Ctor, pr + 4, rm).times(0.25);
r.s = x.s;
return r;
}
Ctor.precision = wpr = pr + 10;
Ctor.rounding = 1;
k = Math.min(28, wpr / LOG_BASE + 2 | 0);
for (i = k; i; --i) x = x.div(x.times(x).plus(1).sqrt().plus(1));
external = false;
j = Math.ceil(wpr / LOG_BASE);
n = 1;
x2 = x.times(x);
r = new Ctor(x);
px = x;
for (; i !== -1; ) {
px = px.times(x2);
t = r.minus(px.div(n += 2));
px = px.times(x2);
r = t.plus(px.div(n += 2));
if (r.d[j] !== void 0) for (i = j; r.d[i] === t.d[i] && i--; ) ;
}
if (k) r = r.times(2 << k - 1);
external = true;
return finalise(r, Ctor.precision = pr, Ctor.rounding = rm, true);
};
P.isFinite = function() {
return !!this.d;
};
P.isInteger = P.isInt = function() {
return !!this.d && mathfloor(this.e / LOG_BASE) > this.d.length - 2;
};
P.isNaN = function() {
return !this.s;
};
P.isNegative = P.isNeg = function() {
return this.s < 0;
};
P.isPositive = P.isPos = function() {
return this.s > 0;
};
P.isZero = function() {
return !!this.d && this.d[0] === 0;
};
P.lessThan = P.lt = function(y) {
return this.cmp(y) < 0;
};
P.lessThanOrEqualTo = P.lte = function(y) {
return this.cmp(y) < 1;
};
P.logarithm = P.log = function(base) {
var isBase10, d, denominator, k, inf, num, sd, r, arg = this, Ctor = arg.constructor, pr = Ctor.precision, rm = Ctor.rounding, guard = 5;
if (base == null) {
base = new Ctor(10);
isBase10 = true;
} else {
base = new Ctor(base);
d = base.d;
if (base.s < 0 || !d || !d[0] || base.eq(1)) return new Ctor(NaN);
isBase10 = base.eq(10);
}
d = arg.d;
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);
}
if (isBase10) {
if (d.length > 1) {
inf = true;
} else {
for (k = d[0]; k % 10 === 0; ) k /= 10;
inf = k !== 1;
}
}
external = false;
sd = pr + guard;
num = naturalLogarithm(arg, sd);
denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
r = divide(num, denominator, sd, 1);
if (checkRoundingDigits(r.d, k = pr, rm)) {
do {
sd += 10;
num = naturalLogarithm(arg, sd);
denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
r = divide(num, denominator, sd, 1);
if (!inf) {
if (+digitsToString(r.d).slice(k + 1, k + 15) + 1 == 1e14) {
r = finalise(r, pr + 1, 0);
}
break;
}
} while (checkRoundingDigits(r.d, k += 10, rm));
}
external = true;
return finalise(r, pr, rm);
};
P.minus = P.sub = function(y) {
var d, e, i, j, k, len, pr, rm, xd, xe, xLTy, yd, x = this, Ctor = x.constructor;
y = new Ctor(y);
if (!x.d || !y.d) {
if (!x.s || !y.s) y = new Ctor(NaN);
else if (x.d) y.s = -y.s;
else y = new Ctor(y.d || x.s !== y.s ? x : NaN);
return y;
}
if (x.s != y.s) {
y.s = -y.s;
return x.plus(y);
}
xd = x.d;
yd = y.d;
pr = Ctor.precision;
rm = Ctor.rounding;
if (!xd[0] || !yd[0]) {
if (yd[0]) y.s = -y.s;
else if (xd[0]) y = new Ctor(x);
else return new Ctor(rm === 3 ? -0 : 0);
return external ? finalise(y, pr, rm) : y;
}
e = mathfloor(y.e / LOG_BASE);
xe = mathfloor(x.e / LOG_BASE);
xd = xd.slice();
k = xe - e;
if (k) {
xLTy = k < 0;
if (xLTy) {
d = xd;
k = -k;
len = yd.length;
} else {
d = yd;
e = xe;
len = xd.length;
}
i = Math.max(Math.ceil(pr / LOG_BASE), len) + 2;
if (k > i) {
k = i;
d.length = 1;
}
d.reverse();
for (i = k; i--; ) d.push(0);
d.reverse();
} else {
i = xd.length;
len = yd.length;
xLTy = i < len;
if (xLTy) len = i;
for (i = 0; i < len; i++) {
if (xd[i] != yd[i]) {
xLTy = xd[i] < yd[i];
break;
}
}
k = 0;
}
if (xLTy) {
d = xd;
xd = yd;
yd = d;
y.s = -y.s;
}
len = xd.length;
for (i = yd.length - len; i > 0; --i) xd[len++] = 0;
for (i = yd.length; i > k; ) {
if (xd[--i] < yd[i]) {
for (j = i; j && xd[--j] === 0; ) xd[j] = BASE - 1;
--xd[j];
xd[i] += BASE;
}
xd[i] -= yd[i];
}
for (; xd[--len] === 0; ) xd.pop();
for (; xd[0] === 0; xd.shift()) --e;
if (!xd[0]) return new Ctor(rm === 3 ? -0 : 0);
y.d = xd;
y.e = getBase10Exponent(xd, e);
return external ? finalise(y, pr, rm) : y;
};
P.modulo = P.mod = function(y) {
var q, x = this, Ctor = x.constructor;
y = new Ctor(y);
if (!x.d || !y.s || y.d && !y.d[0]) return new Ctor(NaN);
if (!y.d || x.d && !x.d[0]) {
return finalise(new Ctor(x), Ctor.precision, Ctor.rounding);
}
external = false;
if (Ctor.modulo == 9) {
q = divide(x, y.abs(), 0, 3, 1);
q.s *= y.s;
} else {
q = divide(x, y, 0, Ctor.modulo, 1);
}
q = q.times(y);
external = true;
return x.minus(q);
};
P.naturalExponential = P.exp = function() {
return naturalExponential(this);
};
P.naturalLogarithm = P.ln = function() {
return naturalLogarithm(this);
};
P.negated = P.neg = function() {
var x = new this.constructor(this);
x.s = -x.s;
return finalise(x);
};
P.plus = P.add = function(y) {
var carry, d, e, i, k, len, pr, rm, xd, yd, x = this, Ctor = x.constructor;
y = new Ctor(y);
if (!x.d || !y.d) {
if (!x.s || !y.s) y = new Ctor(NaN);
else if (!x.d) y = new Ctor(y.d || x.s === y.s ? x : NaN);
return y;
}
if (x.s != y.s) {
y.s = -y.s;
return x.minus(y);
}
xd = x.d;
yd = y.d;
pr = Ctor.precision;
rm = Ctor.rounding;
if (!xd[0] || !yd[0]) {
if (!yd[0]) y = new Ctor(x);
return external ? finalise(y, pr, rm) : y;
}
k = mathfloor(x.e / LOG_BASE);
e = mathfloor(y.e / LOG_BASE);
xd = xd.slice();
i = k - e;
if (i) {
if (i < 0) {
d = xd;
i = -i;
len = yd.length;
} else {
d = yd;
e = k;
len = xd.length;
}
k = Math.ceil(pr / LOG_BASE);
len = k > len ? k + 1 : len + 1;
if (i > len) {
i = len;
d.length = 1;
}
d.reverse();
for (; i--; ) d.push(0);
d.reverse();
}
len = xd.length;
i = yd.length;
if (len - i < 0) {
i = len;
d = yd;
yd = xd;
xd = d;
}
for (carry = 0; i; ) {
carry = (xd[--i] = xd[i] + yd[i] + carry) / BASE | 0;
xd[i] %= BASE;
}
if (carry) {
xd.unshift(carry);
++e;
}
for (len = xd.length; xd[--len] == 0; ) xd.pop();
y.d = xd;
y.e = getBase10Exponent(xd, e);
return external ? finalise(y, pr, rm) : y;
};
P.precision = P.sd = function(z) {
var k, x = this;
if (z !== void 0 && z !== !!z && z !== 1 && z !== 0) throw Error(invalidArgument + z);
if (x.d) {
k = getPrecision(x.d);
if (z && x.e + 1 > k) k = x.e + 1;
} else {
k = NaN;
}
return k;
};
P.round = function() {
var x = this, Ctor = x.constructor;
return finalise(new Ctor(x), x.e + 1, Ctor.rounding);
};
P.sine = P.sin = function() {
var pr, rm, x = this, Ctor = x.constructor;
if (!x.isFinite()) return new Ctor(NaN);
if (x.isZero()) return new Ctor(x);
pr = Ctor.precision;
rm = Ctor.rounding;
Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
Ctor.rounding = 1;
x = sine(Ctor, toLessThanHalfPi(Ctor, x));
Ctor.precision = pr;
Ctor.rounding = rm;
return finalise(quadrant > 2 ? x.neg() : x, pr, rm, true);
};
P.squareRoot = P.sqrt = function() {
var m, n, sd, r, rep, t, x = this, d = x.d, e = x.e, s = x.s, Ctor = x.constructor;
if (s !== 1 || !d || !d[0]) {
return new Ctor(!s || s < 0 && (!d || d[0]) ? NaN : d ? x : 1 / 0);
}
external = false;
s = Math.sqrt(+x);
if (s == 0 || s == 1 / 0) {
n = digitsToString(d);
if ((n.length + e) % 2 == 0) n += "0";
s = Math.sqrt(n);
e = mathfloor((e + 1) / 2) - (e < 0 || e % 2);
if (s == 1 / 0) {
n = "5e" + e;
} else {
n = s.toExponential();
n = n.slice(0, n.indexOf("e") + 1) + e;
}
r = new Ctor(n);
} else {
r = new Ctor(s.toString());
}
sd = (e = Ctor.precision) + 3;
for (; ; ) {
t = r;
r = t.plus(divide(x, t, sd + 2, 1)).times(0.5);
if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
n = n.slice(sd - 3, sd + 1);
if (n == "9999" || !rep && n == "4999") {
if (!rep) {
finalise(t, e + 1, 0);
if (t.times(t).eq(x)) {
r = t;
break;
}
}
sd += 4;
rep = 1;
} else {
if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
finalise(r, e + 1, 1);
m = !r.times(r).eq(x);
}
break;
}
}
}
external = true;
return finalise(r, e, Ctor.rounding, m);
};
P.tangent = P.tan = function() {
var pr, rm, x = this, Ctor = x.constructor;
if (!x.isFinite()) return new Ctor(NaN);
if (x.isZero()) return new Ctor(x);
pr = Ctor.precision;
rm = Ctor.rounding;
Ctor.precision = pr + 10;
Ctor.rounding = 1;
x = x.sin();
x.s = 1;
x = divide(x, new Ctor(1).minus(x.times(x)).sqrt(), pr + 10, 0);
Ctor.precision = pr;
Ctor.rounding = rm;
return finalise(quadrant == 2 || quadrant == 4 ? x.neg() : x, pr, rm, true);
};
P.times = P.mul = function(y) {
var carry, e, i, k, r, rL, t, xdL, ydL, x = this, Ctor = x.constructor, xd = x.d, yd = (y = new Ctor(y)).d;
y.s *= x.s;
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);
}
e = mathfloor(x.e / LOG_BASE) + mathfloor(y.e / LOG_BASE);
xdL = xd.length;
ydL = yd.length;
if (xdL < ydL) {
r = xd;
xd = yd;
yd = r;
rL = xdL;
xdL = ydL;
ydL = rL;
}
r = [];
rL = xdL + ydL;
for (i = rL;