@wzyjs/utils 0.3.6 → 0.3.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/common/ai/index.d.ts +2 -0
- package/dist/common/index.d.ts +3 -3
- package/dist/node.cjs.js +2759 -39
- package/dist/node.d.ts +2 -2
- package/dist/node.esm.js +2763 -45
- package/dist/web.cjs.js +20447 -17730
- package/dist/web.d.ts +5 -5
- package/dist/web.esm.js +20302 -17587
- package/package.json +2 -2
- /package/dist/common/{dayjs.d.ts → dayjs/index.d.ts} +0 -0
- /package/dist/common/{enum.d.ts → enum/index.d.ts} +0 -0
- /package/dist/common/{image.d.ts → image/index.d.ts} +0 -0
package/dist/node.esm.js
CHANGED
|
@@ -358,9 +358,9 @@ var require_lodash = __commonJS((exports, module) => {
|
|
|
358
358
|
var freeProcess = moduleExports && freeGlobal.process;
|
|
359
359
|
var nodeUtil = function() {
|
|
360
360
|
try {
|
|
361
|
-
var
|
|
362
|
-
if (
|
|
363
|
-
return
|
|
361
|
+
var types2 = freeModule && freeModule.require && freeModule.require("util").types;
|
|
362
|
+
if (types2) {
|
|
363
|
+
return types2;
|
|
364
364
|
}
|
|
365
365
|
return freeProcess && freeProcess.binding && freeProcess.binding("util");
|
|
366
366
|
} catch (e) {}
|
|
@@ -1189,7 +1189,7 @@ var require_lodash = __commonJS((exports, module) => {
|
|
|
1189
1189
|
if (typeof func != "function") {
|
|
1190
1190
|
throw new TypeError2(FUNC_ERROR_TEXT);
|
|
1191
1191
|
}
|
|
1192
|
-
return
|
|
1192
|
+
return setTimeout2(function() {
|
|
1193
1193
|
func.apply(undefined2, args);
|
|
1194
1194
|
}, wait);
|
|
1195
1195
|
}
|
|
@@ -2998,7 +2998,7 @@ var require_lodash = __commonJS((exports, module) => {
|
|
|
2998
2998
|
return object[key];
|
|
2999
2999
|
}
|
|
3000
3000
|
var setData = shortOut(baseSetData);
|
|
3001
|
-
var
|
|
3001
|
+
var setTimeout2 = ctxSetTimeout || function(func, wait) {
|
|
3002
3002
|
return root.setTimeout(func, wait);
|
|
3003
3003
|
};
|
|
3004
3004
|
var setToString = shortOut(baseSetToString);
|
|
@@ -3788,7 +3788,7 @@ var require_lodash = __commonJS((exports, module) => {
|
|
|
3788
3788
|
}
|
|
3789
3789
|
function leadingEdge(time) {
|
|
3790
3790
|
lastInvokeTime = time;
|
|
3791
|
-
timerId =
|
|
3791
|
+
timerId = setTimeout2(timerExpired, wait);
|
|
3792
3792
|
return leading ? invokeFunc(time) : result2;
|
|
3793
3793
|
}
|
|
3794
3794
|
function remainingWait(time) {
|
|
@@ -3804,7 +3804,7 @@ var require_lodash = __commonJS((exports, module) => {
|
|
|
3804
3804
|
if (shouldInvoke(time)) {
|
|
3805
3805
|
return trailingEdge(time);
|
|
3806
3806
|
}
|
|
3807
|
-
timerId =
|
|
3807
|
+
timerId = setTimeout2(timerExpired, remainingWait(time));
|
|
3808
3808
|
}
|
|
3809
3809
|
function trailingEdge(time) {
|
|
3810
3810
|
timerId = undefined2;
|
|
@@ -3835,12 +3835,12 @@ var require_lodash = __commonJS((exports, module) => {
|
|
|
3835
3835
|
}
|
|
3836
3836
|
if (maxing) {
|
|
3837
3837
|
clearTimeout(timerId);
|
|
3838
|
-
timerId =
|
|
3838
|
+
timerId = setTimeout2(timerExpired, wait);
|
|
3839
3839
|
return invokeFunc(lastCallTime);
|
|
3840
3840
|
}
|
|
3841
3841
|
}
|
|
3842
3842
|
if (timerId === undefined2) {
|
|
3843
|
-
timerId =
|
|
3843
|
+
timerId = setTimeout2(timerExpired, wait);
|
|
3844
3844
|
}
|
|
3845
3845
|
return result2;
|
|
3846
3846
|
}
|
|
@@ -4047,7 +4047,7 @@ var require_lodash = __commonJS((exports, module) => {
|
|
|
4047
4047
|
var tag = baseGetTag(value);
|
|
4048
4048
|
return tag == errorTag || tag == domExcTag || typeof value.message == "string" && typeof value.name == "string" && !isPlainObject(value);
|
|
4049
4049
|
}
|
|
4050
|
-
function
|
|
4050
|
+
function isFinite2(value) {
|
|
4051
4051
|
return typeof value == "number" && nativeIsFinite(value);
|
|
4052
4052
|
}
|
|
4053
4053
|
function isFunction(value) {
|
|
@@ -4078,7 +4078,7 @@ var require_lodash = __commonJS((exports, module) => {
|
|
|
4078
4078
|
customizer = typeof customizer == "function" ? customizer : undefined2;
|
|
4079
4079
|
return baseIsMatch(object, source, getMatchData(source), customizer);
|
|
4080
4080
|
}
|
|
4081
|
-
function
|
|
4081
|
+
function isNaN2(value) {
|
|
4082
4082
|
return isNumber(value) && value != +value;
|
|
4083
4083
|
}
|
|
4084
4084
|
function isNative(value) {
|
|
@@ -4737,7 +4737,7 @@ __p += '`;
|
|
|
4737
4737
|
}
|
|
4738
4738
|
return result2 + omission;
|
|
4739
4739
|
}
|
|
4740
|
-
function
|
|
4740
|
+
function unescape2(string) {
|
|
4741
4741
|
string = toString(string);
|
|
4742
4742
|
return string && reHasEscapedHtml.test(string) ? string.replace(reEscapedHtml, unescapeHtmlChar) : string;
|
|
4743
4743
|
}
|
|
@@ -5161,14 +5161,14 @@ __p += '`;
|
|
|
5161
5161
|
lodash.isEqual = isEqual;
|
|
5162
5162
|
lodash.isEqualWith = isEqualWith;
|
|
5163
5163
|
lodash.isError = isError;
|
|
5164
|
-
lodash.isFinite =
|
|
5164
|
+
lodash.isFinite = isFinite2;
|
|
5165
5165
|
lodash.isFunction = isFunction;
|
|
5166
5166
|
lodash.isInteger = isInteger;
|
|
5167
5167
|
lodash.isLength = isLength;
|
|
5168
5168
|
lodash.isMap = isMap;
|
|
5169
5169
|
lodash.isMatch = isMatch;
|
|
5170
5170
|
lodash.isMatchWith = isMatchWith;
|
|
5171
|
-
lodash.isNaN =
|
|
5171
|
+
lodash.isNaN = isNaN2;
|
|
5172
5172
|
lodash.isNative = isNative;
|
|
5173
5173
|
lodash.isNil = isNil;
|
|
5174
5174
|
lodash.isNull = isNull;
|
|
@@ -5250,7 +5250,7 @@ __p += '`;
|
|
|
5250
5250
|
lodash.trimEnd = trimEnd;
|
|
5251
5251
|
lodash.trimStart = trimStart;
|
|
5252
5252
|
lodash.truncate = truncate;
|
|
5253
|
-
lodash.unescape =
|
|
5253
|
+
lodash.unescape = unescape2;
|
|
5254
5254
|
lodash.uniqueId = uniqueId;
|
|
5255
5255
|
lodash.upperCase = upperCase;
|
|
5256
5256
|
lodash.upperFirst = upperFirst;
|
|
@@ -5441,14 +5441,6 @@ __p += '`;
|
|
|
5441
5441
|
}).call(exports);
|
|
5442
5442
|
});
|
|
5443
5443
|
|
|
5444
|
-
// src/node.ts
|
|
5445
|
-
var import_lodash = __toESM(require_lodash(), 1);
|
|
5446
|
-
import { default as default2, DataStoreOptions } from "nedb";
|
|
5447
|
-
import * as cheerio from "cheerio";
|
|
5448
|
-
import { default as default4 } from "axios";
|
|
5449
|
-
import { default as default5 } from "json5";
|
|
5450
|
-
import { default as default6 } from "consola";
|
|
5451
|
-
|
|
5452
5444
|
// ../../node_modules/zod/v3/external.js
|
|
5453
5445
|
var exports_external = {};
|
|
5454
5446
|
__export(exports_external, {
|
|
@@ -9422,6 +9414,14 @@ var coerce = {
|
|
|
9422
9414
|
date: (arg) => ZodDate.create({ ...arg, coerce: true })
|
|
9423
9415
|
};
|
|
9424
9416
|
var NEVER = INVALID;
|
|
9417
|
+
// src/node.ts
|
|
9418
|
+
var import_lodash = __toESM(require_lodash(), 1);
|
|
9419
|
+
import * as cheerio from "cheerio";
|
|
9420
|
+
import { default as default3 } from "axios";
|
|
9421
|
+
import { default as default4 } from "json5";
|
|
9422
|
+
import { default as default5 } from "consola";
|
|
9423
|
+
import { default as default6, DataStoreOptions } from "nedb";
|
|
9424
|
+
|
|
9425
9425
|
// src/common/ai/302/axios.ts
|
|
9426
9426
|
import axios_ from "axios";
|
|
9427
9427
|
var key = process.env.AI_302_KEY || process.env.NEXT_PUBLIC_AI_302_KEY;
|
|
@@ -9460,6 +9460,7 @@ var chat = async (message, model = "gpt-4.1", isParse = true) => {
|
|
|
9460
9460
|
throw new Error(error?.response?.statusText || error?.message || "未知原因");
|
|
9461
9461
|
}
|
|
9462
9462
|
};
|
|
9463
|
+
|
|
9463
9464
|
// src/common/ai/keling/utils.ts
|
|
9464
9465
|
import * as url from "url";
|
|
9465
9466
|
import * as path from "path";
|
|
@@ -9673,29 +9674,2694 @@ var generateVideo = async (imageUrl, prompt) => {
|
|
|
9673
9674
|
console.log(666, "任务 id", taskId);
|
|
9674
9675
|
return taskId;
|
|
9675
9676
|
};
|
|
9676
|
-
// src/common/
|
|
9677
|
-
|
|
9678
|
-
|
|
9677
|
+
// src/common/base/string.ts
|
|
9678
|
+
var getChineseByStr = (str) => {
|
|
9679
|
+
if (!str) {
|
|
9680
|
+
return "";
|
|
9681
|
+
}
|
|
9682
|
+
const match = str.match(/[\u4e00-\u9fa5]/g);
|
|
9683
|
+
if (!match) {
|
|
9684
|
+
return "";
|
|
9685
|
+
}
|
|
9686
|
+
return match.join("");
|
|
9687
|
+
};
|
|
9688
|
+
var getStrLength = (value) => {
|
|
9689
|
+
if (!value) {
|
|
9690
|
+
return 0;
|
|
9691
|
+
}
|
|
9692
|
+
const chineseLength = getChineseByStr(value).length;
|
|
9693
|
+
return value.length - chineseLength + chineseLength * 2;
|
|
9694
|
+
};
|
|
9695
|
+
var replaceAll = (str, searchValue, replaceValue) => {
|
|
9696
|
+
if (!str || !searchValue || !replaceValue) {
|
|
9697
|
+
return str || "";
|
|
9698
|
+
}
|
|
9699
|
+
str = str.replace(searchValue, replaceValue);
|
|
9700
|
+
if (!str.includes(searchValue)) {
|
|
9701
|
+
return str;
|
|
9702
|
+
}
|
|
9703
|
+
return replaceAll(str, searchValue, replaceValue);
|
|
9704
|
+
};
|
|
9705
|
+
var replaceByRules = (str, rules) => {
|
|
9706
|
+
rules.forEach(([searchValue, replaceValue]) => {
|
|
9707
|
+
str = str.replaceAll(searchValue, replaceValue);
|
|
9708
|
+
});
|
|
9709
|
+
return str;
|
|
9710
|
+
};
|
|
9711
|
+
var replaceByVariables = (prompt, variables) => {
|
|
9712
|
+
if (!variables || Object.keys(variables).length === 0) {
|
|
9713
|
+
return prompt;
|
|
9714
|
+
}
|
|
9715
|
+
const regex = /\{\{\s*(\w+)\s*\}\}/g;
|
|
9716
|
+
return prompt.replace(regex, (match, p1) => {
|
|
9717
|
+
return variables[p1] ?? match;
|
|
9718
|
+
});
|
|
9719
|
+
};
|
|
9720
|
+
var getType = (value) => {
|
|
9721
|
+
return Object.prototype.toString.call(value).slice(8, -1);
|
|
9722
|
+
};
|
|
9723
|
+
var amount = (str) => {
|
|
9724
|
+
const reg = /(?=(?!\b)(\d{3})+$)/g;
|
|
9725
|
+
return str.replace(reg, ",");
|
|
9726
|
+
};
|
|
9727
|
+
var jsonParse = (value) => {
|
|
9728
|
+
if (typeof value === "object") {
|
|
9729
|
+
return value;
|
|
9730
|
+
}
|
|
9679
9731
|
try {
|
|
9680
|
-
|
|
9681
|
-
|
|
9732
|
+
return JSON.parse(value);
|
|
9733
|
+
} catch (err) {
|
|
9734
|
+
return {};
|
|
9735
|
+
}
|
|
9736
|
+
};
|
|
9737
|
+
var isJson = (str) => {
|
|
9738
|
+
try {
|
|
9739
|
+
if (getType(JSON.parse(str)) === "Object") {
|
|
9740
|
+
return true;
|
|
9741
|
+
}
|
|
9742
|
+
} catch (e) {}
|
|
9743
|
+
return false;
|
|
9744
|
+
};
|
|
9745
|
+
var toString = (value) => {
|
|
9746
|
+
return Object.prototype.toString.call(value).slice(8, -1) === "object" ? JSON.stringify(value) : String(value);
|
|
9747
|
+
};
|
|
9748
|
+
var getRandomColor = () => {
|
|
9749
|
+
const color = Math.floor(Math.random() * 16777215).toString(16);
|
|
9750
|
+
if (color.length === 6) {
|
|
9751
|
+
return color;
|
|
9752
|
+
} else {
|
|
9753
|
+
return getRandomColor();
|
|
9754
|
+
}
|
|
9755
|
+
};
|
|
9756
|
+
var getRandomString = (length = 4) => {
|
|
9757
|
+
return Math.random().toString(36).substr(2, length);
|
|
9758
|
+
};
|
|
9759
|
+
var getChinese = (str) => {
|
|
9760
|
+
if (str == null || str === "") {
|
|
9761
|
+
return "";
|
|
9762
|
+
}
|
|
9763
|
+
const res = str.match(/[\u4e00-\u9fa5]/g);
|
|
9764
|
+
if (!res) {
|
|
9765
|
+
return "";
|
|
9766
|
+
}
|
|
9767
|
+
return res.join("");
|
|
9768
|
+
};
|
|
9769
|
+
var getSliceStr = (str, before, after) => {
|
|
9770
|
+
return str.slice(str.indexOf(before) + before.length, str.lastIndexOf(after));
|
|
9771
|
+
};
|
|
9772
|
+
var getProxyUrl = (url2) => {
|
|
9773
|
+
const beforeUrl = "https://1141871752167714.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/a.LATEST/proxy/?url=";
|
|
9774
|
+
return beforeUrl + url2;
|
|
9775
|
+
};
|
|
9776
|
+
var getLength = (value) => {
|
|
9777
|
+
const chineseLength = getChinese(value).length;
|
|
9778
|
+
return value.length - chineseLength + chineseLength * 2;
|
|
9779
|
+
};
|
|
9780
|
+
var getCookie = (name) => {
|
|
9781
|
+
const reg = new RegExp(`(^| )${name}=([^;]*)(;|$)`);
|
|
9782
|
+
const arr = document.cookie.match(reg);
|
|
9783
|
+
return arr ? unescape(arr[2]) : null;
|
|
9784
|
+
};
|
|
9785
|
+
var generateAlphabetArray = (n) => {
|
|
9786
|
+
return Array.from({ length: n }, (_, i) => String.fromCharCode(97 + i));
|
|
9787
|
+
};
|
|
9788
|
+
var levenshteinDistance = (a, b) => {
|
|
9789
|
+
const matrix = [];
|
|
9790
|
+
for (let i = 0;i <= b.length; i++) {
|
|
9791
|
+
matrix[i] = [i];
|
|
9792
|
+
}
|
|
9793
|
+
for (let j = 0;j <= a.length; j++) {
|
|
9794
|
+
matrix[0][j] = j;
|
|
9795
|
+
}
|
|
9796
|
+
for (let i = 1;i <= b.length; i++) {
|
|
9797
|
+
for (let j = 1;j <= a.length; j++) {
|
|
9798
|
+
if (b[i - 1] === a[j - 1]) {
|
|
9799
|
+
matrix[i][j] = matrix[i - 1][j - 1];
|
|
9800
|
+
} else {
|
|
9801
|
+
matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, matrix[i][j - 1] + 1, matrix[i - 1][j] + 1);
|
|
9802
|
+
}
|
|
9803
|
+
}
|
|
9804
|
+
}
|
|
9805
|
+
return matrix[b.length][a.length];
|
|
9806
|
+
};
|
|
9807
|
+
var generateUniqueFileName = (name) => {
|
|
9808
|
+
const match = name.match(/^(.*?)(\.[^.]*$|$)/);
|
|
9809
|
+
if (match) {
|
|
9810
|
+
const namePart = match[1];
|
|
9811
|
+
const extensionPart = match[2];
|
|
9812
|
+
if (extensionPart) {
|
|
9813
|
+
return `${namePart}_${Date.now()}${extensionPart}`;
|
|
9814
|
+
} else {
|
|
9815
|
+
return `${namePart}_${Date.now()}.dat`;
|
|
9816
|
+
}
|
|
9817
|
+
}
|
|
9818
|
+
throw new Error("Invalid filename format.");
|
|
9819
|
+
};
|
|
9820
|
+
|
|
9821
|
+
// ../../node_modules/decimal.js/decimal.mjs
|
|
9822
|
+
/*!
|
|
9823
|
+
* decimal.js v10.6.0
|
|
9824
|
+
* An arbitrary-precision Decimal type for JavaScript.
|
|
9825
|
+
* https://github.com/MikeMcl/decimal.js
|
|
9826
|
+
* Copyright (c) 2025 Michael Mclaughlin <M8ch88l@gmail.com>
|
|
9827
|
+
* MIT Licence
|
|
9828
|
+
*/
|
|
9829
|
+
var EXP_LIMIT = 9000000000000000;
|
|
9830
|
+
var MAX_DIGITS = 1e9;
|
|
9831
|
+
var NUMERALS = "0123456789abcdef";
|
|
9832
|
+
var LN10 = "2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058";
|
|
9833
|
+
var PI = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789";
|
|
9834
|
+
var DEFAULTS = {
|
|
9835
|
+
precision: 20,
|
|
9836
|
+
rounding: 4,
|
|
9837
|
+
modulo: 1,
|
|
9838
|
+
toExpNeg: -7,
|
|
9839
|
+
toExpPos: 21,
|
|
9840
|
+
minE: -EXP_LIMIT,
|
|
9841
|
+
maxE: EXP_LIMIT,
|
|
9842
|
+
crypto: false
|
|
9843
|
+
};
|
|
9844
|
+
var inexact;
|
|
9845
|
+
var quadrant;
|
|
9846
|
+
var external2 = true;
|
|
9847
|
+
var decimalError = "[DecimalError] ";
|
|
9848
|
+
var invalidArgument = decimalError + "Invalid argument: ";
|
|
9849
|
+
var precisionLimitExceeded = decimalError + "Precision limit exceeded";
|
|
9850
|
+
var cryptoUnavailable = decimalError + "crypto unavailable";
|
|
9851
|
+
var tag = "[object Decimal]";
|
|
9852
|
+
var mathfloor = Math.floor;
|
|
9853
|
+
var mathpow = Math.pow;
|
|
9854
|
+
var isBinary = /^0b([01]+(\.[01]*)?|\.[01]+)(p[+-]?\d+)?$/i;
|
|
9855
|
+
var isHex = /^0x([0-9a-f]+(\.[0-9a-f]*)?|\.[0-9a-f]+)(p[+-]?\d+)?$/i;
|
|
9856
|
+
var isOctal = /^0o([0-7]+(\.[0-7]*)?|\.[0-7]+)(p[+-]?\d+)?$/i;
|
|
9857
|
+
var isDecimal = /^(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i;
|
|
9858
|
+
var BASE = 1e7;
|
|
9859
|
+
var LOG_BASE = 7;
|
|
9860
|
+
var MAX_SAFE_INTEGER = 9007199254740991;
|
|
9861
|
+
var LN10_PRECISION = LN10.length - 1;
|
|
9862
|
+
var PI_PRECISION = PI.length - 1;
|
|
9863
|
+
var P = { toStringTag: tag };
|
|
9864
|
+
P.absoluteValue = P.abs = function() {
|
|
9865
|
+
var x = new this.constructor(this);
|
|
9866
|
+
if (x.s < 0)
|
|
9867
|
+
x.s = 1;
|
|
9868
|
+
return finalise(x);
|
|
9869
|
+
};
|
|
9870
|
+
P.ceil = function() {
|
|
9871
|
+
return finalise(new this.constructor(this), this.e + 1, 2);
|
|
9872
|
+
};
|
|
9873
|
+
P.clampedTo = P.clamp = function(min, max) {
|
|
9874
|
+
var k, x = this, Ctor = x.constructor;
|
|
9875
|
+
min = new Ctor(min);
|
|
9876
|
+
max = new Ctor(max);
|
|
9877
|
+
if (!min.s || !max.s)
|
|
9878
|
+
return new Ctor(NaN);
|
|
9879
|
+
if (min.gt(max))
|
|
9880
|
+
throw Error(invalidArgument + max);
|
|
9881
|
+
k = x.cmp(min);
|
|
9882
|
+
return k < 0 ? min : x.cmp(max) > 0 ? max : new Ctor(x);
|
|
9883
|
+
};
|
|
9884
|
+
P.comparedTo = P.cmp = function(y) {
|
|
9885
|
+
var i, j, xdL, ydL, x = this, xd = x.d, yd = (y = new x.constructor(y)).d, xs = x.s, ys = y.s;
|
|
9886
|
+
if (!xd || !yd) {
|
|
9887
|
+
return !xs || !ys ? NaN : xs !== ys ? xs : xd === yd ? 0 : !xd ^ xs < 0 ? 1 : -1;
|
|
9888
|
+
}
|
|
9889
|
+
if (!xd[0] || !yd[0])
|
|
9890
|
+
return xd[0] ? xs : yd[0] ? -ys : 0;
|
|
9891
|
+
if (xs !== ys)
|
|
9892
|
+
return xs;
|
|
9893
|
+
if (x.e !== y.e)
|
|
9894
|
+
return x.e > y.e ^ xs < 0 ? 1 : -1;
|
|
9895
|
+
xdL = xd.length;
|
|
9896
|
+
ydL = yd.length;
|
|
9897
|
+
for (i = 0, j = xdL < ydL ? xdL : ydL;i < j; ++i) {
|
|
9898
|
+
if (xd[i] !== yd[i])
|
|
9899
|
+
return xd[i] > yd[i] ^ xs < 0 ? 1 : -1;
|
|
9900
|
+
}
|
|
9901
|
+
return xdL === ydL ? 0 : xdL > ydL ^ xs < 0 ? 1 : -1;
|
|
9902
|
+
};
|
|
9903
|
+
P.cosine = P.cos = function() {
|
|
9904
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
9905
|
+
if (!x.d)
|
|
9906
|
+
return new Ctor(NaN);
|
|
9907
|
+
if (!x.d[0])
|
|
9908
|
+
return new Ctor(1);
|
|
9909
|
+
pr = Ctor.precision;
|
|
9910
|
+
rm = Ctor.rounding;
|
|
9911
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
|
|
9912
|
+
Ctor.rounding = 1;
|
|
9913
|
+
x = cosine(Ctor, toLessThanHalfPi(Ctor, x));
|
|
9914
|
+
Ctor.precision = pr;
|
|
9915
|
+
Ctor.rounding = rm;
|
|
9916
|
+
return finalise(quadrant == 2 || quadrant == 3 ? x.neg() : x, pr, rm, true);
|
|
9917
|
+
};
|
|
9918
|
+
P.cubeRoot = P.cbrt = function() {
|
|
9919
|
+
var e, m, n, r, rep, s, sd, t, t3, t3plusx, x = this, Ctor = x.constructor;
|
|
9920
|
+
if (!x.isFinite() || x.isZero())
|
|
9921
|
+
return new Ctor(x);
|
|
9922
|
+
external2 = false;
|
|
9923
|
+
s = x.s * mathpow(x.s * x, 1 / 3);
|
|
9924
|
+
if (!s || Math.abs(s) == 1 / 0) {
|
|
9925
|
+
n = digitsToString(x.d);
|
|
9926
|
+
e = x.e;
|
|
9927
|
+
if (s = (e - n.length + 1) % 3)
|
|
9928
|
+
n += s == 1 || s == -2 ? "0" : "00";
|
|
9929
|
+
s = mathpow(n, 1 / 3);
|
|
9930
|
+
e = mathfloor((e + 1) / 3) - (e % 3 == (e < 0 ? -1 : 2));
|
|
9931
|
+
if (s == 1 / 0) {
|
|
9932
|
+
n = "5e" + e;
|
|
9933
|
+
} else {
|
|
9934
|
+
n = s.toExponential();
|
|
9935
|
+
n = n.slice(0, n.indexOf("e") + 1) + e;
|
|
9936
|
+
}
|
|
9937
|
+
r = new Ctor(n);
|
|
9938
|
+
r.s = x.s;
|
|
9939
|
+
} else {
|
|
9940
|
+
r = new Ctor(s.toString());
|
|
9941
|
+
}
|
|
9942
|
+
sd = (e = Ctor.precision) + 3;
|
|
9943
|
+
for (;; ) {
|
|
9944
|
+
t = r;
|
|
9945
|
+
t3 = t.times(t).times(t);
|
|
9946
|
+
t3plusx = t3.plus(x);
|
|
9947
|
+
r = divide(t3plusx.plus(x).times(t), t3plusx.plus(t3), sd + 2, 1);
|
|
9948
|
+
if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
|
|
9949
|
+
n = n.slice(sd - 3, sd + 1);
|
|
9950
|
+
if (n == "9999" || !rep && n == "4999") {
|
|
9951
|
+
if (!rep) {
|
|
9952
|
+
finalise(t, e + 1, 0);
|
|
9953
|
+
if (t.times(t).times(t).eq(x)) {
|
|
9954
|
+
r = t;
|
|
9955
|
+
break;
|
|
9956
|
+
}
|
|
9957
|
+
}
|
|
9958
|
+
sd += 4;
|
|
9959
|
+
rep = 1;
|
|
9960
|
+
} else {
|
|
9961
|
+
if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
|
|
9962
|
+
finalise(r, e + 1, 1);
|
|
9963
|
+
m = !r.times(r).times(r).eq(x);
|
|
9964
|
+
}
|
|
9965
|
+
break;
|
|
9966
|
+
}
|
|
9967
|
+
}
|
|
9968
|
+
}
|
|
9969
|
+
external2 = true;
|
|
9970
|
+
return finalise(r, e, Ctor.rounding, m);
|
|
9971
|
+
};
|
|
9972
|
+
P.decimalPlaces = P.dp = function() {
|
|
9973
|
+
var w, d = this.d, n = NaN;
|
|
9974
|
+
if (d) {
|
|
9975
|
+
w = d.length - 1;
|
|
9976
|
+
n = (w - mathfloor(this.e / LOG_BASE)) * LOG_BASE;
|
|
9977
|
+
w = d[w];
|
|
9978
|
+
if (w)
|
|
9979
|
+
for (;w % 10 == 0; w /= 10)
|
|
9980
|
+
n--;
|
|
9981
|
+
if (n < 0)
|
|
9982
|
+
n = 0;
|
|
9983
|
+
}
|
|
9984
|
+
return n;
|
|
9985
|
+
};
|
|
9986
|
+
P.dividedBy = P.div = function(y) {
|
|
9987
|
+
return divide(this, new this.constructor(y));
|
|
9988
|
+
};
|
|
9989
|
+
P.dividedToIntegerBy = P.divToInt = function(y) {
|
|
9990
|
+
var x = this, Ctor = x.constructor;
|
|
9991
|
+
return finalise(divide(x, new Ctor(y), 0, 1, 1), Ctor.precision, Ctor.rounding);
|
|
9992
|
+
};
|
|
9993
|
+
P.equals = P.eq = function(y) {
|
|
9994
|
+
return this.cmp(y) === 0;
|
|
9995
|
+
};
|
|
9996
|
+
P.floor = function() {
|
|
9997
|
+
return finalise(new this.constructor(this), this.e + 1, 3);
|
|
9998
|
+
};
|
|
9999
|
+
P.greaterThan = P.gt = function(y) {
|
|
10000
|
+
return this.cmp(y) > 0;
|
|
10001
|
+
};
|
|
10002
|
+
P.greaterThanOrEqualTo = P.gte = function(y) {
|
|
10003
|
+
var k = this.cmp(y);
|
|
10004
|
+
return k == 1 || k === 0;
|
|
10005
|
+
};
|
|
10006
|
+
P.hyperbolicCosine = P.cosh = function() {
|
|
10007
|
+
var k, n, pr, rm, len, x = this, Ctor = x.constructor, one = new Ctor(1);
|
|
10008
|
+
if (!x.isFinite())
|
|
10009
|
+
return new Ctor(x.s ? 1 / 0 : NaN);
|
|
10010
|
+
if (x.isZero())
|
|
10011
|
+
return one;
|
|
10012
|
+
pr = Ctor.precision;
|
|
10013
|
+
rm = Ctor.rounding;
|
|
10014
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
|
|
10015
|
+
Ctor.rounding = 1;
|
|
10016
|
+
len = x.d.length;
|
|
10017
|
+
if (len < 32) {
|
|
10018
|
+
k = Math.ceil(len / 3);
|
|
10019
|
+
n = (1 / tinyPow(4, k)).toString();
|
|
10020
|
+
} else {
|
|
10021
|
+
k = 16;
|
|
10022
|
+
n = "2.3283064365386962890625e-10";
|
|
10023
|
+
}
|
|
10024
|
+
x = taylorSeries(Ctor, 1, x.times(n), new Ctor(1), true);
|
|
10025
|
+
var cosh2_x, i = k, d8 = new Ctor(8);
|
|
10026
|
+
for (;i--; ) {
|
|
10027
|
+
cosh2_x = x.times(x);
|
|
10028
|
+
x = one.minus(cosh2_x.times(d8.minus(cosh2_x.times(d8))));
|
|
10029
|
+
}
|
|
10030
|
+
return finalise(x, Ctor.precision = pr, Ctor.rounding = rm, true);
|
|
10031
|
+
};
|
|
10032
|
+
P.hyperbolicSine = P.sinh = function() {
|
|
10033
|
+
var k, pr, rm, len, x = this, Ctor = x.constructor;
|
|
10034
|
+
if (!x.isFinite() || x.isZero())
|
|
10035
|
+
return new Ctor(x);
|
|
10036
|
+
pr = Ctor.precision;
|
|
10037
|
+
rm = Ctor.rounding;
|
|
10038
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + 4;
|
|
10039
|
+
Ctor.rounding = 1;
|
|
10040
|
+
len = x.d.length;
|
|
10041
|
+
if (len < 3) {
|
|
10042
|
+
x = taylorSeries(Ctor, 2, x, x, true);
|
|
10043
|
+
} else {
|
|
10044
|
+
k = 1.4 * Math.sqrt(len);
|
|
10045
|
+
k = k > 16 ? 16 : k | 0;
|
|
10046
|
+
x = x.times(1 / tinyPow(5, k));
|
|
10047
|
+
x = taylorSeries(Ctor, 2, x, x, true);
|
|
10048
|
+
var sinh2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20);
|
|
10049
|
+
for (;k--; ) {
|
|
10050
|
+
sinh2_x = x.times(x);
|
|
10051
|
+
x = x.times(d5.plus(sinh2_x.times(d16.times(sinh2_x).plus(d20))));
|
|
10052
|
+
}
|
|
10053
|
+
}
|
|
10054
|
+
Ctor.precision = pr;
|
|
10055
|
+
Ctor.rounding = rm;
|
|
10056
|
+
return finalise(x, pr, rm, true);
|
|
10057
|
+
};
|
|
10058
|
+
P.hyperbolicTangent = P.tanh = function() {
|
|
10059
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
10060
|
+
if (!x.isFinite())
|
|
10061
|
+
return new Ctor(x.s);
|
|
10062
|
+
if (x.isZero())
|
|
10063
|
+
return new Ctor(x);
|
|
10064
|
+
pr = Ctor.precision;
|
|
10065
|
+
rm = Ctor.rounding;
|
|
10066
|
+
Ctor.precision = pr + 7;
|
|
10067
|
+
Ctor.rounding = 1;
|
|
10068
|
+
return divide(x.sinh(), x.cosh(), Ctor.precision = pr, Ctor.rounding = rm);
|
|
10069
|
+
};
|
|
10070
|
+
P.inverseCosine = P.acos = function() {
|
|
10071
|
+
var x = this, Ctor = x.constructor, k = x.abs().cmp(1), pr = Ctor.precision, rm = Ctor.rounding;
|
|
10072
|
+
if (k !== -1) {
|
|
10073
|
+
return k === 0 ? x.isNeg() ? getPi(Ctor, pr, rm) : new Ctor(0) : new Ctor(NaN);
|
|
10074
|
+
}
|
|
10075
|
+
if (x.isZero())
|
|
10076
|
+
return getPi(Ctor, pr + 4, rm).times(0.5);
|
|
10077
|
+
Ctor.precision = pr + 6;
|
|
10078
|
+
Ctor.rounding = 1;
|
|
10079
|
+
x = new Ctor(1).minus(x).div(x.plus(1)).sqrt().atan();
|
|
10080
|
+
Ctor.precision = pr;
|
|
10081
|
+
Ctor.rounding = rm;
|
|
10082
|
+
return x.times(2);
|
|
10083
|
+
};
|
|
10084
|
+
P.inverseHyperbolicCosine = P.acosh = function() {
|
|
10085
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
10086
|
+
if (x.lte(1))
|
|
10087
|
+
return new Ctor(x.eq(1) ? 0 : NaN);
|
|
10088
|
+
if (!x.isFinite())
|
|
10089
|
+
return new Ctor(x);
|
|
10090
|
+
pr = Ctor.precision;
|
|
10091
|
+
rm = Ctor.rounding;
|
|
10092
|
+
Ctor.precision = pr + Math.max(Math.abs(x.e), x.sd()) + 4;
|
|
10093
|
+
Ctor.rounding = 1;
|
|
10094
|
+
external2 = false;
|
|
10095
|
+
x = x.times(x).minus(1).sqrt().plus(x);
|
|
10096
|
+
external2 = true;
|
|
10097
|
+
Ctor.precision = pr;
|
|
10098
|
+
Ctor.rounding = rm;
|
|
10099
|
+
return x.ln();
|
|
10100
|
+
};
|
|
10101
|
+
P.inverseHyperbolicSine = P.asinh = function() {
|
|
10102
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
10103
|
+
if (!x.isFinite() || x.isZero())
|
|
10104
|
+
return new Ctor(x);
|
|
10105
|
+
pr = Ctor.precision;
|
|
10106
|
+
rm = Ctor.rounding;
|
|
10107
|
+
Ctor.precision = pr + 2 * Math.max(Math.abs(x.e), x.sd()) + 6;
|
|
10108
|
+
Ctor.rounding = 1;
|
|
10109
|
+
external2 = false;
|
|
10110
|
+
x = x.times(x).plus(1).sqrt().plus(x);
|
|
10111
|
+
external2 = true;
|
|
10112
|
+
Ctor.precision = pr;
|
|
10113
|
+
Ctor.rounding = rm;
|
|
10114
|
+
return x.ln();
|
|
10115
|
+
};
|
|
10116
|
+
P.inverseHyperbolicTangent = P.atanh = function() {
|
|
10117
|
+
var pr, rm, wpr, xsd, x = this, Ctor = x.constructor;
|
|
10118
|
+
if (!x.isFinite())
|
|
10119
|
+
return new Ctor(NaN);
|
|
10120
|
+
if (x.e >= 0)
|
|
10121
|
+
return new Ctor(x.abs().eq(1) ? x.s / 0 : x.isZero() ? x : NaN);
|
|
10122
|
+
pr = Ctor.precision;
|
|
10123
|
+
rm = Ctor.rounding;
|
|
10124
|
+
xsd = x.sd();
|
|
10125
|
+
if (Math.max(xsd, pr) < 2 * -x.e - 1)
|
|
10126
|
+
return finalise(new Ctor(x), pr, rm, true);
|
|
10127
|
+
Ctor.precision = wpr = xsd - x.e;
|
|
10128
|
+
x = divide(x.plus(1), new Ctor(1).minus(x), wpr + pr, 1);
|
|
10129
|
+
Ctor.precision = pr + 4;
|
|
10130
|
+
Ctor.rounding = 1;
|
|
10131
|
+
x = x.ln();
|
|
10132
|
+
Ctor.precision = pr;
|
|
10133
|
+
Ctor.rounding = rm;
|
|
10134
|
+
return x.times(0.5);
|
|
10135
|
+
};
|
|
10136
|
+
P.inverseSine = P.asin = function() {
|
|
10137
|
+
var halfPi, k, pr, rm, x = this, Ctor = x.constructor;
|
|
10138
|
+
if (x.isZero())
|
|
10139
|
+
return new Ctor(x);
|
|
10140
|
+
k = x.abs().cmp(1);
|
|
10141
|
+
pr = Ctor.precision;
|
|
10142
|
+
rm = Ctor.rounding;
|
|
10143
|
+
if (k !== -1) {
|
|
10144
|
+
if (k === 0) {
|
|
10145
|
+
halfPi = getPi(Ctor, pr + 4, rm).times(0.5);
|
|
10146
|
+
halfPi.s = x.s;
|
|
10147
|
+
return halfPi;
|
|
10148
|
+
}
|
|
10149
|
+
return new Ctor(NaN);
|
|
10150
|
+
}
|
|
10151
|
+
Ctor.precision = pr + 6;
|
|
10152
|
+
Ctor.rounding = 1;
|
|
10153
|
+
x = x.div(new Ctor(1).minus(x.times(x)).sqrt().plus(1)).atan();
|
|
10154
|
+
Ctor.precision = pr;
|
|
10155
|
+
Ctor.rounding = rm;
|
|
10156
|
+
return x.times(2);
|
|
10157
|
+
};
|
|
10158
|
+
P.inverseTangent = P.atan = function() {
|
|
10159
|
+
var i, j, k, n, px, t, r, wpr, x2, x = this, Ctor = x.constructor, pr = Ctor.precision, rm = Ctor.rounding;
|
|
10160
|
+
if (!x.isFinite()) {
|
|
10161
|
+
if (!x.s)
|
|
10162
|
+
return new Ctor(NaN);
|
|
10163
|
+
if (pr + 4 <= PI_PRECISION) {
|
|
10164
|
+
r = getPi(Ctor, pr + 4, rm).times(0.5);
|
|
10165
|
+
r.s = x.s;
|
|
10166
|
+
return r;
|
|
10167
|
+
}
|
|
10168
|
+
} else if (x.isZero()) {
|
|
10169
|
+
return new Ctor(x);
|
|
10170
|
+
} else if (x.abs().eq(1) && pr + 4 <= PI_PRECISION) {
|
|
10171
|
+
r = getPi(Ctor, pr + 4, rm).times(0.25);
|
|
10172
|
+
r.s = x.s;
|
|
10173
|
+
return r;
|
|
10174
|
+
}
|
|
10175
|
+
Ctor.precision = wpr = pr + 10;
|
|
10176
|
+
Ctor.rounding = 1;
|
|
10177
|
+
k = Math.min(28, wpr / LOG_BASE + 2 | 0);
|
|
10178
|
+
for (i = k;i; --i)
|
|
10179
|
+
x = x.div(x.times(x).plus(1).sqrt().plus(1));
|
|
10180
|
+
external2 = false;
|
|
10181
|
+
j = Math.ceil(wpr / LOG_BASE);
|
|
10182
|
+
n = 1;
|
|
10183
|
+
x2 = x.times(x);
|
|
10184
|
+
r = new Ctor(x);
|
|
10185
|
+
px = x;
|
|
10186
|
+
for (;i !== -1; ) {
|
|
10187
|
+
px = px.times(x2);
|
|
10188
|
+
t = r.minus(px.div(n += 2));
|
|
10189
|
+
px = px.times(x2);
|
|
10190
|
+
r = t.plus(px.div(n += 2));
|
|
10191
|
+
if (r.d[j] !== undefined)
|
|
10192
|
+
for (i = j;r.d[i] === t.d[i] && i--; )
|
|
10193
|
+
;
|
|
10194
|
+
}
|
|
10195
|
+
if (k)
|
|
10196
|
+
r = r.times(2 << k - 1);
|
|
10197
|
+
external2 = true;
|
|
10198
|
+
return finalise(r, Ctor.precision = pr, Ctor.rounding = rm, true);
|
|
10199
|
+
};
|
|
10200
|
+
P.isFinite = function() {
|
|
10201
|
+
return !!this.d;
|
|
10202
|
+
};
|
|
10203
|
+
P.isInteger = P.isInt = function() {
|
|
10204
|
+
return !!this.d && mathfloor(this.e / LOG_BASE) > this.d.length - 2;
|
|
10205
|
+
};
|
|
10206
|
+
P.isNaN = function() {
|
|
10207
|
+
return !this.s;
|
|
10208
|
+
};
|
|
10209
|
+
P.isNegative = P.isNeg = function() {
|
|
10210
|
+
return this.s < 0;
|
|
10211
|
+
};
|
|
10212
|
+
P.isPositive = P.isPos = function() {
|
|
10213
|
+
return this.s > 0;
|
|
10214
|
+
};
|
|
10215
|
+
P.isZero = function() {
|
|
10216
|
+
return !!this.d && this.d[0] === 0;
|
|
10217
|
+
};
|
|
10218
|
+
P.lessThan = P.lt = function(y) {
|
|
10219
|
+
return this.cmp(y) < 0;
|
|
10220
|
+
};
|
|
10221
|
+
P.lessThanOrEqualTo = P.lte = function(y) {
|
|
10222
|
+
return this.cmp(y) < 1;
|
|
10223
|
+
};
|
|
10224
|
+
P.logarithm = P.log = function(base) {
|
|
10225
|
+
var isBase10, d, denominator, k, inf, num, sd, r, arg = this, Ctor = arg.constructor, pr = Ctor.precision, rm = Ctor.rounding, guard = 5;
|
|
10226
|
+
if (base == null) {
|
|
10227
|
+
base = new Ctor(10);
|
|
10228
|
+
isBase10 = true;
|
|
10229
|
+
} else {
|
|
10230
|
+
base = new Ctor(base);
|
|
10231
|
+
d = base.d;
|
|
10232
|
+
if (base.s < 0 || !d || !d[0] || base.eq(1))
|
|
10233
|
+
return new Ctor(NaN);
|
|
10234
|
+
isBase10 = base.eq(10);
|
|
10235
|
+
}
|
|
10236
|
+
d = arg.d;
|
|
10237
|
+
if (arg.s < 0 || !d || !d[0] || arg.eq(1)) {
|
|
10238
|
+
return new Ctor(d && !d[0] ? -1 / 0 : arg.s != 1 ? NaN : d ? 0 : 1 / 0);
|
|
10239
|
+
}
|
|
10240
|
+
if (isBase10) {
|
|
10241
|
+
if (d.length > 1) {
|
|
10242
|
+
inf = true;
|
|
10243
|
+
} else {
|
|
10244
|
+
for (k = d[0];k % 10 === 0; )
|
|
10245
|
+
k /= 10;
|
|
10246
|
+
inf = k !== 1;
|
|
10247
|
+
}
|
|
10248
|
+
}
|
|
10249
|
+
external2 = false;
|
|
10250
|
+
sd = pr + guard;
|
|
10251
|
+
num = naturalLogarithm(arg, sd);
|
|
10252
|
+
denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
|
|
10253
|
+
r = divide(num, denominator, sd, 1);
|
|
10254
|
+
if (checkRoundingDigits(r.d, k = pr, rm)) {
|
|
10255
|
+
do {
|
|
10256
|
+
sd += 10;
|
|
10257
|
+
num = naturalLogarithm(arg, sd);
|
|
10258
|
+
denominator = isBase10 ? getLn10(Ctor, sd + 10) : naturalLogarithm(base, sd);
|
|
10259
|
+
r = divide(num, denominator, sd, 1);
|
|
10260
|
+
if (!inf) {
|
|
10261
|
+
if (+digitsToString(r.d).slice(k + 1, k + 15) + 1 == 100000000000000) {
|
|
10262
|
+
r = finalise(r, pr + 1, 0);
|
|
10263
|
+
}
|
|
10264
|
+
break;
|
|
10265
|
+
}
|
|
10266
|
+
} while (checkRoundingDigits(r.d, k += 10, rm));
|
|
10267
|
+
}
|
|
10268
|
+
external2 = true;
|
|
10269
|
+
return finalise(r, pr, rm);
|
|
10270
|
+
};
|
|
10271
|
+
P.minus = P.sub = function(y) {
|
|
10272
|
+
var d, e, i, j, k, len, pr, rm, xd, xe, xLTy, yd, x = this, Ctor = x.constructor;
|
|
10273
|
+
y = new Ctor(y);
|
|
10274
|
+
if (!x.d || !y.d) {
|
|
10275
|
+
if (!x.s || !y.s)
|
|
10276
|
+
y = new Ctor(NaN);
|
|
10277
|
+
else if (x.d)
|
|
10278
|
+
y.s = -y.s;
|
|
10279
|
+
else
|
|
10280
|
+
y = new Ctor(y.d || x.s !== y.s ? x : NaN);
|
|
10281
|
+
return y;
|
|
10282
|
+
}
|
|
10283
|
+
if (x.s != y.s) {
|
|
10284
|
+
y.s = -y.s;
|
|
10285
|
+
return x.plus(y);
|
|
10286
|
+
}
|
|
10287
|
+
xd = x.d;
|
|
10288
|
+
yd = y.d;
|
|
10289
|
+
pr = Ctor.precision;
|
|
10290
|
+
rm = Ctor.rounding;
|
|
10291
|
+
if (!xd[0] || !yd[0]) {
|
|
10292
|
+
if (yd[0])
|
|
10293
|
+
y.s = -y.s;
|
|
10294
|
+
else if (xd[0])
|
|
10295
|
+
y = new Ctor(x);
|
|
10296
|
+
else
|
|
10297
|
+
return new Ctor(rm === 3 ? -0 : 0);
|
|
10298
|
+
return external2 ? finalise(y, pr, rm) : y;
|
|
10299
|
+
}
|
|
10300
|
+
e = mathfloor(y.e / LOG_BASE);
|
|
10301
|
+
xe = mathfloor(x.e / LOG_BASE);
|
|
10302
|
+
xd = xd.slice();
|
|
10303
|
+
k = xe - e;
|
|
10304
|
+
if (k) {
|
|
10305
|
+
xLTy = k < 0;
|
|
10306
|
+
if (xLTy) {
|
|
10307
|
+
d = xd;
|
|
10308
|
+
k = -k;
|
|
10309
|
+
len = yd.length;
|
|
10310
|
+
} else {
|
|
10311
|
+
d = yd;
|
|
10312
|
+
e = xe;
|
|
10313
|
+
len = xd.length;
|
|
10314
|
+
}
|
|
10315
|
+
i = Math.max(Math.ceil(pr / LOG_BASE), len) + 2;
|
|
10316
|
+
if (k > i) {
|
|
10317
|
+
k = i;
|
|
10318
|
+
d.length = 1;
|
|
10319
|
+
}
|
|
10320
|
+
d.reverse();
|
|
10321
|
+
for (i = k;i--; )
|
|
10322
|
+
d.push(0);
|
|
10323
|
+
d.reverse();
|
|
10324
|
+
} else {
|
|
10325
|
+
i = xd.length;
|
|
10326
|
+
len = yd.length;
|
|
10327
|
+
xLTy = i < len;
|
|
10328
|
+
if (xLTy)
|
|
10329
|
+
len = i;
|
|
10330
|
+
for (i = 0;i < len; i++) {
|
|
10331
|
+
if (xd[i] != yd[i]) {
|
|
10332
|
+
xLTy = xd[i] < yd[i];
|
|
10333
|
+
break;
|
|
10334
|
+
}
|
|
10335
|
+
}
|
|
10336
|
+
k = 0;
|
|
10337
|
+
}
|
|
10338
|
+
if (xLTy) {
|
|
10339
|
+
d = xd;
|
|
10340
|
+
xd = yd;
|
|
10341
|
+
yd = d;
|
|
10342
|
+
y.s = -y.s;
|
|
10343
|
+
}
|
|
10344
|
+
len = xd.length;
|
|
10345
|
+
for (i = yd.length - len;i > 0; --i)
|
|
10346
|
+
xd[len++] = 0;
|
|
10347
|
+
for (i = yd.length;i > k; ) {
|
|
10348
|
+
if (xd[--i] < yd[i]) {
|
|
10349
|
+
for (j = i;j && xd[--j] === 0; )
|
|
10350
|
+
xd[j] = BASE - 1;
|
|
10351
|
+
--xd[j];
|
|
10352
|
+
xd[i] += BASE;
|
|
10353
|
+
}
|
|
10354
|
+
xd[i] -= yd[i];
|
|
10355
|
+
}
|
|
10356
|
+
for (;xd[--len] === 0; )
|
|
10357
|
+
xd.pop();
|
|
10358
|
+
for (;xd[0] === 0; xd.shift())
|
|
10359
|
+
--e;
|
|
10360
|
+
if (!xd[0])
|
|
10361
|
+
return new Ctor(rm === 3 ? -0 : 0);
|
|
10362
|
+
y.d = xd;
|
|
10363
|
+
y.e = getBase10Exponent(xd, e);
|
|
10364
|
+
return external2 ? finalise(y, pr, rm) : y;
|
|
10365
|
+
};
|
|
10366
|
+
P.modulo = P.mod = function(y) {
|
|
10367
|
+
var q, x = this, Ctor = x.constructor;
|
|
10368
|
+
y = new Ctor(y);
|
|
10369
|
+
if (!x.d || !y.s || y.d && !y.d[0])
|
|
10370
|
+
return new Ctor(NaN);
|
|
10371
|
+
if (!y.d || x.d && !x.d[0]) {
|
|
10372
|
+
return finalise(new Ctor(x), Ctor.precision, Ctor.rounding);
|
|
10373
|
+
}
|
|
10374
|
+
external2 = false;
|
|
10375
|
+
if (Ctor.modulo == 9) {
|
|
10376
|
+
q = divide(x, y.abs(), 0, 3, 1);
|
|
10377
|
+
q.s *= y.s;
|
|
10378
|
+
} else {
|
|
10379
|
+
q = divide(x, y, 0, Ctor.modulo, 1);
|
|
10380
|
+
}
|
|
10381
|
+
q = q.times(y);
|
|
10382
|
+
external2 = true;
|
|
10383
|
+
return x.minus(q);
|
|
10384
|
+
};
|
|
10385
|
+
P.naturalExponential = P.exp = function() {
|
|
10386
|
+
return naturalExponential(this);
|
|
10387
|
+
};
|
|
10388
|
+
P.naturalLogarithm = P.ln = function() {
|
|
10389
|
+
return naturalLogarithm(this);
|
|
10390
|
+
};
|
|
10391
|
+
P.negated = P.neg = function() {
|
|
10392
|
+
var x = new this.constructor(this);
|
|
10393
|
+
x.s = -x.s;
|
|
10394
|
+
return finalise(x);
|
|
10395
|
+
};
|
|
10396
|
+
P.plus = P.add = function(y) {
|
|
10397
|
+
var carry, d, e, i, k, len, pr, rm, xd, yd, x = this, Ctor = x.constructor;
|
|
10398
|
+
y = new Ctor(y);
|
|
10399
|
+
if (!x.d || !y.d) {
|
|
10400
|
+
if (!x.s || !y.s)
|
|
10401
|
+
y = new Ctor(NaN);
|
|
10402
|
+
else if (!x.d)
|
|
10403
|
+
y = new Ctor(y.d || x.s === y.s ? x : NaN);
|
|
10404
|
+
return y;
|
|
10405
|
+
}
|
|
10406
|
+
if (x.s != y.s) {
|
|
10407
|
+
y.s = -y.s;
|
|
10408
|
+
return x.minus(y);
|
|
10409
|
+
}
|
|
10410
|
+
xd = x.d;
|
|
10411
|
+
yd = y.d;
|
|
10412
|
+
pr = Ctor.precision;
|
|
10413
|
+
rm = Ctor.rounding;
|
|
10414
|
+
if (!xd[0] || !yd[0]) {
|
|
10415
|
+
if (!yd[0])
|
|
10416
|
+
y = new Ctor(x);
|
|
10417
|
+
return external2 ? finalise(y, pr, rm) : y;
|
|
10418
|
+
}
|
|
10419
|
+
k = mathfloor(x.e / LOG_BASE);
|
|
10420
|
+
e = mathfloor(y.e / LOG_BASE);
|
|
10421
|
+
xd = xd.slice();
|
|
10422
|
+
i = k - e;
|
|
10423
|
+
if (i) {
|
|
10424
|
+
if (i < 0) {
|
|
10425
|
+
d = xd;
|
|
10426
|
+
i = -i;
|
|
10427
|
+
len = yd.length;
|
|
10428
|
+
} else {
|
|
10429
|
+
d = yd;
|
|
10430
|
+
e = k;
|
|
10431
|
+
len = xd.length;
|
|
10432
|
+
}
|
|
10433
|
+
k = Math.ceil(pr / LOG_BASE);
|
|
10434
|
+
len = k > len ? k + 1 : len + 1;
|
|
10435
|
+
if (i > len) {
|
|
10436
|
+
i = len;
|
|
10437
|
+
d.length = 1;
|
|
10438
|
+
}
|
|
10439
|
+
d.reverse();
|
|
10440
|
+
for (;i--; )
|
|
10441
|
+
d.push(0);
|
|
10442
|
+
d.reverse();
|
|
10443
|
+
}
|
|
10444
|
+
len = xd.length;
|
|
10445
|
+
i = yd.length;
|
|
10446
|
+
if (len - i < 0) {
|
|
10447
|
+
i = len;
|
|
10448
|
+
d = yd;
|
|
10449
|
+
yd = xd;
|
|
10450
|
+
xd = d;
|
|
10451
|
+
}
|
|
10452
|
+
for (carry = 0;i; ) {
|
|
10453
|
+
carry = (xd[--i] = xd[i] + yd[i] + carry) / BASE | 0;
|
|
10454
|
+
xd[i] %= BASE;
|
|
10455
|
+
}
|
|
10456
|
+
if (carry) {
|
|
10457
|
+
xd.unshift(carry);
|
|
10458
|
+
++e;
|
|
10459
|
+
}
|
|
10460
|
+
for (len = xd.length;xd[--len] == 0; )
|
|
10461
|
+
xd.pop();
|
|
10462
|
+
y.d = xd;
|
|
10463
|
+
y.e = getBase10Exponent(xd, e);
|
|
10464
|
+
return external2 ? finalise(y, pr, rm) : y;
|
|
10465
|
+
};
|
|
10466
|
+
P.precision = P.sd = function(z) {
|
|
10467
|
+
var k, x = this;
|
|
10468
|
+
if (z !== undefined && z !== !!z && z !== 1 && z !== 0)
|
|
10469
|
+
throw Error(invalidArgument + z);
|
|
10470
|
+
if (x.d) {
|
|
10471
|
+
k = getPrecision(x.d);
|
|
10472
|
+
if (z && x.e + 1 > k)
|
|
10473
|
+
k = x.e + 1;
|
|
10474
|
+
} else {
|
|
10475
|
+
k = NaN;
|
|
10476
|
+
}
|
|
10477
|
+
return k;
|
|
10478
|
+
};
|
|
10479
|
+
P.round = function() {
|
|
10480
|
+
var x = this, Ctor = x.constructor;
|
|
10481
|
+
return finalise(new Ctor(x), x.e + 1, Ctor.rounding);
|
|
10482
|
+
};
|
|
10483
|
+
P.sine = P.sin = function() {
|
|
10484
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
10485
|
+
if (!x.isFinite())
|
|
10486
|
+
return new Ctor(NaN);
|
|
10487
|
+
if (x.isZero())
|
|
10488
|
+
return new Ctor(x);
|
|
10489
|
+
pr = Ctor.precision;
|
|
10490
|
+
rm = Ctor.rounding;
|
|
10491
|
+
Ctor.precision = pr + Math.max(x.e, x.sd()) + LOG_BASE;
|
|
10492
|
+
Ctor.rounding = 1;
|
|
10493
|
+
x = sine(Ctor, toLessThanHalfPi(Ctor, x));
|
|
10494
|
+
Ctor.precision = pr;
|
|
10495
|
+
Ctor.rounding = rm;
|
|
10496
|
+
return finalise(quadrant > 2 ? x.neg() : x, pr, rm, true);
|
|
10497
|
+
};
|
|
10498
|
+
P.squareRoot = P.sqrt = function() {
|
|
10499
|
+
var m, n, sd, r, rep, t, x = this, d = x.d, e = x.e, s = x.s, Ctor = x.constructor;
|
|
10500
|
+
if (s !== 1 || !d || !d[0]) {
|
|
10501
|
+
return new Ctor(!s || s < 0 && (!d || d[0]) ? NaN : d ? x : 1 / 0);
|
|
10502
|
+
}
|
|
10503
|
+
external2 = false;
|
|
10504
|
+
s = Math.sqrt(+x);
|
|
10505
|
+
if (s == 0 || s == 1 / 0) {
|
|
10506
|
+
n = digitsToString(d);
|
|
10507
|
+
if ((n.length + e) % 2 == 0)
|
|
10508
|
+
n += "0";
|
|
10509
|
+
s = Math.sqrt(n);
|
|
10510
|
+
e = mathfloor((e + 1) / 2) - (e < 0 || e % 2);
|
|
10511
|
+
if (s == 1 / 0) {
|
|
10512
|
+
n = "5e" + e;
|
|
10513
|
+
} else {
|
|
10514
|
+
n = s.toExponential();
|
|
10515
|
+
n = n.slice(0, n.indexOf("e") + 1) + e;
|
|
10516
|
+
}
|
|
10517
|
+
r = new Ctor(n);
|
|
10518
|
+
} else {
|
|
10519
|
+
r = new Ctor(s.toString());
|
|
10520
|
+
}
|
|
10521
|
+
sd = (e = Ctor.precision) + 3;
|
|
10522
|
+
for (;; ) {
|
|
10523
|
+
t = r;
|
|
10524
|
+
r = t.plus(divide(x, t, sd + 2, 1)).times(0.5);
|
|
10525
|
+
if (digitsToString(t.d).slice(0, sd) === (n = digitsToString(r.d)).slice(0, sd)) {
|
|
10526
|
+
n = n.slice(sd - 3, sd + 1);
|
|
10527
|
+
if (n == "9999" || !rep && n == "4999") {
|
|
10528
|
+
if (!rep) {
|
|
10529
|
+
finalise(t, e + 1, 0);
|
|
10530
|
+
if (t.times(t).eq(x)) {
|
|
10531
|
+
r = t;
|
|
10532
|
+
break;
|
|
10533
|
+
}
|
|
10534
|
+
}
|
|
10535
|
+
sd += 4;
|
|
10536
|
+
rep = 1;
|
|
10537
|
+
} else {
|
|
10538
|
+
if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
|
|
10539
|
+
finalise(r, e + 1, 1);
|
|
10540
|
+
m = !r.times(r).eq(x);
|
|
10541
|
+
}
|
|
10542
|
+
break;
|
|
10543
|
+
}
|
|
10544
|
+
}
|
|
10545
|
+
}
|
|
10546
|
+
external2 = true;
|
|
10547
|
+
return finalise(r, e, Ctor.rounding, m);
|
|
10548
|
+
};
|
|
10549
|
+
P.tangent = P.tan = function() {
|
|
10550
|
+
var pr, rm, x = this, Ctor = x.constructor;
|
|
10551
|
+
if (!x.isFinite())
|
|
10552
|
+
return new Ctor(NaN);
|
|
10553
|
+
if (x.isZero())
|
|
10554
|
+
return new Ctor(x);
|
|
10555
|
+
pr = Ctor.precision;
|
|
10556
|
+
rm = Ctor.rounding;
|
|
10557
|
+
Ctor.precision = pr + 10;
|
|
10558
|
+
Ctor.rounding = 1;
|
|
10559
|
+
x = x.sin();
|
|
10560
|
+
x.s = 1;
|
|
10561
|
+
x = divide(x, new Ctor(1).minus(x.times(x)).sqrt(), pr + 10, 0);
|
|
10562
|
+
Ctor.precision = pr;
|
|
10563
|
+
Ctor.rounding = rm;
|
|
10564
|
+
return finalise(quadrant == 2 || quadrant == 4 ? x.neg() : x, pr, rm, true);
|
|
10565
|
+
};
|
|
10566
|
+
P.times = P.mul = function(y) {
|
|
10567
|
+
var carry, e, i, k, r, rL, t, xdL, ydL, x = this, Ctor = x.constructor, xd = x.d, yd = (y = new Ctor(y)).d;
|
|
10568
|
+
y.s *= x.s;
|
|
10569
|
+
if (!xd || !xd[0] || !yd || !yd[0]) {
|
|
10570
|
+
return new Ctor(!y.s || xd && !xd[0] && !yd || yd && !yd[0] && !xd ? NaN : !xd || !yd ? y.s / 0 : y.s * 0);
|
|
10571
|
+
}
|
|
10572
|
+
e = mathfloor(x.e / LOG_BASE) + mathfloor(y.e / LOG_BASE);
|
|
10573
|
+
xdL = xd.length;
|
|
10574
|
+
ydL = yd.length;
|
|
10575
|
+
if (xdL < ydL) {
|
|
10576
|
+
r = xd;
|
|
10577
|
+
xd = yd;
|
|
10578
|
+
yd = r;
|
|
10579
|
+
rL = xdL;
|
|
10580
|
+
xdL = ydL;
|
|
10581
|
+
ydL = rL;
|
|
10582
|
+
}
|
|
10583
|
+
r = [];
|
|
10584
|
+
rL = xdL + ydL;
|
|
10585
|
+
for (i = rL;i--; )
|
|
10586
|
+
r.push(0);
|
|
10587
|
+
for (i = ydL;--i >= 0; ) {
|
|
10588
|
+
carry = 0;
|
|
10589
|
+
for (k = xdL + i;k > i; ) {
|
|
10590
|
+
t = r[k] + yd[i] * xd[k - i - 1] + carry;
|
|
10591
|
+
r[k--] = t % BASE | 0;
|
|
10592
|
+
carry = t / BASE | 0;
|
|
10593
|
+
}
|
|
10594
|
+
r[k] = (r[k] + carry) % BASE | 0;
|
|
10595
|
+
}
|
|
10596
|
+
for (;!r[--rL]; )
|
|
10597
|
+
r.pop();
|
|
10598
|
+
if (carry)
|
|
10599
|
+
++e;
|
|
10600
|
+
else
|
|
10601
|
+
r.shift();
|
|
10602
|
+
y.d = r;
|
|
10603
|
+
y.e = getBase10Exponent(r, e);
|
|
10604
|
+
return external2 ? finalise(y, Ctor.precision, Ctor.rounding) : y;
|
|
10605
|
+
};
|
|
10606
|
+
P.toBinary = function(sd, rm) {
|
|
10607
|
+
return toStringBinary(this, 2, sd, rm);
|
|
10608
|
+
};
|
|
10609
|
+
P.toDecimalPlaces = P.toDP = function(dp, rm) {
|
|
10610
|
+
var x = this, Ctor = x.constructor;
|
|
10611
|
+
x = new Ctor(x);
|
|
10612
|
+
if (dp === undefined)
|
|
10613
|
+
return x;
|
|
10614
|
+
checkInt32(dp, 0, MAX_DIGITS);
|
|
10615
|
+
if (rm === undefined)
|
|
10616
|
+
rm = Ctor.rounding;
|
|
10617
|
+
else
|
|
10618
|
+
checkInt32(rm, 0, 8);
|
|
10619
|
+
return finalise(x, dp + x.e + 1, rm);
|
|
10620
|
+
};
|
|
10621
|
+
P.toExponential = function(dp, rm) {
|
|
10622
|
+
var str, x = this, Ctor = x.constructor;
|
|
10623
|
+
if (dp === undefined) {
|
|
10624
|
+
str = finiteToString(x, true);
|
|
10625
|
+
} else {
|
|
10626
|
+
checkInt32(dp, 0, MAX_DIGITS);
|
|
10627
|
+
if (rm === undefined)
|
|
10628
|
+
rm = Ctor.rounding;
|
|
10629
|
+
else
|
|
10630
|
+
checkInt32(rm, 0, 8);
|
|
10631
|
+
x = finalise(new Ctor(x), dp + 1, rm);
|
|
10632
|
+
str = finiteToString(x, true, dp + 1);
|
|
10633
|
+
}
|
|
10634
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
10635
|
+
};
|
|
10636
|
+
P.toFixed = function(dp, rm) {
|
|
10637
|
+
var str, y, x = this, Ctor = x.constructor;
|
|
10638
|
+
if (dp === undefined) {
|
|
10639
|
+
str = finiteToString(x);
|
|
10640
|
+
} else {
|
|
10641
|
+
checkInt32(dp, 0, MAX_DIGITS);
|
|
10642
|
+
if (rm === undefined)
|
|
10643
|
+
rm = Ctor.rounding;
|
|
10644
|
+
else
|
|
10645
|
+
checkInt32(rm, 0, 8);
|
|
10646
|
+
y = finalise(new Ctor(x), dp + x.e + 1, rm);
|
|
10647
|
+
str = finiteToString(y, false, dp + y.e + 1);
|
|
10648
|
+
}
|
|
10649
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
10650
|
+
};
|
|
10651
|
+
P.toFraction = function(maxD) {
|
|
10652
|
+
var d, d0, d1, d2, e, k, n, n0, n1, pr, q, r, x = this, xd = x.d, Ctor = x.constructor;
|
|
10653
|
+
if (!xd)
|
|
10654
|
+
return new Ctor(x);
|
|
10655
|
+
n1 = d0 = new Ctor(1);
|
|
10656
|
+
d1 = n0 = new Ctor(0);
|
|
10657
|
+
d = new Ctor(d1);
|
|
10658
|
+
e = d.e = getPrecision(xd) - x.e - 1;
|
|
10659
|
+
k = e % LOG_BASE;
|
|
10660
|
+
d.d[0] = mathpow(10, k < 0 ? LOG_BASE + k : k);
|
|
10661
|
+
if (maxD == null) {
|
|
10662
|
+
maxD = e > 0 ? d : n1;
|
|
10663
|
+
} else {
|
|
10664
|
+
n = new Ctor(maxD);
|
|
10665
|
+
if (!n.isInt() || n.lt(n1))
|
|
10666
|
+
throw Error(invalidArgument + n);
|
|
10667
|
+
maxD = n.gt(d) ? e > 0 ? d : n1 : n;
|
|
10668
|
+
}
|
|
10669
|
+
external2 = false;
|
|
10670
|
+
n = new Ctor(digitsToString(xd));
|
|
10671
|
+
pr = Ctor.precision;
|
|
10672
|
+
Ctor.precision = e = xd.length * LOG_BASE * 2;
|
|
10673
|
+
for (;; ) {
|
|
10674
|
+
q = divide(n, d, 0, 1, 1);
|
|
10675
|
+
d2 = d0.plus(q.times(d1));
|
|
10676
|
+
if (d2.cmp(maxD) == 1)
|
|
10677
|
+
break;
|
|
10678
|
+
d0 = d1;
|
|
10679
|
+
d1 = d2;
|
|
10680
|
+
d2 = n1;
|
|
10681
|
+
n1 = n0.plus(q.times(d2));
|
|
10682
|
+
n0 = d2;
|
|
10683
|
+
d2 = d;
|
|
10684
|
+
d = n.minus(q.times(d2));
|
|
10685
|
+
n = d2;
|
|
10686
|
+
}
|
|
10687
|
+
d2 = divide(maxD.minus(d0), d1, 0, 1, 1);
|
|
10688
|
+
n0 = n0.plus(d2.times(n1));
|
|
10689
|
+
d0 = d0.plus(d2.times(d1));
|
|
10690
|
+
n0.s = n1.s = x.s;
|
|
10691
|
+
r = divide(n1, d1, e, 1).minus(x).abs().cmp(divide(n0, d0, e, 1).minus(x).abs()) < 1 ? [n1, d1] : [n0, d0];
|
|
10692
|
+
Ctor.precision = pr;
|
|
10693
|
+
external2 = true;
|
|
10694
|
+
return r;
|
|
10695
|
+
};
|
|
10696
|
+
P.toHexadecimal = P.toHex = function(sd, rm) {
|
|
10697
|
+
return toStringBinary(this, 16, sd, rm);
|
|
10698
|
+
};
|
|
10699
|
+
P.toNearest = function(y, rm) {
|
|
10700
|
+
var x = this, Ctor = x.constructor;
|
|
10701
|
+
x = new Ctor(x);
|
|
10702
|
+
if (y == null) {
|
|
10703
|
+
if (!x.d)
|
|
10704
|
+
return x;
|
|
10705
|
+
y = new Ctor(1);
|
|
10706
|
+
rm = Ctor.rounding;
|
|
10707
|
+
} else {
|
|
10708
|
+
y = new Ctor(y);
|
|
10709
|
+
if (rm === undefined) {
|
|
10710
|
+
rm = Ctor.rounding;
|
|
10711
|
+
} else {
|
|
10712
|
+
checkInt32(rm, 0, 8);
|
|
10713
|
+
}
|
|
10714
|
+
if (!x.d)
|
|
10715
|
+
return y.s ? x : y;
|
|
10716
|
+
if (!y.d) {
|
|
10717
|
+
if (y.s)
|
|
10718
|
+
y.s = x.s;
|
|
10719
|
+
return y;
|
|
10720
|
+
}
|
|
10721
|
+
}
|
|
10722
|
+
if (y.d[0]) {
|
|
10723
|
+
external2 = false;
|
|
10724
|
+
x = divide(x, y, 0, rm, 1).times(y);
|
|
10725
|
+
external2 = true;
|
|
10726
|
+
finalise(x);
|
|
10727
|
+
} else {
|
|
10728
|
+
y.s = x.s;
|
|
10729
|
+
x = y;
|
|
10730
|
+
}
|
|
10731
|
+
return x;
|
|
10732
|
+
};
|
|
10733
|
+
P.toNumber = function() {
|
|
10734
|
+
return +this;
|
|
10735
|
+
};
|
|
10736
|
+
P.toOctal = function(sd, rm) {
|
|
10737
|
+
return toStringBinary(this, 8, sd, rm);
|
|
10738
|
+
};
|
|
10739
|
+
P.toPower = P.pow = function(y) {
|
|
10740
|
+
var e, k, pr, r, rm, s, x = this, Ctor = x.constructor, yn = +(y = new Ctor(y));
|
|
10741
|
+
if (!x.d || !y.d || !x.d[0] || !y.d[0])
|
|
10742
|
+
return new Ctor(mathpow(+x, yn));
|
|
10743
|
+
x = new Ctor(x);
|
|
10744
|
+
if (x.eq(1))
|
|
10745
|
+
return x;
|
|
10746
|
+
pr = Ctor.precision;
|
|
10747
|
+
rm = Ctor.rounding;
|
|
10748
|
+
if (y.eq(1))
|
|
10749
|
+
return finalise(x, pr, rm);
|
|
10750
|
+
e = mathfloor(y.e / LOG_BASE);
|
|
10751
|
+
if (e >= y.d.length - 1 && (k = yn < 0 ? -yn : yn) <= MAX_SAFE_INTEGER) {
|
|
10752
|
+
r = intPow(Ctor, x, k, pr);
|
|
10753
|
+
return y.s < 0 ? new Ctor(1).div(r) : finalise(r, pr, rm);
|
|
10754
|
+
}
|
|
10755
|
+
s = x.s;
|
|
10756
|
+
if (s < 0) {
|
|
10757
|
+
if (e < y.d.length - 1)
|
|
10758
|
+
return new Ctor(NaN);
|
|
10759
|
+
if ((y.d[e] & 1) == 0)
|
|
10760
|
+
s = 1;
|
|
10761
|
+
if (x.e == 0 && x.d[0] == 1 && x.d.length == 1) {
|
|
10762
|
+
x.s = s;
|
|
10763
|
+
return x;
|
|
10764
|
+
}
|
|
10765
|
+
}
|
|
10766
|
+
k = mathpow(+x, yn);
|
|
10767
|
+
e = k == 0 || !isFinite(k) ? mathfloor(yn * (Math.log("0." + digitsToString(x.d)) / Math.LN10 + x.e + 1)) : new Ctor(k + "").e;
|
|
10768
|
+
if (e > Ctor.maxE + 1 || e < Ctor.minE - 1)
|
|
10769
|
+
return new Ctor(e > 0 ? s / 0 : 0);
|
|
10770
|
+
external2 = false;
|
|
10771
|
+
Ctor.rounding = x.s = 1;
|
|
10772
|
+
k = Math.min(12, (e + "").length);
|
|
10773
|
+
r = naturalExponential(y.times(naturalLogarithm(x, pr + k)), pr);
|
|
10774
|
+
if (r.d) {
|
|
10775
|
+
r = finalise(r, pr + 5, 1);
|
|
10776
|
+
if (checkRoundingDigits(r.d, pr, rm)) {
|
|
10777
|
+
e = pr + 10;
|
|
10778
|
+
r = finalise(naturalExponential(y.times(naturalLogarithm(x, e + k)), e), e + 5, 1);
|
|
10779
|
+
if (+digitsToString(r.d).slice(pr + 1, pr + 15) + 1 == 100000000000000) {
|
|
10780
|
+
r = finalise(r, pr + 1, 0);
|
|
10781
|
+
}
|
|
10782
|
+
}
|
|
10783
|
+
}
|
|
10784
|
+
r.s = s;
|
|
10785
|
+
external2 = true;
|
|
10786
|
+
Ctor.rounding = rm;
|
|
10787
|
+
return finalise(r, pr, rm);
|
|
10788
|
+
};
|
|
10789
|
+
P.toPrecision = function(sd, rm) {
|
|
10790
|
+
var str, x = this, Ctor = x.constructor;
|
|
10791
|
+
if (sd === undefined) {
|
|
10792
|
+
str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
|
|
10793
|
+
} else {
|
|
10794
|
+
checkInt32(sd, 1, MAX_DIGITS);
|
|
10795
|
+
if (rm === undefined)
|
|
10796
|
+
rm = Ctor.rounding;
|
|
10797
|
+
else
|
|
10798
|
+
checkInt32(rm, 0, 8);
|
|
10799
|
+
x = finalise(new Ctor(x), sd, rm);
|
|
10800
|
+
str = finiteToString(x, sd <= x.e || x.e <= Ctor.toExpNeg, sd);
|
|
10801
|
+
}
|
|
10802
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
10803
|
+
};
|
|
10804
|
+
P.toSignificantDigits = P.toSD = function(sd, rm) {
|
|
10805
|
+
var x = this, Ctor = x.constructor;
|
|
10806
|
+
if (sd === undefined) {
|
|
10807
|
+
sd = Ctor.precision;
|
|
10808
|
+
rm = Ctor.rounding;
|
|
10809
|
+
} else {
|
|
10810
|
+
checkInt32(sd, 1, MAX_DIGITS);
|
|
10811
|
+
if (rm === undefined)
|
|
10812
|
+
rm = Ctor.rounding;
|
|
10813
|
+
else
|
|
10814
|
+
checkInt32(rm, 0, 8);
|
|
10815
|
+
}
|
|
10816
|
+
return finalise(new Ctor(x), sd, rm);
|
|
10817
|
+
};
|
|
10818
|
+
P.toString = function() {
|
|
10819
|
+
var x = this, Ctor = x.constructor, str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
|
|
10820
|
+
return x.isNeg() && !x.isZero() ? "-" + str : str;
|
|
10821
|
+
};
|
|
10822
|
+
P.truncated = P.trunc = function() {
|
|
10823
|
+
return finalise(new this.constructor(this), this.e + 1, 1);
|
|
10824
|
+
};
|
|
10825
|
+
P.valueOf = P.toJSON = function() {
|
|
10826
|
+
var x = this, Ctor = x.constructor, str = finiteToString(x, x.e <= Ctor.toExpNeg || x.e >= Ctor.toExpPos);
|
|
10827
|
+
return x.isNeg() ? "-" + str : str;
|
|
10828
|
+
};
|
|
10829
|
+
function digitsToString(d) {
|
|
10830
|
+
var i, k, ws, indexOfLastWord = d.length - 1, str = "", w = d[0];
|
|
10831
|
+
if (indexOfLastWord > 0) {
|
|
10832
|
+
str += w;
|
|
10833
|
+
for (i = 1;i < indexOfLastWord; i++) {
|
|
10834
|
+
ws = d[i] + "";
|
|
10835
|
+
k = LOG_BASE - ws.length;
|
|
10836
|
+
if (k)
|
|
10837
|
+
str += getZeroString(k);
|
|
10838
|
+
str += ws;
|
|
10839
|
+
}
|
|
10840
|
+
w = d[i];
|
|
10841
|
+
ws = w + "";
|
|
10842
|
+
k = LOG_BASE - ws.length;
|
|
10843
|
+
if (k)
|
|
10844
|
+
str += getZeroString(k);
|
|
10845
|
+
} else if (w === 0) {
|
|
10846
|
+
return "0";
|
|
10847
|
+
}
|
|
10848
|
+
for (;w % 10 === 0; )
|
|
10849
|
+
w /= 10;
|
|
10850
|
+
return str + w;
|
|
10851
|
+
}
|
|
10852
|
+
function checkInt32(i, min, max) {
|
|
10853
|
+
if (i !== ~~i || i < min || i > max) {
|
|
10854
|
+
throw Error(invalidArgument + i);
|
|
10855
|
+
}
|
|
10856
|
+
}
|
|
10857
|
+
function checkRoundingDigits(d, i, rm, repeating) {
|
|
10858
|
+
var di, k, r, rd;
|
|
10859
|
+
for (k = d[0];k >= 10; k /= 10)
|
|
10860
|
+
--i;
|
|
10861
|
+
if (--i < 0) {
|
|
10862
|
+
i += LOG_BASE;
|
|
10863
|
+
di = 0;
|
|
10864
|
+
} else {
|
|
10865
|
+
di = Math.ceil((i + 1) / LOG_BASE);
|
|
10866
|
+
i %= LOG_BASE;
|
|
10867
|
+
}
|
|
10868
|
+
k = mathpow(10, LOG_BASE - i);
|
|
10869
|
+
rd = d[di] % k | 0;
|
|
10870
|
+
if (repeating == null) {
|
|
10871
|
+
if (i < 3) {
|
|
10872
|
+
if (i == 0)
|
|
10873
|
+
rd = rd / 100 | 0;
|
|
10874
|
+
else if (i == 1)
|
|
10875
|
+
rd = rd / 10 | 0;
|
|
10876
|
+
r = rm < 4 && rd == 99999 || rm > 3 && rd == 49999 || rd == 50000 || rd == 0;
|
|
10877
|
+
} else {
|
|
10878
|
+
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;
|
|
10879
|
+
}
|
|
10880
|
+
} else {
|
|
10881
|
+
if (i < 4) {
|
|
10882
|
+
if (i == 0)
|
|
10883
|
+
rd = rd / 1000 | 0;
|
|
10884
|
+
else if (i == 1)
|
|
10885
|
+
rd = rd / 100 | 0;
|
|
10886
|
+
else if (i == 2)
|
|
10887
|
+
rd = rd / 10 | 0;
|
|
10888
|
+
r = (repeating || rm < 4) && rd == 9999 || !repeating && rm > 3 && rd == 4999;
|
|
10889
|
+
} else {
|
|
10890
|
+
r = ((repeating || rm < 4) && rd + 1 == k || !repeating && rm > 3 && rd + 1 == k / 2) && (d[di + 1] / k / 1000 | 0) == mathpow(10, i - 3) - 1;
|
|
10891
|
+
}
|
|
10892
|
+
}
|
|
10893
|
+
return r;
|
|
10894
|
+
}
|
|
10895
|
+
function convertBase(str, baseIn, baseOut) {
|
|
10896
|
+
var j, arr = [0], arrL, i = 0, strL = str.length;
|
|
10897
|
+
for (;i < strL; ) {
|
|
10898
|
+
for (arrL = arr.length;arrL--; )
|
|
10899
|
+
arr[arrL] *= baseIn;
|
|
10900
|
+
arr[0] += NUMERALS.indexOf(str.charAt(i++));
|
|
10901
|
+
for (j = 0;j < arr.length; j++) {
|
|
10902
|
+
if (arr[j] > baseOut - 1) {
|
|
10903
|
+
if (arr[j + 1] === undefined)
|
|
10904
|
+
arr[j + 1] = 0;
|
|
10905
|
+
arr[j + 1] += arr[j] / baseOut | 0;
|
|
10906
|
+
arr[j] %= baseOut;
|
|
10907
|
+
}
|
|
10908
|
+
}
|
|
10909
|
+
}
|
|
10910
|
+
return arr.reverse();
|
|
10911
|
+
}
|
|
10912
|
+
function cosine(Ctor, x) {
|
|
10913
|
+
var k, len, y;
|
|
10914
|
+
if (x.isZero())
|
|
10915
|
+
return x;
|
|
10916
|
+
len = x.d.length;
|
|
10917
|
+
if (len < 32) {
|
|
10918
|
+
k = Math.ceil(len / 3);
|
|
10919
|
+
y = (1 / tinyPow(4, k)).toString();
|
|
10920
|
+
} else {
|
|
10921
|
+
k = 16;
|
|
10922
|
+
y = "2.3283064365386962890625e-10";
|
|
10923
|
+
}
|
|
10924
|
+
Ctor.precision += k;
|
|
10925
|
+
x = taylorSeries(Ctor, 1, x.times(y), new Ctor(1));
|
|
10926
|
+
for (var i = k;i--; ) {
|
|
10927
|
+
var cos2x = x.times(x);
|
|
10928
|
+
x = cos2x.times(cos2x).minus(cos2x).times(8).plus(1);
|
|
10929
|
+
}
|
|
10930
|
+
Ctor.precision -= k;
|
|
10931
|
+
return x;
|
|
10932
|
+
}
|
|
10933
|
+
var divide = function() {
|
|
10934
|
+
function multiplyInteger(x, k, base) {
|
|
10935
|
+
var temp, carry = 0, i = x.length;
|
|
10936
|
+
for (x = x.slice();i--; ) {
|
|
10937
|
+
temp = x[i] * k + carry;
|
|
10938
|
+
x[i] = temp % base | 0;
|
|
10939
|
+
carry = temp / base | 0;
|
|
10940
|
+
}
|
|
10941
|
+
if (carry)
|
|
10942
|
+
x.unshift(carry);
|
|
10943
|
+
return x;
|
|
10944
|
+
}
|
|
10945
|
+
function compare(a, b, aL, bL) {
|
|
10946
|
+
var i, r;
|
|
10947
|
+
if (aL != bL) {
|
|
10948
|
+
r = aL > bL ? 1 : -1;
|
|
10949
|
+
} else {
|
|
10950
|
+
for (i = r = 0;i < aL; i++) {
|
|
10951
|
+
if (a[i] != b[i]) {
|
|
10952
|
+
r = a[i] > b[i] ? 1 : -1;
|
|
10953
|
+
break;
|
|
10954
|
+
}
|
|
10955
|
+
}
|
|
10956
|
+
}
|
|
10957
|
+
return r;
|
|
10958
|
+
}
|
|
10959
|
+
function subtract(a, b, aL, base) {
|
|
10960
|
+
var i = 0;
|
|
10961
|
+
for (;aL--; ) {
|
|
10962
|
+
a[aL] -= i;
|
|
10963
|
+
i = a[aL] < b[aL] ? 1 : 0;
|
|
10964
|
+
a[aL] = i * base + a[aL] - b[aL];
|
|
10965
|
+
}
|
|
10966
|
+
for (;!a[0] && a.length > 1; )
|
|
10967
|
+
a.shift();
|
|
10968
|
+
}
|
|
10969
|
+
return function(x, y, pr, rm, dp, base) {
|
|
10970
|
+
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;
|
|
10971
|
+
if (!xd || !xd[0] || !yd || !yd[0]) {
|
|
10972
|
+
return new Ctor(!x.s || !y.s || (xd ? yd && xd[0] == yd[0] : !yd) ? NaN : xd && xd[0] == 0 || !yd ? sign * 0 : sign / 0);
|
|
10973
|
+
}
|
|
10974
|
+
if (base) {
|
|
10975
|
+
logBase = 1;
|
|
10976
|
+
e = x.e - y.e;
|
|
10977
|
+
} else {
|
|
10978
|
+
base = BASE;
|
|
10979
|
+
logBase = LOG_BASE;
|
|
10980
|
+
e = mathfloor(x.e / logBase) - mathfloor(y.e / logBase);
|
|
10981
|
+
}
|
|
10982
|
+
yL = yd.length;
|
|
10983
|
+
xL = xd.length;
|
|
10984
|
+
q = new Ctor(sign);
|
|
10985
|
+
qd = q.d = [];
|
|
10986
|
+
for (i = 0;yd[i] == (xd[i] || 0); i++)
|
|
10987
|
+
;
|
|
10988
|
+
if (yd[i] > (xd[i] || 0))
|
|
10989
|
+
e--;
|
|
10990
|
+
if (pr == null) {
|
|
10991
|
+
sd = pr = Ctor.precision;
|
|
10992
|
+
rm = Ctor.rounding;
|
|
10993
|
+
} else if (dp) {
|
|
10994
|
+
sd = pr + (x.e - y.e) + 1;
|
|
10995
|
+
} else {
|
|
10996
|
+
sd = pr;
|
|
10997
|
+
}
|
|
10998
|
+
if (sd < 0) {
|
|
10999
|
+
qd.push(1);
|
|
11000
|
+
more = true;
|
|
11001
|
+
} else {
|
|
11002
|
+
sd = sd / logBase + 2 | 0;
|
|
11003
|
+
i = 0;
|
|
11004
|
+
if (yL == 1) {
|
|
11005
|
+
k = 0;
|
|
11006
|
+
yd = yd[0];
|
|
11007
|
+
sd++;
|
|
11008
|
+
for (;(i < xL || k) && sd--; i++) {
|
|
11009
|
+
t = k * base + (xd[i] || 0);
|
|
11010
|
+
qd[i] = t / yd | 0;
|
|
11011
|
+
k = t % yd | 0;
|
|
11012
|
+
}
|
|
11013
|
+
more = k || i < xL;
|
|
11014
|
+
} else {
|
|
11015
|
+
k = base / (yd[0] + 1) | 0;
|
|
11016
|
+
if (k > 1) {
|
|
11017
|
+
yd = multiplyInteger(yd, k, base);
|
|
11018
|
+
xd = multiplyInteger(xd, k, base);
|
|
11019
|
+
yL = yd.length;
|
|
11020
|
+
xL = xd.length;
|
|
11021
|
+
}
|
|
11022
|
+
xi = yL;
|
|
11023
|
+
rem = xd.slice(0, yL);
|
|
11024
|
+
remL = rem.length;
|
|
11025
|
+
for (;remL < yL; )
|
|
11026
|
+
rem[remL++] = 0;
|
|
11027
|
+
yz = yd.slice();
|
|
11028
|
+
yz.unshift(0);
|
|
11029
|
+
yd0 = yd[0];
|
|
11030
|
+
if (yd[1] >= base / 2)
|
|
11031
|
+
++yd0;
|
|
11032
|
+
do {
|
|
11033
|
+
k = 0;
|
|
11034
|
+
cmp = compare(yd, rem, yL, remL);
|
|
11035
|
+
if (cmp < 0) {
|
|
11036
|
+
rem0 = rem[0];
|
|
11037
|
+
if (yL != remL)
|
|
11038
|
+
rem0 = rem0 * base + (rem[1] || 0);
|
|
11039
|
+
k = rem0 / yd0 | 0;
|
|
11040
|
+
if (k > 1) {
|
|
11041
|
+
if (k >= base)
|
|
11042
|
+
k = base - 1;
|
|
11043
|
+
prod = multiplyInteger(yd, k, base);
|
|
11044
|
+
prodL = prod.length;
|
|
11045
|
+
remL = rem.length;
|
|
11046
|
+
cmp = compare(prod, rem, prodL, remL);
|
|
11047
|
+
if (cmp == 1) {
|
|
11048
|
+
k--;
|
|
11049
|
+
subtract(prod, yL < prodL ? yz : yd, prodL, base);
|
|
11050
|
+
}
|
|
11051
|
+
} else {
|
|
11052
|
+
if (k == 0)
|
|
11053
|
+
cmp = k = 1;
|
|
11054
|
+
prod = yd.slice();
|
|
11055
|
+
}
|
|
11056
|
+
prodL = prod.length;
|
|
11057
|
+
if (prodL < remL)
|
|
11058
|
+
prod.unshift(0);
|
|
11059
|
+
subtract(rem, prod, remL, base);
|
|
11060
|
+
if (cmp == -1) {
|
|
11061
|
+
remL = rem.length;
|
|
11062
|
+
cmp = compare(yd, rem, yL, remL);
|
|
11063
|
+
if (cmp < 1) {
|
|
11064
|
+
k++;
|
|
11065
|
+
subtract(rem, yL < remL ? yz : yd, remL, base);
|
|
11066
|
+
}
|
|
11067
|
+
}
|
|
11068
|
+
remL = rem.length;
|
|
11069
|
+
} else if (cmp === 0) {
|
|
11070
|
+
k++;
|
|
11071
|
+
rem = [0];
|
|
11072
|
+
}
|
|
11073
|
+
qd[i++] = k;
|
|
11074
|
+
if (cmp && rem[0]) {
|
|
11075
|
+
rem[remL++] = xd[xi] || 0;
|
|
11076
|
+
} else {
|
|
11077
|
+
rem = [xd[xi]];
|
|
11078
|
+
remL = 1;
|
|
11079
|
+
}
|
|
11080
|
+
} while ((xi++ < xL || rem[0] !== undefined) && sd--);
|
|
11081
|
+
more = rem[0] !== undefined;
|
|
11082
|
+
}
|
|
11083
|
+
if (!qd[0])
|
|
11084
|
+
qd.shift();
|
|
11085
|
+
}
|
|
11086
|
+
if (logBase == 1) {
|
|
11087
|
+
q.e = e;
|
|
11088
|
+
inexact = more;
|
|
11089
|
+
} else {
|
|
11090
|
+
for (i = 1, k = qd[0];k >= 10; k /= 10)
|
|
11091
|
+
i++;
|
|
11092
|
+
q.e = i + e * logBase - 1;
|
|
11093
|
+
finalise(q, dp ? pr + q.e + 1 : pr, rm, more);
|
|
11094
|
+
}
|
|
11095
|
+
return q;
|
|
11096
|
+
};
|
|
11097
|
+
}();
|
|
11098
|
+
function finalise(x, sd, rm, isTruncated) {
|
|
11099
|
+
var digits, i, j, k, rd, roundUp, w, xd, xdi, Ctor = x.constructor;
|
|
11100
|
+
out:
|
|
11101
|
+
if (sd != null) {
|
|
11102
|
+
xd = x.d;
|
|
11103
|
+
if (!xd)
|
|
11104
|
+
return x;
|
|
11105
|
+
for (digits = 1, k = xd[0];k >= 10; k /= 10)
|
|
11106
|
+
digits++;
|
|
11107
|
+
i = sd - digits;
|
|
11108
|
+
if (i < 0) {
|
|
11109
|
+
i += LOG_BASE;
|
|
11110
|
+
j = sd;
|
|
11111
|
+
w = xd[xdi = 0];
|
|
11112
|
+
rd = w / mathpow(10, digits - j - 1) % 10 | 0;
|
|
11113
|
+
} else {
|
|
11114
|
+
xdi = Math.ceil((i + 1) / LOG_BASE);
|
|
11115
|
+
k = xd.length;
|
|
11116
|
+
if (xdi >= k) {
|
|
11117
|
+
if (isTruncated) {
|
|
11118
|
+
for (;k++ <= xdi; )
|
|
11119
|
+
xd.push(0);
|
|
11120
|
+
w = rd = 0;
|
|
11121
|
+
digits = 1;
|
|
11122
|
+
i %= LOG_BASE;
|
|
11123
|
+
j = i - LOG_BASE + 1;
|
|
11124
|
+
} else {
|
|
11125
|
+
break out;
|
|
11126
|
+
}
|
|
11127
|
+
} else {
|
|
11128
|
+
w = k = xd[xdi];
|
|
11129
|
+
for (digits = 1;k >= 10; k /= 10)
|
|
11130
|
+
digits++;
|
|
11131
|
+
i %= LOG_BASE;
|
|
11132
|
+
j = i - LOG_BASE + digits;
|
|
11133
|
+
rd = j < 0 ? 0 : w / mathpow(10, digits - j - 1) % 10 | 0;
|
|
11134
|
+
}
|
|
11135
|
+
}
|
|
11136
|
+
isTruncated = isTruncated || sd < 0 || xd[xdi + 1] !== undefined || (j < 0 ? w : w % mathpow(10, digits - j - 1));
|
|
11137
|
+
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));
|
|
11138
|
+
if (sd < 1 || !xd[0]) {
|
|
11139
|
+
xd.length = 0;
|
|
11140
|
+
if (roundUp) {
|
|
11141
|
+
sd -= x.e + 1;
|
|
11142
|
+
xd[0] = mathpow(10, (LOG_BASE - sd % LOG_BASE) % LOG_BASE);
|
|
11143
|
+
x.e = -sd || 0;
|
|
11144
|
+
} else {
|
|
11145
|
+
xd[0] = x.e = 0;
|
|
11146
|
+
}
|
|
11147
|
+
return x;
|
|
11148
|
+
}
|
|
11149
|
+
if (i == 0) {
|
|
11150
|
+
xd.length = xdi;
|
|
11151
|
+
k = 1;
|
|
11152
|
+
xdi--;
|
|
11153
|
+
} else {
|
|
11154
|
+
xd.length = xdi + 1;
|
|
11155
|
+
k = mathpow(10, LOG_BASE - i);
|
|
11156
|
+
xd[xdi] = j > 0 ? (w / mathpow(10, digits - j) % mathpow(10, j) | 0) * k : 0;
|
|
11157
|
+
}
|
|
11158
|
+
if (roundUp) {
|
|
11159
|
+
for (;; ) {
|
|
11160
|
+
if (xdi == 0) {
|
|
11161
|
+
for (i = 1, j = xd[0];j >= 10; j /= 10)
|
|
11162
|
+
i++;
|
|
11163
|
+
j = xd[0] += k;
|
|
11164
|
+
for (k = 1;j >= 10; j /= 10)
|
|
11165
|
+
k++;
|
|
11166
|
+
if (i != k) {
|
|
11167
|
+
x.e++;
|
|
11168
|
+
if (xd[0] == BASE)
|
|
11169
|
+
xd[0] = 1;
|
|
11170
|
+
}
|
|
11171
|
+
break;
|
|
11172
|
+
} else {
|
|
11173
|
+
xd[xdi] += k;
|
|
11174
|
+
if (xd[xdi] != BASE)
|
|
11175
|
+
break;
|
|
11176
|
+
xd[xdi--] = 0;
|
|
11177
|
+
k = 1;
|
|
11178
|
+
}
|
|
11179
|
+
}
|
|
11180
|
+
}
|
|
11181
|
+
for (i = xd.length;xd[--i] === 0; )
|
|
11182
|
+
xd.pop();
|
|
11183
|
+
}
|
|
11184
|
+
if (external2) {
|
|
11185
|
+
if (x.e > Ctor.maxE) {
|
|
11186
|
+
x.d = null;
|
|
11187
|
+
x.e = NaN;
|
|
11188
|
+
} else if (x.e < Ctor.minE) {
|
|
11189
|
+
x.e = 0;
|
|
11190
|
+
x.d = [0];
|
|
11191
|
+
}
|
|
11192
|
+
}
|
|
11193
|
+
return x;
|
|
11194
|
+
}
|
|
11195
|
+
function finiteToString(x, isExp, sd) {
|
|
11196
|
+
if (!x.isFinite())
|
|
11197
|
+
return nonFiniteToString(x);
|
|
11198
|
+
var k, e = x.e, str = digitsToString(x.d), len = str.length;
|
|
11199
|
+
if (isExp) {
|
|
11200
|
+
if (sd && (k = sd - len) > 0) {
|
|
11201
|
+
str = str.charAt(0) + "." + str.slice(1) + getZeroString(k);
|
|
11202
|
+
} else if (len > 1) {
|
|
11203
|
+
str = str.charAt(0) + "." + str.slice(1);
|
|
11204
|
+
}
|
|
11205
|
+
str = str + (x.e < 0 ? "e" : "e+") + x.e;
|
|
11206
|
+
} else if (e < 0) {
|
|
11207
|
+
str = "0." + getZeroString(-e - 1) + str;
|
|
11208
|
+
if (sd && (k = sd - len) > 0)
|
|
11209
|
+
str += getZeroString(k);
|
|
11210
|
+
} else if (e >= len) {
|
|
11211
|
+
str += getZeroString(e + 1 - len);
|
|
11212
|
+
if (sd && (k = sd - e - 1) > 0)
|
|
11213
|
+
str = str + "." + getZeroString(k);
|
|
11214
|
+
} else {
|
|
11215
|
+
if ((k = e + 1) < len)
|
|
11216
|
+
str = str.slice(0, k) + "." + str.slice(k);
|
|
11217
|
+
if (sd && (k = sd - len) > 0) {
|
|
11218
|
+
if (e + 1 === len)
|
|
11219
|
+
str += ".";
|
|
11220
|
+
str += getZeroString(k);
|
|
11221
|
+
}
|
|
11222
|
+
}
|
|
11223
|
+
return str;
|
|
11224
|
+
}
|
|
11225
|
+
function getBase10Exponent(digits, e) {
|
|
11226
|
+
var w = digits[0];
|
|
11227
|
+
for (e *= LOG_BASE;w >= 10; w /= 10)
|
|
11228
|
+
e++;
|
|
11229
|
+
return e;
|
|
11230
|
+
}
|
|
11231
|
+
function getLn10(Ctor, sd, pr) {
|
|
11232
|
+
if (sd > LN10_PRECISION) {
|
|
11233
|
+
external2 = true;
|
|
11234
|
+
if (pr)
|
|
11235
|
+
Ctor.precision = pr;
|
|
11236
|
+
throw Error(precisionLimitExceeded);
|
|
11237
|
+
}
|
|
11238
|
+
return finalise(new Ctor(LN10), sd, 1, true);
|
|
11239
|
+
}
|
|
11240
|
+
function getPi(Ctor, sd, rm) {
|
|
11241
|
+
if (sd > PI_PRECISION)
|
|
11242
|
+
throw Error(precisionLimitExceeded);
|
|
11243
|
+
return finalise(new Ctor(PI), sd, rm, true);
|
|
11244
|
+
}
|
|
11245
|
+
function getPrecision(digits) {
|
|
11246
|
+
var w = digits.length - 1, len = w * LOG_BASE + 1;
|
|
11247
|
+
w = digits[w];
|
|
11248
|
+
if (w) {
|
|
11249
|
+
for (;w % 10 == 0; w /= 10)
|
|
11250
|
+
len--;
|
|
11251
|
+
for (w = digits[0];w >= 10; w /= 10)
|
|
11252
|
+
len++;
|
|
11253
|
+
}
|
|
11254
|
+
return len;
|
|
11255
|
+
}
|
|
11256
|
+
function getZeroString(k) {
|
|
11257
|
+
var zs = "";
|
|
11258
|
+
for (;k--; )
|
|
11259
|
+
zs += "0";
|
|
11260
|
+
return zs;
|
|
11261
|
+
}
|
|
11262
|
+
function intPow(Ctor, x, n, pr) {
|
|
11263
|
+
var isTruncated, r = new Ctor(1), k = Math.ceil(pr / LOG_BASE + 4);
|
|
11264
|
+
external2 = false;
|
|
11265
|
+
for (;; ) {
|
|
11266
|
+
if (n % 2) {
|
|
11267
|
+
r = r.times(x);
|
|
11268
|
+
if (truncate(r.d, k))
|
|
11269
|
+
isTruncated = true;
|
|
11270
|
+
}
|
|
11271
|
+
n = mathfloor(n / 2);
|
|
11272
|
+
if (n === 0) {
|
|
11273
|
+
n = r.d.length - 1;
|
|
11274
|
+
if (isTruncated && r.d[n] === 0)
|
|
11275
|
+
++r.d[n];
|
|
11276
|
+
break;
|
|
11277
|
+
}
|
|
11278
|
+
x = x.times(x);
|
|
11279
|
+
truncate(x.d, k);
|
|
11280
|
+
}
|
|
11281
|
+
external2 = true;
|
|
11282
|
+
return r;
|
|
11283
|
+
}
|
|
11284
|
+
function isOdd(n) {
|
|
11285
|
+
return n.d[n.d.length - 1] & 1;
|
|
11286
|
+
}
|
|
11287
|
+
function maxOrMin(Ctor, args, n) {
|
|
11288
|
+
var k, y, x = new Ctor(args[0]), i = 0;
|
|
11289
|
+
for (;++i < args.length; ) {
|
|
11290
|
+
y = new Ctor(args[i]);
|
|
11291
|
+
if (!y.s) {
|
|
11292
|
+
x = y;
|
|
11293
|
+
break;
|
|
11294
|
+
}
|
|
11295
|
+
k = x.cmp(y);
|
|
11296
|
+
if (k === n || k === 0 && x.s === n) {
|
|
11297
|
+
x = y;
|
|
11298
|
+
}
|
|
11299
|
+
}
|
|
11300
|
+
return x;
|
|
11301
|
+
}
|
|
11302
|
+
function naturalExponential(x, sd) {
|
|
11303
|
+
var denominator, guard, j, pow, sum, t, wpr, rep = 0, i = 0, k = 0, Ctor = x.constructor, rm = Ctor.rounding, pr = Ctor.precision;
|
|
11304
|
+
if (!x.d || !x.d[0] || x.e > 17) {
|
|
11305
|
+
return new Ctor(x.d ? !x.d[0] ? 1 : x.s < 0 ? 0 : 1 / 0 : x.s ? x.s < 0 ? 0 : x : 0 / 0);
|
|
11306
|
+
}
|
|
11307
|
+
if (sd == null) {
|
|
11308
|
+
external2 = false;
|
|
11309
|
+
wpr = pr;
|
|
11310
|
+
} else {
|
|
11311
|
+
wpr = sd;
|
|
11312
|
+
}
|
|
11313
|
+
t = new Ctor(0.03125);
|
|
11314
|
+
while (x.e > -2) {
|
|
11315
|
+
x = x.times(t);
|
|
11316
|
+
k += 5;
|
|
11317
|
+
}
|
|
11318
|
+
guard = Math.log(mathpow(2, k)) / Math.LN10 * 2 + 5 | 0;
|
|
11319
|
+
wpr += guard;
|
|
11320
|
+
denominator = pow = sum = new Ctor(1);
|
|
11321
|
+
Ctor.precision = wpr;
|
|
11322
|
+
for (;; ) {
|
|
11323
|
+
pow = finalise(pow.times(x), wpr, 1);
|
|
11324
|
+
denominator = denominator.times(++i);
|
|
11325
|
+
t = sum.plus(divide(pow, denominator, wpr, 1));
|
|
11326
|
+
if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) {
|
|
11327
|
+
j = k;
|
|
11328
|
+
while (j--)
|
|
11329
|
+
sum = finalise(sum.times(sum), wpr, 1);
|
|
11330
|
+
if (sd == null) {
|
|
11331
|
+
if (rep < 3 && checkRoundingDigits(sum.d, wpr - guard, rm, rep)) {
|
|
11332
|
+
Ctor.precision = wpr += 10;
|
|
11333
|
+
denominator = pow = t = new Ctor(1);
|
|
11334
|
+
i = 0;
|
|
11335
|
+
rep++;
|
|
11336
|
+
} else {
|
|
11337
|
+
return finalise(sum, Ctor.precision = pr, rm, external2 = true);
|
|
11338
|
+
}
|
|
11339
|
+
} else {
|
|
11340
|
+
Ctor.precision = pr;
|
|
11341
|
+
return sum;
|
|
11342
|
+
}
|
|
11343
|
+
}
|
|
11344
|
+
sum = t;
|
|
11345
|
+
}
|
|
11346
|
+
}
|
|
11347
|
+
function naturalLogarithm(y, sd) {
|
|
11348
|
+
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;
|
|
11349
|
+
if (x.s < 0 || !xd || !xd[0] || !x.e && xd[0] == 1 && xd.length == 1) {
|
|
11350
|
+
return new Ctor(xd && !xd[0] ? -1 / 0 : x.s != 1 ? NaN : xd ? 0 : x);
|
|
11351
|
+
}
|
|
11352
|
+
if (sd == null) {
|
|
11353
|
+
external2 = false;
|
|
11354
|
+
wpr = pr;
|
|
11355
|
+
} else {
|
|
11356
|
+
wpr = sd;
|
|
11357
|
+
}
|
|
11358
|
+
Ctor.precision = wpr += guard;
|
|
11359
|
+
c = digitsToString(xd);
|
|
11360
|
+
c0 = c.charAt(0);
|
|
11361
|
+
if (Math.abs(e = x.e) < 1500000000000000) {
|
|
11362
|
+
while (c0 < 7 && c0 != 1 || c0 == 1 && c.charAt(1) > 3) {
|
|
11363
|
+
x = x.times(y);
|
|
11364
|
+
c = digitsToString(x.d);
|
|
11365
|
+
c0 = c.charAt(0);
|
|
11366
|
+
n++;
|
|
11367
|
+
}
|
|
11368
|
+
e = x.e;
|
|
11369
|
+
if (c0 > 1) {
|
|
11370
|
+
x = new Ctor("0." + c);
|
|
11371
|
+
e++;
|
|
11372
|
+
} else {
|
|
11373
|
+
x = new Ctor(c0 + "." + c.slice(1));
|
|
11374
|
+
}
|
|
11375
|
+
} else {
|
|
11376
|
+
t = getLn10(Ctor, wpr + 2, pr).times(e + "");
|
|
11377
|
+
x = naturalLogarithm(new Ctor(c0 + "." + c.slice(1)), wpr - guard).plus(t);
|
|
11378
|
+
Ctor.precision = pr;
|
|
11379
|
+
return sd == null ? finalise(x, pr, rm, external2 = true) : x;
|
|
11380
|
+
}
|
|
11381
|
+
x1 = x;
|
|
11382
|
+
sum = numerator = x = divide(x.minus(1), x.plus(1), wpr, 1);
|
|
11383
|
+
x2 = finalise(x.times(x), wpr, 1);
|
|
11384
|
+
denominator = 3;
|
|
11385
|
+
for (;; ) {
|
|
11386
|
+
numerator = finalise(numerator.times(x2), wpr, 1);
|
|
11387
|
+
t = sum.plus(divide(numerator, new Ctor(denominator), wpr, 1));
|
|
11388
|
+
if (digitsToString(t.d).slice(0, wpr) === digitsToString(sum.d).slice(0, wpr)) {
|
|
11389
|
+
sum = sum.times(2);
|
|
11390
|
+
if (e !== 0)
|
|
11391
|
+
sum = sum.plus(getLn10(Ctor, wpr + 2, pr).times(e + ""));
|
|
11392
|
+
sum = divide(sum, new Ctor(n), wpr, 1);
|
|
11393
|
+
if (sd == null) {
|
|
11394
|
+
if (checkRoundingDigits(sum.d, wpr - guard, rm, rep)) {
|
|
11395
|
+
Ctor.precision = wpr += guard;
|
|
11396
|
+
t = numerator = x = divide(x1.minus(1), x1.plus(1), wpr, 1);
|
|
11397
|
+
x2 = finalise(x.times(x), wpr, 1);
|
|
11398
|
+
denominator = rep = 1;
|
|
11399
|
+
} else {
|
|
11400
|
+
return finalise(sum, Ctor.precision = pr, rm, external2 = true);
|
|
11401
|
+
}
|
|
11402
|
+
} else {
|
|
11403
|
+
Ctor.precision = pr;
|
|
11404
|
+
return sum;
|
|
11405
|
+
}
|
|
11406
|
+
}
|
|
11407
|
+
sum = t;
|
|
11408
|
+
denominator += 2;
|
|
11409
|
+
}
|
|
11410
|
+
}
|
|
11411
|
+
function nonFiniteToString(x) {
|
|
11412
|
+
return String(x.s * x.s / 0);
|
|
11413
|
+
}
|
|
11414
|
+
function parseDecimal(x, str) {
|
|
11415
|
+
var e, i, len;
|
|
11416
|
+
if ((e = str.indexOf(".")) > -1)
|
|
11417
|
+
str = str.replace(".", "");
|
|
11418
|
+
if ((i = str.search(/e/i)) > 0) {
|
|
11419
|
+
if (e < 0)
|
|
11420
|
+
e = i;
|
|
11421
|
+
e += +str.slice(i + 1);
|
|
11422
|
+
str = str.substring(0, i);
|
|
11423
|
+
} else if (e < 0) {
|
|
11424
|
+
e = str.length;
|
|
11425
|
+
}
|
|
11426
|
+
for (i = 0;str.charCodeAt(i) === 48; i++)
|
|
11427
|
+
;
|
|
11428
|
+
for (len = str.length;str.charCodeAt(len - 1) === 48; --len)
|
|
11429
|
+
;
|
|
11430
|
+
str = str.slice(i, len);
|
|
11431
|
+
if (str) {
|
|
11432
|
+
len -= i;
|
|
11433
|
+
x.e = e = e - i - 1;
|
|
11434
|
+
x.d = [];
|
|
11435
|
+
i = (e + 1) % LOG_BASE;
|
|
11436
|
+
if (e < 0)
|
|
11437
|
+
i += LOG_BASE;
|
|
11438
|
+
if (i < len) {
|
|
11439
|
+
if (i)
|
|
11440
|
+
x.d.push(+str.slice(0, i));
|
|
11441
|
+
for (len -= LOG_BASE;i < len; )
|
|
11442
|
+
x.d.push(+str.slice(i, i += LOG_BASE));
|
|
11443
|
+
str = str.slice(i);
|
|
11444
|
+
i = LOG_BASE - str.length;
|
|
11445
|
+
} else {
|
|
11446
|
+
i -= len;
|
|
11447
|
+
}
|
|
11448
|
+
for (;i--; )
|
|
11449
|
+
str += "0";
|
|
11450
|
+
x.d.push(+str);
|
|
11451
|
+
if (external2) {
|
|
11452
|
+
if (x.e > x.constructor.maxE) {
|
|
11453
|
+
x.d = null;
|
|
11454
|
+
x.e = NaN;
|
|
11455
|
+
} else if (x.e < x.constructor.minE) {
|
|
11456
|
+
x.e = 0;
|
|
11457
|
+
x.d = [0];
|
|
11458
|
+
}
|
|
11459
|
+
}
|
|
11460
|
+
} else {
|
|
11461
|
+
x.e = 0;
|
|
11462
|
+
x.d = [0];
|
|
11463
|
+
}
|
|
11464
|
+
return x;
|
|
11465
|
+
}
|
|
11466
|
+
function parseOther(x, str) {
|
|
11467
|
+
var base, Ctor, divisor, i, isFloat, len, p, xd, xe;
|
|
11468
|
+
if (str.indexOf("_") > -1) {
|
|
11469
|
+
str = str.replace(/(\d)_(?=\d)/g, "$1");
|
|
11470
|
+
if (isDecimal.test(str))
|
|
11471
|
+
return parseDecimal(x, str);
|
|
11472
|
+
} else if (str === "Infinity" || str === "NaN") {
|
|
11473
|
+
if (!+str)
|
|
11474
|
+
x.s = NaN;
|
|
11475
|
+
x.e = NaN;
|
|
11476
|
+
x.d = null;
|
|
11477
|
+
return x;
|
|
11478
|
+
}
|
|
11479
|
+
if (isHex.test(str)) {
|
|
11480
|
+
base = 16;
|
|
11481
|
+
str = str.toLowerCase();
|
|
11482
|
+
} else if (isBinary.test(str)) {
|
|
11483
|
+
base = 2;
|
|
11484
|
+
} else if (isOctal.test(str)) {
|
|
11485
|
+
base = 8;
|
|
11486
|
+
} else {
|
|
11487
|
+
throw Error(invalidArgument + str);
|
|
11488
|
+
}
|
|
11489
|
+
i = str.search(/p/i);
|
|
11490
|
+
if (i > 0) {
|
|
11491
|
+
p = +str.slice(i + 1);
|
|
11492
|
+
str = str.substring(2, i);
|
|
11493
|
+
} else {
|
|
11494
|
+
str = str.slice(2);
|
|
11495
|
+
}
|
|
11496
|
+
i = str.indexOf(".");
|
|
11497
|
+
isFloat = i >= 0;
|
|
11498
|
+
Ctor = x.constructor;
|
|
11499
|
+
if (isFloat) {
|
|
11500
|
+
str = str.replace(".", "");
|
|
11501
|
+
len = str.length;
|
|
11502
|
+
i = len - i;
|
|
11503
|
+
divisor = intPow(Ctor, new Ctor(base), i, i * 2);
|
|
11504
|
+
}
|
|
11505
|
+
xd = convertBase(str, base, BASE);
|
|
11506
|
+
xe = xd.length - 1;
|
|
11507
|
+
for (i = xe;xd[i] === 0; --i)
|
|
11508
|
+
xd.pop();
|
|
11509
|
+
if (i < 0)
|
|
11510
|
+
return new Ctor(x.s * 0);
|
|
11511
|
+
x.e = getBase10Exponent(xd, xe);
|
|
11512
|
+
x.d = xd;
|
|
11513
|
+
external2 = false;
|
|
11514
|
+
if (isFloat)
|
|
11515
|
+
x = divide(x, divisor, len * 4);
|
|
11516
|
+
if (p)
|
|
11517
|
+
x = x.times(Math.abs(p) < 54 ? mathpow(2, p) : Decimal.pow(2, p));
|
|
11518
|
+
external2 = true;
|
|
11519
|
+
return x;
|
|
11520
|
+
}
|
|
11521
|
+
function sine(Ctor, x) {
|
|
11522
|
+
var k, len = x.d.length;
|
|
11523
|
+
if (len < 3) {
|
|
11524
|
+
return x.isZero() ? x : taylorSeries(Ctor, 2, x, x);
|
|
11525
|
+
}
|
|
11526
|
+
k = 1.4 * Math.sqrt(len);
|
|
11527
|
+
k = k > 16 ? 16 : k | 0;
|
|
11528
|
+
x = x.times(1 / tinyPow(5, k));
|
|
11529
|
+
x = taylorSeries(Ctor, 2, x, x);
|
|
11530
|
+
var sin2_x, d5 = new Ctor(5), d16 = new Ctor(16), d20 = new Ctor(20);
|
|
11531
|
+
for (;k--; ) {
|
|
11532
|
+
sin2_x = x.times(x);
|
|
11533
|
+
x = x.times(d5.plus(sin2_x.times(d16.times(sin2_x).minus(d20))));
|
|
11534
|
+
}
|
|
11535
|
+
return x;
|
|
11536
|
+
}
|
|
11537
|
+
function taylorSeries(Ctor, n, x, y, isHyperbolic) {
|
|
11538
|
+
var j, t, u, x2, i = 1, pr = Ctor.precision, k = Math.ceil(pr / LOG_BASE);
|
|
11539
|
+
external2 = false;
|
|
11540
|
+
x2 = x.times(x);
|
|
11541
|
+
u = new Ctor(y);
|
|
11542
|
+
for (;; ) {
|
|
11543
|
+
t = divide(u.times(x2), new Ctor(n++ * n++), pr, 1);
|
|
11544
|
+
u = isHyperbolic ? y.plus(t) : y.minus(t);
|
|
11545
|
+
y = divide(t.times(x2), new Ctor(n++ * n++), pr, 1);
|
|
11546
|
+
t = u.plus(y);
|
|
11547
|
+
if (t.d[k] !== undefined) {
|
|
11548
|
+
for (j = k;t.d[j] === u.d[j] && j--; )
|
|
11549
|
+
;
|
|
11550
|
+
if (j == -1)
|
|
11551
|
+
break;
|
|
11552
|
+
}
|
|
11553
|
+
j = u;
|
|
11554
|
+
u = y;
|
|
11555
|
+
y = t;
|
|
11556
|
+
t = j;
|
|
11557
|
+
i++;
|
|
11558
|
+
}
|
|
11559
|
+
external2 = true;
|
|
11560
|
+
t.d.length = k + 1;
|
|
11561
|
+
return t;
|
|
11562
|
+
}
|
|
11563
|
+
function tinyPow(b, e) {
|
|
11564
|
+
var n = b;
|
|
11565
|
+
while (--e)
|
|
11566
|
+
n *= b;
|
|
11567
|
+
return n;
|
|
11568
|
+
}
|
|
11569
|
+
function toLessThanHalfPi(Ctor, x) {
|
|
11570
|
+
var t, isNeg = x.s < 0, pi = getPi(Ctor, Ctor.precision, 1), halfPi = pi.times(0.5);
|
|
11571
|
+
x = x.abs();
|
|
11572
|
+
if (x.lte(halfPi)) {
|
|
11573
|
+
quadrant = isNeg ? 4 : 1;
|
|
11574
|
+
return x;
|
|
11575
|
+
}
|
|
11576
|
+
t = x.divToInt(pi);
|
|
11577
|
+
if (t.isZero()) {
|
|
11578
|
+
quadrant = isNeg ? 3 : 2;
|
|
11579
|
+
} else {
|
|
11580
|
+
x = x.minus(t.times(pi));
|
|
11581
|
+
if (x.lte(halfPi)) {
|
|
11582
|
+
quadrant = isOdd(t) ? isNeg ? 2 : 3 : isNeg ? 4 : 1;
|
|
11583
|
+
return x;
|
|
11584
|
+
}
|
|
11585
|
+
quadrant = isOdd(t) ? isNeg ? 1 : 4 : isNeg ? 3 : 2;
|
|
11586
|
+
}
|
|
11587
|
+
return x.minus(pi).abs();
|
|
11588
|
+
}
|
|
11589
|
+
function toStringBinary(x, baseOut, sd, rm) {
|
|
11590
|
+
var base, e, i, k, len, roundUp, str, xd, y, Ctor = x.constructor, isExp = sd !== undefined;
|
|
11591
|
+
if (isExp) {
|
|
11592
|
+
checkInt32(sd, 1, MAX_DIGITS);
|
|
11593
|
+
if (rm === undefined)
|
|
11594
|
+
rm = Ctor.rounding;
|
|
11595
|
+
else
|
|
11596
|
+
checkInt32(rm, 0, 8);
|
|
11597
|
+
} else {
|
|
11598
|
+
sd = Ctor.precision;
|
|
11599
|
+
rm = Ctor.rounding;
|
|
11600
|
+
}
|
|
11601
|
+
if (!x.isFinite()) {
|
|
11602
|
+
str = nonFiniteToString(x);
|
|
11603
|
+
} else {
|
|
11604
|
+
str = finiteToString(x);
|
|
11605
|
+
i = str.indexOf(".");
|
|
11606
|
+
if (isExp) {
|
|
11607
|
+
base = 2;
|
|
11608
|
+
if (baseOut == 16) {
|
|
11609
|
+
sd = sd * 4 - 3;
|
|
11610
|
+
} else if (baseOut == 8) {
|
|
11611
|
+
sd = sd * 3 - 2;
|
|
11612
|
+
}
|
|
11613
|
+
} else {
|
|
11614
|
+
base = baseOut;
|
|
11615
|
+
}
|
|
11616
|
+
if (i >= 0) {
|
|
11617
|
+
str = str.replace(".", "");
|
|
11618
|
+
y = new Ctor(1);
|
|
11619
|
+
y.e = str.length - i;
|
|
11620
|
+
y.d = convertBase(finiteToString(y), 10, base);
|
|
11621
|
+
y.e = y.d.length;
|
|
11622
|
+
}
|
|
11623
|
+
xd = convertBase(str, 10, base);
|
|
11624
|
+
e = len = xd.length;
|
|
11625
|
+
for (;xd[--len] == 0; )
|
|
11626
|
+
xd.pop();
|
|
11627
|
+
if (!xd[0]) {
|
|
11628
|
+
str = isExp ? "0p+0" : "0";
|
|
11629
|
+
} else {
|
|
11630
|
+
if (i < 0) {
|
|
11631
|
+
e--;
|
|
11632
|
+
} else {
|
|
11633
|
+
x = new Ctor(x);
|
|
11634
|
+
x.d = xd;
|
|
11635
|
+
x.e = e;
|
|
11636
|
+
x = divide(x, y, sd, rm, 0, base);
|
|
11637
|
+
xd = x.d;
|
|
11638
|
+
e = x.e;
|
|
11639
|
+
roundUp = inexact;
|
|
11640
|
+
}
|
|
11641
|
+
i = xd[sd];
|
|
11642
|
+
k = base / 2;
|
|
11643
|
+
roundUp = roundUp || xd[sd + 1] !== undefined;
|
|
11644
|
+
roundUp = rm < 4 ? (i !== undefined || 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));
|
|
11645
|
+
xd.length = sd;
|
|
11646
|
+
if (roundUp) {
|
|
11647
|
+
for (;++xd[--sd] > base - 1; ) {
|
|
11648
|
+
xd[sd] = 0;
|
|
11649
|
+
if (!sd) {
|
|
11650
|
+
++e;
|
|
11651
|
+
xd.unshift(1);
|
|
11652
|
+
}
|
|
11653
|
+
}
|
|
11654
|
+
}
|
|
11655
|
+
for (len = xd.length;!xd[len - 1]; --len)
|
|
11656
|
+
;
|
|
11657
|
+
for (i = 0, str = "";i < len; i++)
|
|
11658
|
+
str += NUMERALS.charAt(xd[i]);
|
|
11659
|
+
if (isExp) {
|
|
11660
|
+
if (len > 1) {
|
|
11661
|
+
if (baseOut == 16 || baseOut == 8) {
|
|
11662
|
+
i = baseOut == 16 ? 4 : 3;
|
|
11663
|
+
for (--len;len % i; len++)
|
|
11664
|
+
str += "0";
|
|
11665
|
+
xd = convertBase(str, base, baseOut);
|
|
11666
|
+
for (len = xd.length;!xd[len - 1]; --len)
|
|
11667
|
+
;
|
|
11668
|
+
for (i = 1, str = "1.";i < len; i++)
|
|
11669
|
+
str += NUMERALS.charAt(xd[i]);
|
|
11670
|
+
} else {
|
|
11671
|
+
str = str.charAt(0) + "." + str.slice(1);
|
|
11672
|
+
}
|
|
11673
|
+
}
|
|
11674
|
+
str = str + (e < 0 ? "p" : "p+") + e;
|
|
11675
|
+
} else if (e < 0) {
|
|
11676
|
+
for (;++e; )
|
|
11677
|
+
str = "0" + str;
|
|
11678
|
+
str = "0." + str;
|
|
11679
|
+
} else {
|
|
11680
|
+
if (++e > len)
|
|
11681
|
+
for (e -= len;e--; )
|
|
11682
|
+
str += "0";
|
|
11683
|
+
else if (e < len)
|
|
11684
|
+
str = str.slice(0, e) + "." + str.slice(e);
|
|
11685
|
+
}
|
|
11686
|
+
}
|
|
11687
|
+
str = (baseOut == 16 ? "0x" : baseOut == 2 ? "0b" : baseOut == 8 ? "0o" : "") + str;
|
|
11688
|
+
}
|
|
11689
|
+
return x.s < 0 ? "-" + str : str;
|
|
11690
|
+
}
|
|
11691
|
+
function truncate(arr, len) {
|
|
11692
|
+
if (arr.length > len) {
|
|
11693
|
+
arr.length = len;
|
|
11694
|
+
return true;
|
|
11695
|
+
}
|
|
11696
|
+
}
|
|
11697
|
+
function abs(x) {
|
|
11698
|
+
return new this(x).abs();
|
|
11699
|
+
}
|
|
11700
|
+
function acos(x) {
|
|
11701
|
+
return new this(x).acos();
|
|
11702
|
+
}
|
|
11703
|
+
function acosh(x) {
|
|
11704
|
+
return new this(x).acosh();
|
|
11705
|
+
}
|
|
11706
|
+
function add(x, y) {
|
|
11707
|
+
return new this(x).plus(y);
|
|
11708
|
+
}
|
|
11709
|
+
function asin(x) {
|
|
11710
|
+
return new this(x).asin();
|
|
11711
|
+
}
|
|
11712
|
+
function asinh(x) {
|
|
11713
|
+
return new this(x).asinh();
|
|
11714
|
+
}
|
|
11715
|
+
function atan(x) {
|
|
11716
|
+
return new this(x).atan();
|
|
11717
|
+
}
|
|
11718
|
+
function atanh(x) {
|
|
11719
|
+
return new this(x).atanh();
|
|
11720
|
+
}
|
|
11721
|
+
function atan2(y, x) {
|
|
11722
|
+
y = new this(y);
|
|
11723
|
+
x = new this(x);
|
|
11724
|
+
var r, pr = this.precision, rm = this.rounding, wpr = pr + 4;
|
|
11725
|
+
if (!y.s || !x.s) {
|
|
11726
|
+
r = new this(NaN);
|
|
11727
|
+
} else if (!y.d && !x.d) {
|
|
11728
|
+
r = getPi(this, wpr, 1).times(x.s > 0 ? 0.25 : 0.75);
|
|
11729
|
+
r.s = y.s;
|
|
11730
|
+
} else if (!x.d || y.isZero()) {
|
|
11731
|
+
r = x.s < 0 ? getPi(this, pr, rm) : new this(0);
|
|
11732
|
+
r.s = y.s;
|
|
11733
|
+
} else if (!y.d || x.isZero()) {
|
|
11734
|
+
r = getPi(this, wpr, 1).times(0.5);
|
|
11735
|
+
r.s = y.s;
|
|
11736
|
+
} else if (x.s < 0) {
|
|
11737
|
+
this.precision = wpr;
|
|
11738
|
+
this.rounding = 1;
|
|
11739
|
+
r = this.atan(divide(y, x, wpr, 1));
|
|
11740
|
+
x = getPi(this, wpr, 1);
|
|
11741
|
+
this.precision = pr;
|
|
11742
|
+
this.rounding = rm;
|
|
11743
|
+
r = y.s < 0 ? r.minus(x) : r.plus(x);
|
|
11744
|
+
} else {
|
|
11745
|
+
r = this.atan(divide(y, x, wpr, 1));
|
|
11746
|
+
}
|
|
11747
|
+
return r;
|
|
11748
|
+
}
|
|
11749
|
+
function cbrt(x) {
|
|
11750
|
+
return new this(x).cbrt();
|
|
11751
|
+
}
|
|
11752
|
+
function ceil(x) {
|
|
11753
|
+
return finalise(x = new this(x), x.e + 1, 2);
|
|
11754
|
+
}
|
|
11755
|
+
function clamp(x, min, max) {
|
|
11756
|
+
return new this(x).clamp(min, max);
|
|
11757
|
+
}
|
|
11758
|
+
function config(obj) {
|
|
11759
|
+
if (!obj || typeof obj !== "object")
|
|
11760
|
+
throw Error(decimalError + "Object expected");
|
|
11761
|
+
var i, p, v, useDefaults = obj.defaults === true, ps = [
|
|
11762
|
+
"precision",
|
|
11763
|
+
1,
|
|
11764
|
+
MAX_DIGITS,
|
|
11765
|
+
"rounding",
|
|
11766
|
+
0,
|
|
11767
|
+
8,
|
|
11768
|
+
"toExpNeg",
|
|
11769
|
+
-EXP_LIMIT,
|
|
11770
|
+
0,
|
|
11771
|
+
"toExpPos",
|
|
11772
|
+
0,
|
|
11773
|
+
EXP_LIMIT,
|
|
11774
|
+
"maxE",
|
|
11775
|
+
0,
|
|
11776
|
+
EXP_LIMIT,
|
|
11777
|
+
"minE",
|
|
11778
|
+
-EXP_LIMIT,
|
|
11779
|
+
0,
|
|
11780
|
+
"modulo",
|
|
11781
|
+
0,
|
|
11782
|
+
9
|
|
11783
|
+
];
|
|
11784
|
+
for (i = 0;i < ps.length; i += 3) {
|
|
11785
|
+
if (p = ps[i], useDefaults)
|
|
11786
|
+
this[p] = DEFAULTS[p];
|
|
11787
|
+
if ((v = obj[p]) !== undefined) {
|
|
11788
|
+
if (mathfloor(v) === v && v >= ps[i + 1] && v <= ps[i + 2])
|
|
11789
|
+
this[p] = v;
|
|
11790
|
+
else
|
|
11791
|
+
throw Error(invalidArgument + p + ": " + v);
|
|
11792
|
+
}
|
|
11793
|
+
}
|
|
11794
|
+
if (p = "crypto", useDefaults)
|
|
11795
|
+
this[p] = DEFAULTS[p];
|
|
11796
|
+
if ((v = obj[p]) !== undefined) {
|
|
11797
|
+
if (v === true || v === false || v === 0 || v === 1) {
|
|
11798
|
+
if (v) {
|
|
11799
|
+
if (typeof crypto != "undefined" && crypto && (crypto.getRandomValues || crypto.randomBytes)) {
|
|
11800
|
+
this[p] = true;
|
|
11801
|
+
} else {
|
|
11802
|
+
throw Error(cryptoUnavailable);
|
|
11803
|
+
}
|
|
11804
|
+
} else {
|
|
11805
|
+
this[p] = false;
|
|
11806
|
+
}
|
|
11807
|
+
} else {
|
|
11808
|
+
throw Error(invalidArgument + p + ": " + v);
|
|
11809
|
+
}
|
|
11810
|
+
}
|
|
11811
|
+
return this;
|
|
11812
|
+
}
|
|
11813
|
+
function cos(x) {
|
|
11814
|
+
return new this(x).cos();
|
|
11815
|
+
}
|
|
11816
|
+
function cosh(x) {
|
|
11817
|
+
return new this(x).cosh();
|
|
11818
|
+
}
|
|
11819
|
+
function clone(obj) {
|
|
11820
|
+
var i, p, ps;
|
|
11821
|
+
function Decimal(v) {
|
|
11822
|
+
var e, i2, t, x = this;
|
|
11823
|
+
if (!(x instanceof Decimal))
|
|
11824
|
+
return new Decimal(v);
|
|
11825
|
+
x.constructor = Decimal;
|
|
11826
|
+
if (isDecimalInstance(v)) {
|
|
11827
|
+
x.s = v.s;
|
|
11828
|
+
if (external2) {
|
|
11829
|
+
if (!v.d || v.e > Decimal.maxE) {
|
|
11830
|
+
x.e = NaN;
|
|
11831
|
+
x.d = null;
|
|
11832
|
+
} else if (v.e < Decimal.minE) {
|
|
11833
|
+
x.e = 0;
|
|
11834
|
+
x.d = [0];
|
|
11835
|
+
} else {
|
|
11836
|
+
x.e = v.e;
|
|
11837
|
+
x.d = v.d.slice();
|
|
11838
|
+
}
|
|
11839
|
+
} else {
|
|
11840
|
+
x.e = v.e;
|
|
11841
|
+
x.d = v.d ? v.d.slice() : v.d;
|
|
11842
|
+
}
|
|
11843
|
+
return;
|
|
11844
|
+
}
|
|
11845
|
+
t = typeof v;
|
|
11846
|
+
if (t === "number") {
|
|
11847
|
+
if (v === 0) {
|
|
11848
|
+
x.s = 1 / v < 0 ? -1 : 1;
|
|
11849
|
+
x.e = 0;
|
|
11850
|
+
x.d = [0];
|
|
11851
|
+
return;
|
|
11852
|
+
}
|
|
11853
|
+
if (v < 0) {
|
|
11854
|
+
v = -v;
|
|
11855
|
+
x.s = -1;
|
|
11856
|
+
} else {
|
|
11857
|
+
x.s = 1;
|
|
11858
|
+
}
|
|
11859
|
+
if (v === ~~v && v < 1e7) {
|
|
11860
|
+
for (e = 0, i2 = v;i2 >= 10; i2 /= 10)
|
|
11861
|
+
e++;
|
|
11862
|
+
if (external2) {
|
|
11863
|
+
if (e > Decimal.maxE) {
|
|
11864
|
+
x.e = NaN;
|
|
11865
|
+
x.d = null;
|
|
11866
|
+
} else if (e < Decimal.minE) {
|
|
11867
|
+
x.e = 0;
|
|
11868
|
+
x.d = [0];
|
|
11869
|
+
} else {
|
|
11870
|
+
x.e = e;
|
|
11871
|
+
x.d = [v];
|
|
11872
|
+
}
|
|
11873
|
+
} else {
|
|
11874
|
+
x.e = e;
|
|
11875
|
+
x.d = [v];
|
|
11876
|
+
}
|
|
11877
|
+
return;
|
|
11878
|
+
}
|
|
11879
|
+
if (v * 0 !== 0) {
|
|
11880
|
+
if (!v)
|
|
11881
|
+
x.s = NaN;
|
|
11882
|
+
x.e = NaN;
|
|
11883
|
+
x.d = null;
|
|
11884
|
+
return;
|
|
11885
|
+
}
|
|
11886
|
+
return parseDecimal(x, v.toString());
|
|
11887
|
+
}
|
|
11888
|
+
if (t === "string") {
|
|
11889
|
+
if ((i2 = v.charCodeAt(0)) === 45) {
|
|
11890
|
+
v = v.slice(1);
|
|
11891
|
+
x.s = -1;
|
|
11892
|
+
} else {
|
|
11893
|
+
if (i2 === 43)
|
|
11894
|
+
v = v.slice(1);
|
|
11895
|
+
x.s = 1;
|
|
11896
|
+
}
|
|
11897
|
+
return isDecimal.test(v) ? parseDecimal(x, v) : parseOther(x, v);
|
|
11898
|
+
}
|
|
11899
|
+
if (t === "bigint") {
|
|
11900
|
+
if (v < 0) {
|
|
11901
|
+
v = -v;
|
|
11902
|
+
x.s = -1;
|
|
11903
|
+
} else {
|
|
11904
|
+
x.s = 1;
|
|
11905
|
+
}
|
|
11906
|
+
return parseDecimal(x, v.toString());
|
|
11907
|
+
}
|
|
11908
|
+
throw Error(invalidArgument + v);
|
|
11909
|
+
}
|
|
11910
|
+
Decimal.prototype = P;
|
|
11911
|
+
Decimal.ROUND_UP = 0;
|
|
11912
|
+
Decimal.ROUND_DOWN = 1;
|
|
11913
|
+
Decimal.ROUND_CEIL = 2;
|
|
11914
|
+
Decimal.ROUND_FLOOR = 3;
|
|
11915
|
+
Decimal.ROUND_HALF_UP = 4;
|
|
11916
|
+
Decimal.ROUND_HALF_DOWN = 5;
|
|
11917
|
+
Decimal.ROUND_HALF_EVEN = 6;
|
|
11918
|
+
Decimal.ROUND_HALF_CEIL = 7;
|
|
11919
|
+
Decimal.ROUND_HALF_FLOOR = 8;
|
|
11920
|
+
Decimal.EUCLID = 9;
|
|
11921
|
+
Decimal.config = Decimal.set = config;
|
|
11922
|
+
Decimal.clone = clone;
|
|
11923
|
+
Decimal.isDecimal = isDecimalInstance;
|
|
11924
|
+
Decimal.abs = abs;
|
|
11925
|
+
Decimal.acos = acos;
|
|
11926
|
+
Decimal.acosh = acosh;
|
|
11927
|
+
Decimal.add = add;
|
|
11928
|
+
Decimal.asin = asin;
|
|
11929
|
+
Decimal.asinh = asinh;
|
|
11930
|
+
Decimal.atan = atan;
|
|
11931
|
+
Decimal.atanh = atanh;
|
|
11932
|
+
Decimal.atan2 = atan2;
|
|
11933
|
+
Decimal.cbrt = cbrt;
|
|
11934
|
+
Decimal.ceil = ceil;
|
|
11935
|
+
Decimal.clamp = clamp;
|
|
11936
|
+
Decimal.cos = cos;
|
|
11937
|
+
Decimal.cosh = cosh;
|
|
11938
|
+
Decimal.div = div;
|
|
11939
|
+
Decimal.exp = exp;
|
|
11940
|
+
Decimal.floor = floor;
|
|
11941
|
+
Decimal.hypot = hypot;
|
|
11942
|
+
Decimal.ln = ln;
|
|
11943
|
+
Decimal.log = log;
|
|
11944
|
+
Decimal.log10 = log10;
|
|
11945
|
+
Decimal.log2 = log2;
|
|
11946
|
+
Decimal.max = max;
|
|
11947
|
+
Decimal.min = min;
|
|
11948
|
+
Decimal.mod = mod;
|
|
11949
|
+
Decimal.mul = mul;
|
|
11950
|
+
Decimal.pow = pow;
|
|
11951
|
+
Decimal.random = random;
|
|
11952
|
+
Decimal.round = round;
|
|
11953
|
+
Decimal.sign = sign;
|
|
11954
|
+
Decimal.sin = sin;
|
|
11955
|
+
Decimal.sinh = sinh;
|
|
11956
|
+
Decimal.sqrt = sqrt;
|
|
11957
|
+
Decimal.sub = sub;
|
|
11958
|
+
Decimal.sum = sum;
|
|
11959
|
+
Decimal.tan = tan;
|
|
11960
|
+
Decimal.tanh = tanh;
|
|
11961
|
+
Decimal.trunc = trunc;
|
|
11962
|
+
if (obj === undefined)
|
|
11963
|
+
obj = {};
|
|
11964
|
+
if (obj) {
|
|
11965
|
+
if (obj.defaults !== true) {
|
|
11966
|
+
ps = ["precision", "rounding", "toExpNeg", "toExpPos", "maxE", "minE", "modulo", "crypto"];
|
|
11967
|
+
for (i = 0;i < ps.length; )
|
|
11968
|
+
if (!obj.hasOwnProperty(p = ps[i++]))
|
|
11969
|
+
obj[p] = this[p];
|
|
11970
|
+
}
|
|
11971
|
+
}
|
|
11972
|
+
Decimal.config(obj);
|
|
11973
|
+
return Decimal;
|
|
11974
|
+
}
|
|
11975
|
+
function div(x, y) {
|
|
11976
|
+
return new this(x).div(y);
|
|
11977
|
+
}
|
|
11978
|
+
function exp(x) {
|
|
11979
|
+
return new this(x).exp();
|
|
11980
|
+
}
|
|
11981
|
+
function floor(x) {
|
|
11982
|
+
return finalise(x = new this(x), x.e + 1, 3);
|
|
11983
|
+
}
|
|
11984
|
+
function hypot() {
|
|
11985
|
+
var i, n, t = new this(0);
|
|
11986
|
+
external2 = false;
|
|
11987
|
+
for (i = 0;i < arguments.length; ) {
|
|
11988
|
+
n = new this(arguments[i++]);
|
|
11989
|
+
if (!n.d) {
|
|
11990
|
+
if (n.s) {
|
|
11991
|
+
external2 = true;
|
|
11992
|
+
return new this(1 / 0);
|
|
11993
|
+
}
|
|
11994
|
+
t = n;
|
|
11995
|
+
} else if (t.d) {
|
|
11996
|
+
t = t.plus(n.times(n));
|
|
11997
|
+
}
|
|
11998
|
+
}
|
|
11999
|
+
external2 = true;
|
|
12000
|
+
return t.sqrt();
|
|
12001
|
+
}
|
|
12002
|
+
function isDecimalInstance(obj) {
|
|
12003
|
+
return obj instanceof Decimal || obj && obj.toStringTag === tag || false;
|
|
12004
|
+
}
|
|
12005
|
+
function ln(x) {
|
|
12006
|
+
return new this(x).ln();
|
|
12007
|
+
}
|
|
12008
|
+
function log(x, y) {
|
|
12009
|
+
return new this(x).log(y);
|
|
12010
|
+
}
|
|
12011
|
+
function log2(x) {
|
|
12012
|
+
return new this(x).log(2);
|
|
12013
|
+
}
|
|
12014
|
+
function log10(x) {
|
|
12015
|
+
return new this(x).log(10);
|
|
12016
|
+
}
|
|
12017
|
+
function max() {
|
|
12018
|
+
return maxOrMin(this, arguments, -1);
|
|
12019
|
+
}
|
|
12020
|
+
function min() {
|
|
12021
|
+
return maxOrMin(this, arguments, 1);
|
|
12022
|
+
}
|
|
12023
|
+
function mod(x, y) {
|
|
12024
|
+
return new this(x).mod(y);
|
|
12025
|
+
}
|
|
12026
|
+
function mul(x, y) {
|
|
12027
|
+
return new this(x).mul(y);
|
|
12028
|
+
}
|
|
12029
|
+
function pow(x, y) {
|
|
12030
|
+
return new this(x).pow(y);
|
|
12031
|
+
}
|
|
12032
|
+
function random(sd) {
|
|
12033
|
+
var d, e, k, n, i = 0, r = new this(1), rd = [];
|
|
12034
|
+
if (sd === undefined)
|
|
12035
|
+
sd = this.precision;
|
|
12036
|
+
else
|
|
12037
|
+
checkInt32(sd, 1, MAX_DIGITS);
|
|
12038
|
+
k = Math.ceil(sd / LOG_BASE);
|
|
12039
|
+
if (!this.crypto) {
|
|
12040
|
+
for (;i < k; )
|
|
12041
|
+
rd[i++] = Math.random() * 1e7 | 0;
|
|
12042
|
+
} else if (crypto.getRandomValues) {
|
|
12043
|
+
d = crypto.getRandomValues(new Uint32Array(k));
|
|
12044
|
+
for (;i < k; ) {
|
|
12045
|
+
n = d[i];
|
|
12046
|
+
if (n >= 4290000000) {
|
|
12047
|
+
d[i] = crypto.getRandomValues(new Uint32Array(1))[0];
|
|
12048
|
+
} else {
|
|
12049
|
+
rd[i++] = n % 1e7;
|
|
12050
|
+
}
|
|
12051
|
+
}
|
|
12052
|
+
} else if (crypto.randomBytes) {
|
|
12053
|
+
d = crypto.randomBytes(k *= 4);
|
|
12054
|
+
for (;i < k; ) {
|
|
12055
|
+
n = d[i] + (d[i + 1] << 8) + (d[i + 2] << 16) + ((d[i + 3] & 127) << 24);
|
|
12056
|
+
if (n >= 2140000000) {
|
|
12057
|
+
crypto.randomBytes(4).copy(d, i);
|
|
12058
|
+
} else {
|
|
12059
|
+
rd.push(n % 1e7);
|
|
12060
|
+
i += 4;
|
|
12061
|
+
}
|
|
12062
|
+
}
|
|
12063
|
+
i = k / 4;
|
|
12064
|
+
} else {
|
|
12065
|
+
throw Error(cryptoUnavailable);
|
|
12066
|
+
}
|
|
12067
|
+
k = rd[--i];
|
|
12068
|
+
sd %= LOG_BASE;
|
|
12069
|
+
if (k && sd) {
|
|
12070
|
+
n = mathpow(10, LOG_BASE - sd);
|
|
12071
|
+
rd[i] = (k / n | 0) * n;
|
|
12072
|
+
}
|
|
12073
|
+
for (;rd[i] === 0; i--)
|
|
12074
|
+
rd.pop();
|
|
12075
|
+
if (i < 0) {
|
|
12076
|
+
e = 0;
|
|
12077
|
+
rd = [0];
|
|
12078
|
+
} else {
|
|
12079
|
+
e = -1;
|
|
12080
|
+
for (;rd[0] === 0; e -= LOG_BASE)
|
|
12081
|
+
rd.shift();
|
|
12082
|
+
for (k = 1, n = rd[0];n >= 10; n /= 10)
|
|
12083
|
+
k++;
|
|
12084
|
+
if (k < LOG_BASE)
|
|
12085
|
+
e -= LOG_BASE - k;
|
|
12086
|
+
}
|
|
12087
|
+
r.e = e;
|
|
12088
|
+
r.d = rd;
|
|
12089
|
+
return r;
|
|
12090
|
+
}
|
|
12091
|
+
function round(x) {
|
|
12092
|
+
return finalise(x = new this(x), x.e + 1, this.rounding);
|
|
12093
|
+
}
|
|
12094
|
+
function sign(x) {
|
|
12095
|
+
x = new this(x);
|
|
12096
|
+
return x.d ? x.d[0] ? x.s : 0 * x.s : x.s || NaN;
|
|
12097
|
+
}
|
|
12098
|
+
function sin(x) {
|
|
12099
|
+
return new this(x).sin();
|
|
12100
|
+
}
|
|
12101
|
+
function sinh(x) {
|
|
12102
|
+
return new this(x).sinh();
|
|
12103
|
+
}
|
|
12104
|
+
function sqrt(x) {
|
|
12105
|
+
return new this(x).sqrt();
|
|
12106
|
+
}
|
|
12107
|
+
function sub(x, y) {
|
|
12108
|
+
return new this(x).sub(y);
|
|
12109
|
+
}
|
|
12110
|
+
function sum() {
|
|
12111
|
+
var i = 0, args = arguments, x = new this(args[i]);
|
|
12112
|
+
external2 = false;
|
|
12113
|
+
for (;x.s && ++i < args.length; )
|
|
12114
|
+
x = x.plus(args[i]);
|
|
12115
|
+
external2 = true;
|
|
12116
|
+
return finalise(x, this.precision, this.rounding);
|
|
12117
|
+
}
|
|
12118
|
+
function tan(x) {
|
|
12119
|
+
return new this(x).tan();
|
|
12120
|
+
}
|
|
12121
|
+
function tanh(x) {
|
|
12122
|
+
return new this(x).tanh();
|
|
12123
|
+
}
|
|
12124
|
+
function trunc(x) {
|
|
12125
|
+
return finalise(x = new this(x), x.e + 1, 1);
|
|
12126
|
+
}
|
|
12127
|
+
P[Symbol.for("nodejs.util.inspect.custom")] = P.toString;
|
|
12128
|
+
P[Symbol.toStringTag] = "Decimal";
|
|
12129
|
+
var Decimal = P.constructor = clone(DEFAULTS);
|
|
12130
|
+
LN10 = new Decimal(LN10);
|
|
12131
|
+
PI = new Decimal(PI);
|
|
12132
|
+
var decimal_default = Decimal;
|
|
12133
|
+
|
|
12134
|
+
// src/common/base/other.ts
|
|
12135
|
+
var delay = (time = 1000) => {
|
|
12136
|
+
return new Promise((resolve) => setTimeout(resolve, time));
|
|
12137
|
+
};
|
|
12138
|
+
var calcJsText = (expr, context) => {
|
|
12139
|
+
const keys = Object.keys(context);
|
|
12140
|
+
const values = keys.map((key2) => context[key2]);
|
|
12141
|
+
return Function(...keys, `return (${expr})`)(...values);
|
|
12142
|
+
};
|
|
12143
|
+
var optionsToEnum = (options, text, key2) => {
|
|
12144
|
+
return options.reduce((acc, cur) => {
|
|
12145
|
+
acc[cur[key2]] = {
|
|
12146
|
+
text: cur[text]
|
|
12147
|
+
};
|
|
12148
|
+
return acc;
|
|
12149
|
+
}, {});
|
|
12150
|
+
};
|
|
12151
|
+
var performDecimalOperation = (num1, num2, operator) => {
|
|
12152
|
+
if (num1 === undefined || num2 === undefined || !operator) {
|
|
12153
|
+
return;
|
|
12154
|
+
}
|
|
12155
|
+
const decimalNum1 = new decimal_default(Number(num1));
|
|
12156
|
+
const decimalNum2 = new decimal_default(Number(num2));
|
|
12157
|
+
switch (operator) {
|
|
12158
|
+
case "+":
|
|
12159
|
+
return decimalNum1.plus(decimalNum2).toNumber();
|
|
12160
|
+
case "-":
|
|
12161
|
+
return decimalNum1.minus(decimalNum2).toNumber();
|
|
12162
|
+
case "*":
|
|
12163
|
+
return decimalNum1.times(decimalNum2).toNumber();
|
|
12164
|
+
case "/":
|
|
12165
|
+
if (num2 !== 0) {
|
|
12166
|
+
return decimalNum1.dividedBy(decimalNum2).toNumber();
|
|
12167
|
+
} else {
|
|
12168
|
+
console.error("Division by zero is not allowed.");
|
|
12169
|
+
return NaN;
|
|
12170
|
+
}
|
|
12171
|
+
default:
|
|
12172
|
+
console.error("Invalid operator:", operator);
|
|
12173
|
+
return NaN;
|
|
12174
|
+
}
|
|
12175
|
+
};
|
|
12176
|
+
var printConsoleLog = (type, path2, params, response) => {
|
|
12177
|
+
const styles = {
|
|
12178
|
+
header: "color: #fff; background: #35495e; padding: 2px 8px; border-radius: 3px 0 0 3px;",
|
|
12179
|
+
value: "color: #35495e; background: #f0f4f8; padding: 2px 6px; border-radius: 0 3px 3px 0;",
|
|
12180
|
+
separator: "color: #409EFF; margin: 0 4px;"
|
|
12181
|
+
};
|
|
12182
|
+
console.groupCollapsed(`666 %c${type}%c${path2}`, styles.header, styles.value);
|
|
12183
|
+
if (params) {
|
|
12184
|
+
console.table(params);
|
|
12185
|
+
}
|
|
12186
|
+
if (response) {
|
|
12187
|
+
console.table(response);
|
|
12188
|
+
}
|
|
12189
|
+
console.groupEnd();
|
|
12190
|
+
};
|
|
12191
|
+
|
|
12192
|
+
// src/common/base/array.ts
|
|
12193
|
+
var shuffleArray = (array) => {
|
|
12194
|
+
const shuffledArray = [...array];
|
|
12195
|
+
for (let i = shuffledArray.length - 1;i > 0; i--) {
|
|
12196
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
12197
|
+
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
|
|
12198
|
+
}
|
|
12199
|
+
return shuffledArray;
|
|
12200
|
+
};
|
|
12201
|
+
var removeSimilarDuplicates = (arr, threshold, isLog) => {
|
|
12202
|
+
const uniqueArray = [];
|
|
12203
|
+
const similarPairs = [];
|
|
12204
|
+
for (const str of arr) {
|
|
12205
|
+
const isSimilar = uniqueArray.some((uniqueStr) => {
|
|
12206
|
+
const distance = levenshteinDistance(str, uniqueStr);
|
|
12207
|
+
if (distance <= threshold) {
|
|
12208
|
+
similarPairs.push([str, uniqueStr]);
|
|
12209
|
+
return true;
|
|
12210
|
+
}
|
|
12211
|
+
return false;
|
|
9682
12212
|
});
|
|
9683
|
-
|
|
9684
|
-
|
|
12213
|
+
if (!isSimilar) {
|
|
12214
|
+
uniqueArray.push(str);
|
|
12215
|
+
}
|
|
12216
|
+
}
|
|
12217
|
+
if (isLog) {
|
|
12218
|
+
if (similarPairs.length > 0) {
|
|
12219
|
+
similarPairs.forEach((pair) => {
|
|
12220
|
+
console.log(pair[0]);
|
|
12221
|
+
console.log(pair[1]);
|
|
12222
|
+
console.log();
|
|
12223
|
+
});
|
|
12224
|
+
} else {
|
|
12225
|
+
console.log("没有找到类似的字符串");
|
|
12226
|
+
}
|
|
12227
|
+
}
|
|
12228
|
+
return uniqueArray;
|
|
12229
|
+
};
|
|
12230
|
+
var getTotal = (arr, key2) => {
|
|
12231
|
+
if (!Array.isArray(arr) || !key2) {
|
|
12232
|
+
return 0;
|
|
12233
|
+
}
|
|
12234
|
+
return arr.reduce((total, item) => {
|
|
12235
|
+
const itemValue = Number(item[key2]);
|
|
12236
|
+
if (!isNaN(itemValue)) {
|
|
12237
|
+
return performDecimalOperation(total, itemValue, "+");
|
|
12238
|
+
}
|
|
12239
|
+
return total;
|
|
12240
|
+
}, 0);
|
|
12241
|
+
};
|
|
12242
|
+
|
|
12243
|
+
// src/common/base/number.ts
|
|
12244
|
+
var getRandomNum = (min2, max2) => {
|
|
12245
|
+
return Math.floor(Math.random() * (max2 - min2 + 1) + min2);
|
|
12246
|
+
};
|
|
12247
|
+
var limitDecimals = (v, num = 2, isForce) => {
|
|
12248
|
+
let value = parseFloat(v);
|
|
12249
|
+
if (isNaN(value)) {
|
|
12250
|
+
if (isForce) {
|
|
12251
|
+
value = 0;
|
|
12252
|
+
} else {
|
|
12253
|
+
return "";
|
|
12254
|
+
}
|
|
12255
|
+
}
|
|
12256
|
+
return value.toFixed(num).toString();
|
|
12257
|
+
};
|
|
12258
|
+
var getFileSize = (size) => {
|
|
12259
|
+
let d = "G";
|
|
12260
|
+
let s = size / 1024 / 1024 / 1024;
|
|
12261
|
+
if (s < 1) {
|
|
12262
|
+
d = "M";
|
|
12263
|
+
s *= 1024;
|
|
12264
|
+
}
|
|
12265
|
+
return `${(s.toFixed(1) / 1).toString()}${d}`;
|
|
12266
|
+
};
|
|
12267
|
+
var isValidNumber = (value) => {
|
|
12268
|
+
if (typeof value !== "number") {
|
|
12269
|
+
return false;
|
|
12270
|
+
}
|
|
12271
|
+
if (isNaN(value)) {
|
|
12272
|
+
return false;
|
|
12273
|
+
}
|
|
12274
|
+
if (value === Infinity || value === -Infinity) {
|
|
12275
|
+
return false;
|
|
12276
|
+
}
|
|
12277
|
+
return true;
|
|
12278
|
+
};
|
|
12279
|
+
var numberWithCommas = (x) => {
|
|
12280
|
+
if (x === undefined || x === null) {
|
|
12281
|
+
return "";
|
|
12282
|
+
}
|
|
12283
|
+
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
12284
|
+
};
|
|
12285
|
+
|
|
12286
|
+
// src/common/base/object.ts
|
|
12287
|
+
var findItem = (list, attr, value) => {
|
|
12288
|
+
return list.find((item) => value === undefined ? item[attr] : item[attr] === value);
|
|
12289
|
+
};
|
|
12290
|
+
var filterParams = (params, deep = false) => {
|
|
12291
|
+
const clean = (value) => {
|
|
12292
|
+
if (value === undefined || value === null) {
|
|
12293
|
+
return;
|
|
12294
|
+
}
|
|
12295
|
+
if (!deep) {
|
|
12296
|
+
return value;
|
|
12297
|
+
}
|
|
12298
|
+
if (Array.isArray(value)) {
|
|
12299
|
+
return value.map((v) => clean(v)).filter((v) => v !== undefined);
|
|
12300
|
+
}
|
|
12301
|
+
if (typeof value === "object") {
|
|
12302
|
+
return Object.entries(value).reduce((acc, [k, v]) => {
|
|
12303
|
+
const cleaned = clean(v);
|
|
12304
|
+
if (cleaned !== undefined)
|
|
12305
|
+
acc[k] = cleaned;
|
|
12306
|
+
return acc;
|
|
12307
|
+
}, {});
|
|
12308
|
+
}
|
|
12309
|
+
return value;
|
|
12310
|
+
};
|
|
12311
|
+
return clean(params);
|
|
12312
|
+
};
|
|
12313
|
+
var watch = {
|
|
12314
|
+
observe(obj, key2, watchFun) {
|
|
12315
|
+
const val = obj[key2];
|
|
12316
|
+
Object.defineProperty(obj, key2, {
|
|
12317
|
+
configurable: true,
|
|
12318
|
+
enumerable: true,
|
|
12319
|
+
set(value) {
|
|
12320
|
+
obj[key2] = value;
|
|
12321
|
+
watchFun(value, val);
|
|
12322
|
+
},
|
|
12323
|
+
get() {
|
|
12324
|
+
return val;
|
|
12325
|
+
}
|
|
12326
|
+
});
|
|
12327
|
+
},
|
|
12328
|
+
setWatcher(data = {}, watch2 = {}) {
|
|
12329
|
+
Object.keys(watch2).forEach((v) => {
|
|
12330
|
+
this.observe(data, v, watch2[v]);
|
|
12331
|
+
});
|
|
12332
|
+
}
|
|
12333
|
+
};
|
|
12334
|
+
|
|
12335
|
+
// src/common/base/promise.ts
|
|
12336
|
+
var executePromise = async (promise) => {
|
|
12337
|
+
const start = Date.now();
|
|
12338
|
+
const result = await promise;
|
|
12339
|
+
const time = Date.now() - start;
|
|
12340
|
+
return {
|
|
12341
|
+
result,
|
|
12342
|
+
time
|
|
12343
|
+
};
|
|
12344
|
+
};
|
|
12345
|
+
var retryPromise = async (promiseFn, retryInterval = 1000, retryCount = 3) => {
|
|
12346
|
+
try {
|
|
12347
|
+
return await promiseFn();
|
|
9685
12348
|
} catch (error) {
|
|
9686
|
-
|
|
9687
|
-
|
|
12349
|
+
if (retryCount <= 0) {
|
|
12350
|
+
throw error;
|
|
12351
|
+
}
|
|
12352
|
+
await new Promise((resolve) => setTimeout(resolve, retryInterval));
|
|
12353
|
+
return retryPromise(promiseFn, retryCount - 1, retryInterval);
|
|
9688
12354
|
}
|
|
9689
|
-
}
|
|
9690
|
-
// src/common/enum.ts
|
|
12355
|
+
};
|
|
12356
|
+
// src/common/enum/index.ts
|
|
9691
12357
|
class EnumItem {
|
|
9692
12358
|
label;
|
|
9693
12359
|
value;
|
|
9694
12360
|
extra;
|
|
9695
|
-
constructor(
|
|
9696
|
-
this.value =
|
|
9697
|
-
this.label =
|
|
9698
|
-
this.extra =
|
|
12361
|
+
constructor(config2) {
|
|
12362
|
+
this.value = config2.value;
|
|
12363
|
+
this.label = config2.label;
|
|
12364
|
+
this.extra = config2.extra;
|
|
9699
12365
|
}
|
|
9700
12366
|
}
|
|
9701
12367
|
var Enum = {
|
|
@@ -9734,7 +12400,21 @@ var Enum = {
|
|
|
9734
12400
|
return enumObj;
|
|
9735
12401
|
}
|
|
9736
12402
|
};
|
|
9737
|
-
// src/common/
|
|
12403
|
+
// src/common/image/index.ts
|
|
12404
|
+
import axios3 from "axios";
|
|
12405
|
+
async function imageToBase64(url2) {
|
|
12406
|
+
try {
|
|
12407
|
+
const response = await axios3.get(url2, {
|
|
12408
|
+
responseType: "arraybuffer"
|
|
12409
|
+
});
|
|
12410
|
+
const base64String = Buffer.from(response.data).toString("base64");
|
|
12411
|
+
return `data:image/*;base64,${base64String}`;
|
|
12412
|
+
} catch (error) {
|
|
12413
|
+
console.error("Failed to fetch the image:", error);
|
|
12414
|
+
throw error;
|
|
12415
|
+
}
|
|
12416
|
+
}
|
|
12417
|
+
// src/common/dayjs/index.ts
|
|
9738
12418
|
import dayjs from "dayjs";
|
|
9739
12419
|
import isBetween from "dayjs/plugin/isBetween";
|
|
9740
12420
|
import weekday from "dayjs/plugin/weekday";
|
|
@@ -9792,10 +12472,10 @@ var transporter = nodemailer.createTransport({
|
|
|
9792
12472
|
secure: true,
|
|
9793
12473
|
auth: authMap.gmail
|
|
9794
12474
|
});
|
|
9795
|
-
var sendMail = (
|
|
12475
|
+
var sendMail = (config2) => {
|
|
9796
12476
|
return transporter.sendMail({
|
|
9797
12477
|
from: authMap.gmail.user,
|
|
9798
|
-
...
|
|
12478
|
+
...config2
|
|
9799
12479
|
});
|
|
9800
12480
|
};
|
|
9801
12481
|
// src/node/file.ts
|
|
@@ -9932,6 +12612,7 @@ var export__ = import_lodash.default;
|
|
|
9932
12612
|
|
|
9933
12613
|
export {
|
|
9934
12614
|
exports_external as z,
|
|
12615
|
+
watch,
|
|
9935
12616
|
voidType as void,
|
|
9936
12617
|
util,
|
|
9937
12618
|
uploadFile,
|
|
@@ -9940,26 +12621,37 @@ export {
|
|
|
9940
12621
|
undefinedType as undefined,
|
|
9941
12622
|
tupleType as tuple,
|
|
9942
12623
|
effectsType as transformer,
|
|
12624
|
+
toString,
|
|
9943
12625
|
symbolType as symbol,
|
|
9944
12626
|
stringType as string,
|
|
9945
12627
|
strictObjectType as strictObject,
|
|
12628
|
+
shuffleArray,
|
|
9946
12629
|
setErrorMap,
|
|
9947
12630
|
setCookie,
|
|
9948
12631
|
setType as set,
|
|
9949
12632
|
sendMail,
|
|
9950
12633
|
schedule,
|
|
12634
|
+
retryPromise,
|
|
9951
12635
|
replaceContentInFile,
|
|
12636
|
+
replaceByVariables,
|
|
12637
|
+
replaceByRules,
|
|
12638
|
+
replaceAll,
|
|
12639
|
+
removeSimilarDuplicates,
|
|
9952
12640
|
recordType as record,
|
|
9953
12641
|
quotelessJson,
|
|
9954
12642
|
promiseType as promise,
|
|
12643
|
+
printConsoleLog,
|
|
9955
12644
|
preprocessType as preprocess,
|
|
9956
12645
|
pipelineType as pipeline,
|
|
12646
|
+
performDecimalOperation,
|
|
9957
12647
|
ostring,
|
|
12648
|
+
optionsToEnum,
|
|
9958
12649
|
optionalType as optional,
|
|
9959
12650
|
onumber,
|
|
9960
12651
|
oboolean,
|
|
9961
12652
|
objectUtil,
|
|
9962
12653
|
objectType as object,
|
|
12654
|
+
numberWithCommas,
|
|
9963
12655
|
numberType as number,
|
|
9964
12656
|
nullableType as nullable,
|
|
9965
12657
|
nullType as null,
|
|
@@ -9969,10 +12661,15 @@ export {
|
|
|
9969
12661
|
mapType as map,
|
|
9970
12662
|
makeIssue,
|
|
9971
12663
|
literalType as literal,
|
|
12664
|
+
limitDecimals,
|
|
12665
|
+
levenshteinDistance,
|
|
9972
12666
|
lazyType as lazy,
|
|
9973
12667
|
late,
|
|
9974
|
-
|
|
12668
|
+
jsonParse,
|
|
12669
|
+
default4 as json5,
|
|
12670
|
+
isValidNumber,
|
|
9975
12671
|
isValid,
|
|
12672
|
+
isJson,
|
|
9976
12673
|
isDirty,
|
|
9977
12674
|
isAsync,
|
|
9978
12675
|
isAborted,
|
|
@@ -9982,29 +12679,50 @@ export {
|
|
|
9982
12679
|
initChinaDayjs,
|
|
9983
12680
|
imageToBase64,
|
|
9984
12681
|
getVideoStatus,
|
|
12682
|
+
getType,
|
|
12683
|
+
getTotal,
|
|
12684
|
+
getStrLength,
|
|
12685
|
+
getSliceStr,
|
|
12686
|
+
getRandomString,
|
|
12687
|
+
getRandomNum,
|
|
12688
|
+
getRandomColor,
|
|
12689
|
+
getProxyUrl,
|
|
9985
12690
|
getParsedType,
|
|
12691
|
+
getLength,
|
|
12692
|
+
getFileSize,
|
|
9986
12693
|
getErrorMap,
|
|
12694
|
+
getCookie,
|
|
12695
|
+
getChineseByStr,
|
|
12696
|
+
getChinese,
|
|
9987
12697
|
generateVideo,
|
|
12698
|
+
generateUniqueFileName,
|
|
12699
|
+
generateAlphabetArray,
|
|
9988
12700
|
functionType as function,
|
|
12701
|
+
findItem,
|
|
12702
|
+
filterParams,
|
|
12703
|
+
executePromise,
|
|
9989
12704
|
enumType as enum,
|
|
9990
12705
|
effectsType as effect,
|
|
9991
12706
|
downloadFile,
|
|
9992
12707
|
discriminatedUnionType as discriminatedUnion,
|
|
12708
|
+
delay,
|
|
9993
12709
|
en_default as defaultErrorMap,
|
|
9994
12710
|
dayjs_default as dayjs,
|
|
9995
12711
|
datetimeRegex,
|
|
9996
12712
|
dateType as date,
|
|
9997
12713
|
custom,
|
|
9998
|
-
|
|
12714
|
+
default5 as consola,
|
|
9999
12715
|
coerce,
|
|
10000
12716
|
chinaDayjs,
|
|
10001
12717
|
cheerio,
|
|
10002
12718
|
chat,
|
|
12719
|
+
calcJsText,
|
|
10003
12720
|
booleanType as boolean,
|
|
10004
12721
|
bigIntType as bigint,
|
|
10005
|
-
|
|
12722
|
+
default3 as axios,
|
|
10006
12723
|
arrayType as array,
|
|
10007
12724
|
anyType as any,
|
|
12725
|
+
amount,
|
|
10008
12726
|
addIssueToContext,
|
|
10009
12727
|
export__ as _,
|
|
10010
12728
|
ZodVoid,
|
|
@@ -10060,7 +12778,7 @@ export {
|
|
|
10060
12778
|
EMPTY_PATH,
|
|
10061
12779
|
Dayjs,
|
|
10062
12780
|
DataStoreOptions,
|
|
10063
|
-
|
|
12781
|
+
default6 as DataStore,
|
|
10064
12782
|
DIRTY,
|
|
10065
12783
|
BRAND
|
|
10066
12784
|
};
|