feat:node-modules
This commit is contained in:
399
node_modules/mathjs/lib/esm/utils/bignumber/bitwise.js
generated
vendored
Normal file
399
node_modules/mathjs/lib/esm/utils/bignumber/bitwise.js
generated
vendored
Normal file
@@ -0,0 +1,399 @@
|
||||
/**
|
||||
* Bitwise and for Bignumbers
|
||||
*
|
||||
* Special Cases:
|
||||
* N & n = N
|
||||
* n & 0 = 0
|
||||
* n & -1 = n
|
||||
* n & n = n
|
||||
* I & I = I
|
||||
* -I & -I = -I
|
||||
* I & -I = 0
|
||||
* I & n = n
|
||||
* I & -n = I
|
||||
* -I & n = 0
|
||||
* -I & -n = -I
|
||||
*
|
||||
* @param {BigNumber} x
|
||||
* @param {BigNumber} y
|
||||
* @return {BigNumber} Result of `x` & `y`, is fully precise
|
||||
* @private
|
||||
*/
|
||||
export function bitAndBigNumber(x, y) {
|
||||
if (x.isFinite() && !x.isInteger() || y.isFinite() && !y.isInteger()) {
|
||||
throw new Error('Integers expected in function bitAnd');
|
||||
}
|
||||
var BigNumber = x.constructor;
|
||||
if (x.isNaN() || y.isNaN()) {
|
||||
return new BigNumber(NaN);
|
||||
}
|
||||
if (x.isZero() || y.eq(-1) || x.eq(y)) {
|
||||
return x;
|
||||
}
|
||||
if (y.isZero() || x.eq(-1)) {
|
||||
return y;
|
||||
}
|
||||
if (!x.isFinite() || !y.isFinite()) {
|
||||
if (!x.isFinite() && !y.isFinite()) {
|
||||
if (x.isNegative() === y.isNegative()) {
|
||||
return x;
|
||||
}
|
||||
return new BigNumber(0);
|
||||
}
|
||||
if (!x.isFinite()) {
|
||||
if (y.isNegative()) {
|
||||
return x;
|
||||
}
|
||||
if (x.isNegative()) {
|
||||
return new BigNumber(0);
|
||||
}
|
||||
return y;
|
||||
}
|
||||
if (!y.isFinite()) {
|
||||
if (x.isNegative()) {
|
||||
return y;
|
||||
}
|
||||
if (y.isNegative()) {
|
||||
return new BigNumber(0);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
}
|
||||
return bitwise(x, y, function (a, b) {
|
||||
return a & b;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Bitwise not
|
||||
* @param {BigNumber} x
|
||||
* @return {BigNumber} Result of ~`x`, fully precise
|
||||
*
|
||||
*/
|
||||
export function bitNotBigNumber(x) {
|
||||
if (x.isFinite() && !x.isInteger()) {
|
||||
throw new Error('Integer expected in function bitNot');
|
||||
}
|
||||
var BigNumber = x.constructor;
|
||||
var prevPrec = BigNumber.precision;
|
||||
BigNumber.config({
|
||||
precision: 1E9
|
||||
});
|
||||
var result = x.plus(new BigNumber(1));
|
||||
result.s = -result.s || null;
|
||||
BigNumber.config({
|
||||
precision: prevPrec
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bitwise OR for BigNumbers
|
||||
*
|
||||
* Special Cases:
|
||||
* N | n = N
|
||||
* n | 0 = n
|
||||
* n | -1 = -1
|
||||
* n | n = n
|
||||
* I | I = I
|
||||
* -I | -I = -I
|
||||
* I | -n = -1
|
||||
* I | -I = -1
|
||||
* I | n = I
|
||||
* -I | n = -I
|
||||
* -I | -n = -n
|
||||
*
|
||||
* @param {BigNumber} x
|
||||
* @param {BigNumber} y
|
||||
* @return {BigNumber} Result of `x` | `y`, fully precise
|
||||
*/
|
||||
export function bitOrBigNumber(x, y) {
|
||||
if (x.isFinite() && !x.isInteger() || y.isFinite() && !y.isInteger()) {
|
||||
throw new Error('Integers expected in function bitOr');
|
||||
}
|
||||
var BigNumber = x.constructor;
|
||||
if (x.isNaN() || y.isNaN()) {
|
||||
return new BigNumber(NaN);
|
||||
}
|
||||
var negOne = new BigNumber(-1);
|
||||
if (x.isZero() || y.eq(negOne) || x.eq(y)) {
|
||||
return y;
|
||||
}
|
||||
if (y.isZero() || x.eq(negOne)) {
|
||||
return x;
|
||||
}
|
||||
if (!x.isFinite() || !y.isFinite()) {
|
||||
if (!x.isFinite() && !x.isNegative() && y.isNegative() || x.isNegative() && !y.isNegative() && !y.isFinite()) {
|
||||
return negOne;
|
||||
}
|
||||
if (x.isNegative() && y.isNegative()) {
|
||||
return x.isFinite() ? x : y;
|
||||
}
|
||||
return x.isFinite() ? y : x;
|
||||
}
|
||||
return bitwise(x, y, function (a, b) {
|
||||
return a | b;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies bitwise function to numbers
|
||||
* @param {BigNumber} x
|
||||
* @param {BigNumber} y
|
||||
* @param {function (a, b)} func
|
||||
* @return {BigNumber}
|
||||
*/
|
||||
export function bitwise(x, y, func) {
|
||||
var BigNumber = x.constructor;
|
||||
var xBits, yBits;
|
||||
var xSign = +(x.s < 0);
|
||||
var ySign = +(y.s < 0);
|
||||
if (xSign) {
|
||||
xBits = decCoefficientToBinaryString(bitNotBigNumber(x));
|
||||
for (var i = 0; i < xBits.length; ++i) {
|
||||
xBits[i] ^= 1;
|
||||
}
|
||||
} else {
|
||||
xBits = decCoefficientToBinaryString(x);
|
||||
}
|
||||
if (ySign) {
|
||||
yBits = decCoefficientToBinaryString(bitNotBigNumber(y));
|
||||
for (var _i = 0; _i < yBits.length; ++_i) {
|
||||
yBits[_i] ^= 1;
|
||||
}
|
||||
} else {
|
||||
yBits = decCoefficientToBinaryString(y);
|
||||
}
|
||||
var minBits, maxBits, minSign;
|
||||
if (xBits.length <= yBits.length) {
|
||||
minBits = xBits;
|
||||
maxBits = yBits;
|
||||
minSign = xSign;
|
||||
} else {
|
||||
minBits = yBits;
|
||||
maxBits = xBits;
|
||||
minSign = ySign;
|
||||
}
|
||||
var shortLen = minBits.length;
|
||||
var longLen = maxBits.length;
|
||||
var expFuncVal = func(xSign, ySign) ^ 1;
|
||||
var outVal = new BigNumber(expFuncVal ^ 1);
|
||||
var twoPower = new BigNumber(1);
|
||||
var two = new BigNumber(2);
|
||||
var prevPrec = BigNumber.precision;
|
||||
BigNumber.config({
|
||||
precision: 1E9
|
||||
});
|
||||
while (shortLen > 0) {
|
||||
if (func(minBits[--shortLen], maxBits[--longLen]) === expFuncVal) {
|
||||
outVal = outVal.plus(twoPower);
|
||||
}
|
||||
twoPower = twoPower.times(two);
|
||||
}
|
||||
while (longLen > 0) {
|
||||
if (func(minSign, maxBits[--longLen]) === expFuncVal) {
|
||||
outVal = outVal.plus(twoPower);
|
||||
}
|
||||
twoPower = twoPower.times(two);
|
||||
}
|
||||
BigNumber.config({
|
||||
precision: prevPrec
|
||||
});
|
||||
if (expFuncVal === 0) {
|
||||
outVal.s = -outVal.s;
|
||||
}
|
||||
return outVal;
|
||||
}
|
||||
|
||||
/* Extracted from decimal.js, and edited to specialize. */
|
||||
function decCoefficientToBinaryString(x) {
|
||||
// Convert to string
|
||||
var a = x.d; // array with digits
|
||||
var r = a[0] + '';
|
||||
for (var i = 1; i < a.length; ++i) {
|
||||
var s = a[i] + '';
|
||||
for (var z = 7 - s.length; z--;) {
|
||||
s = '0' + s;
|
||||
}
|
||||
r += s;
|
||||
}
|
||||
var j = r.length;
|
||||
while (r.charAt(j) === '0') {
|
||||
j--;
|
||||
}
|
||||
var xe = x.e;
|
||||
var str = r.slice(0, j + 1 || 1);
|
||||
var strL = str.length;
|
||||
if (xe > 0) {
|
||||
if (++xe > strL) {
|
||||
// Append zeros.
|
||||
xe -= strL;
|
||||
while (xe--) {
|
||||
str += '0';
|
||||
}
|
||||
} else if (xe < strL) {
|
||||
str = str.slice(0, xe) + '.' + str.slice(xe);
|
||||
}
|
||||
}
|
||||
|
||||
// Convert from base 10 (decimal) to base 2
|
||||
var arr = [0];
|
||||
for (var _i2 = 0; _i2 < str.length;) {
|
||||
var arrL = arr.length;
|
||||
while (arrL--) {
|
||||
arr[arrL] *= 10;
|
||||
}
|
||||
arr[0] += parseInt(str.charAt(_i2++)); // convert to int
|
||||
for (var _j = 0; _j < arr.length; ++_j) {
|
||||
if (arr[_j] > 1) {
|
||||
if (arr[_j + 1] === null || arr[_j + 1] === undefined) {
|
||||
arr[_j + 1] = 0;
|
||||
}
|
||||
arr[_j + 1] += arr[_j] >> 1;
|
||||
arr[_j] &= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return arr.reverse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Bitwise XOR for BigNumbers
|
||||
*
|
||||
* Special Cases:
|
||||
* N ^ n = N
|
||||
* n ^ 0 = n
|
||||
* n ^ n = 0
|
||||
* n ^ -1 = ~n
|
||||
* I ^ n = I
|
||||
* I ^ -n = -I
|
||||
* I ^ -I = -1
|
||||
* -I ^ n = -I
|
||||
* -I ^ -n = I
|
||||
*
|
||||
* @param {BigNumber} x
|
||||
* @param {BigNumber} y
|
||||
* @return {BigNumber} Result of `x` ^ `y`, fully precise
|
||||
*
|
||||
*/
|
||||
export function bitXor(x, y) {
|
||||
if (x.isFinite() && !x.isInteger() || y.isFinite() && !y.isInteger()) {
|
||||
throw new Error('Integers expected in function bitXor');
|
||||
}
|
||||
var BigNumber = x.constructor;
|
||||
if (x.isNaN() || y.isNaN()) {
|
||||
return new BigNumber(NaN);
|
||||
}
|
||||
if (x.isZero()) {
|
||||
return y;
|
||||
}
|
||||
if (y.isZero()) {
|
||||
return x;
|
||||
}
|
||||
if (x.eq(y)) {
|
||||
return new BigNumber(0);
|
||||
}
|
||||
var negOne = new BigNumber(-1);
|
||||
if (x.eq(negOne)) {
|
||||
return bitNotBigNumber(y);
|
||||
}
|
||||
if (y.eq(negOne)) {
|
||||
return bitNotBigNumber(x);
|
||||
}
|
||||
if (!x.isFinite() || !y.isFinite()) {
|
||||
if (!x.isFinite() && !y.isFinite()) {
|
||||
return negOne;
|
||||
}
|
||||
return new BigNumber(x.isNegative() === y.isNegative() ? Infinity : -Infinity);
|
||||
}
|
||||
return bitwise(x, y, function (a, b) {
|
||||
return a ^ b;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Bitwise left shift
|
||||
*
|
||||
* Special Cases:
|
||||
* n << -n = N
|
||||
* n << N = N
|
||||
* N << n = N
|
||||
* n << 0 = n
|
||||
* 0 << n = 0
|
||||
* I << I = N
|
||||
* I << n = I
|
||||
* n << I = I
|
||||
*
|
||||
* @param {BigNumber} x
|
||||
* @param {BigNumber} y
|
||||
* @return {BigNumber} Result of `x` << `y`
|
||||
*
|
||||
*/
|
||||
export function leftShiftBigNumber(x, y) {
|
||||
if (x.isFinite() && !x.isInteger() || y.isFinite() && !y.isInteger()) {
|
||||
throw new Error('Integers expected in function leftShift');
|
||||
}
|
||||
var BigNumber = x.constructor;
|
||||
if (x.isNaN() || y.isNaN() || y.isNegative() && !y.isZero()) {
|
||||
return new BigNumber(NaN);
|
||||
}
|
||||
if (x.isZero() || y.isZero()) {
|
||||
return x;
|
||||
}
|
||||
if (!x.isFinite() && !y.isFinite()) {
|
||||
return new BigNumber(NaN);
|
||||
}
|
||||
|
||||
// Math.pow(2, y) is fully precise for y < 55, and fast
|
||||
if (y.lt(55)) {
|
||||
return x.times(Math.pow(2, y.toNumber()) + '');
|
||||
}
|
||||
return x.times(new BigNumber(2).pow(y));
|
||||
}
|
||||
|
||||
/*
|
||||
* Special Cases:
|
||||
* n >> -n = N
|
||||
* n >> N = N
|
||||
* N >> n = N
|
||||
* I >> I = N
|
||||
* n >> 0 = n
|
||||
* I >> n = I
|
||||
* -I >> n = -I
|
||||
* -I >> I = -I
|
||||
* n >> I = I
|
||||
* -n >> I = -1
|
||||
* 0 >> n = 0
|
||||
*
|
||||
* @param {BigNumber} value
|
||||
* @param {BigNumber} value
|
||||
* @return {BigNumber} Result of `x` >> `y`
|
||||
*
|
||||
*/
|
||||
export function rightArithShiftBigNumber(x, y) {
|
||||
if (x.isFinite() && !x.isInteger() || y.isFinite() && !y.isInteger()) {
|
||||
throw new Error('Integers expected in function rightArithShift');
|
||||
}
|
||||
var BigNumber = x.constructor;
|
||||
if (x.isNaN() || y.isNaN() || y.isNegative() && !y.isZero()) {
|
||||
return new BigNumber(NaN);
|
||||
}
|
||||
if (x.isZero() || y.isZero()) {
|
||||
return x;
|
||||
}
|
||||
if (!y.isFinite()) {
|
||||
if (x.isNegative()) {
|
||||
return new BigNumber(-1);
|
||||
}
|
||||
if (!x.isFinite()) {
|
||||
return new BigNumber(NaN);
|
||||
}
|
||||
return new BigNumber(0);
|
||||
}
|
||||
|
||||
// Math.pow(2, y) is fully precise for y < 55, and fast
|
||||
if (y.lt(55)) {
|
||||
return x.div(Math.pow(2, y.toNumber()) + '').floor();
|
||||
}
|
||||
return x.div(new BigNumber(2).pow(y)).floor();
|
||||
}
|
||||
57
node_modules/mathjs/lib/esm/utils/bignumber/constants.js
generated
vendored
Normal file
57
node_modules/mathjs/lib/esm/utils/bignumber/constants.js
generated
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
import { memoize } from '../function.js';
|
||||
|
||||
/**
|
||||
* Calculate BigNumber e
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @returns {BigNumber} Returns e
|
||||
*/
|
||||
export var createBigNumberE = memoize(function (BigNumber) {
|
||||
return new BigNumber(1).exp();
|
||||
}, {
|
||||
hasher
|
||||
});
|
||||
|
||||
/**
|
||||
* Calculate BigNumber golden ratio, phi = (1+sqrt(5))/2
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @returns {BigNumber} Returns phi
|
||||
*/
|
||||
export var createBigNumberPhi = memoize(function (BigNumber) {
|
||||
return new BigNumber(1).plus(new BigNumber(5).sqrt()).div(2);
|
||||
}, {
|
||||
hasher
|
||||
});
|
||||
|
||||
/**
|
||||
* Calculate BigNumber pi.
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @returns {BigNumber} Returns pi
|
||||
*/
|
||||
export var createBigNumberPi = memoize(function (BigNumber) {
|
||||
return BigNumber.acos(-1);
|
||||
}, {
|
||||
hasher
|
||||
});
|
||||
|
||||
/**
|
||||
* Calculate BigNumber tau, tau = 2 * pi
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @returns {BigNumber} Returns tau
|
||||
*/
|
||||
export var createBigNumberTau = memoize(function (BigNumber) {
|
||||
return createBigNumberPi(BigNumber).times(2);
|
||||
}, {
|
||||
hasher
|
||||
});
|
||||
|
||||
/**
|
||||
* Create a hash for a BigNumber constructor function. The created has is
|
||||
* the configured precision
|
||||
* @param {Array} args Supposed to contain a single entry with
|
||||
* a BigNumber constructor
|
||||
* @return {number} precision
|
||||
* @private
|
||||
*/
|
||||
function hasher(args) {
|
||||
return args[0].precision;
|
||||
}
|
||||
243
node_modules/mathjs/lib/esm/utils/bignumber/formatter.js
generated
vendored
Normal file
243
node_modules/mathjs/lib/esm/utils/bignumber/formatter.js
generated
vendored
Normal file
@@ -0,0 +1,243 @@
|
||||
import { isBigNumber, isNumber } from '../is.js';
|
||||
import { isInteger, normalizeFormatOptions } from '../number.js';
|
||||
|
||||
/**
|
||||
* Formats a BigNumber in a given base
|
||||
* @param {BigNumber} n
|
||||
* @param {number} base
|
||||
* @param {number} size
|
||||
* @returns {string}
|
||||
*/
|
||||
function formatBigNumberToBase(n, base, size) {
|
||||
var BigNumberCtor = n.constructor;
|
||||
var big2 = new BigNumberCtor(2);
|
||||
var suffix = '';
|
||||
if (size) {
|
||||
if (size < 1) {
|
||||
throw new Error('size must be in greater than 0');
|
||||
}
|
||||
if (!isInteger(size)) {
|
||||
throw new Error('size must be an integer');
|
||||
}
|
||||
if (n.greaterThan(big2.pow(size - 1).sub(1)) || n.lessThan(big2.pow(size - 1).mul(-1))) {
|
||||
throw new Error("Value must be in range [-2^".concat(size - 1, ", 2^").concat(size - 1, "-1]"));
|
||||
}
|
||||
if (!n.isInteger()) {
|
||||
throw new Error('Value must be an integer');
|
||||
}
|
||||
if (n.lessThan(0)) {
|
||||
n = n.add(big2.pow(size));
|
||||
}
|
||||
suffix = "i".concat(size);
|
||||
}
|
||||
switch (base) {
|
||||
case 2:
|
||||
return "".concat(n.toBinary()).concat(suffix);
|
||||
case 8:
|
||||
return "".concat(n.toOctal()).concat(suffix);
|
||||
case 16:
|
||||
return "".concat(n.toHexadecimal()).concat(suffix);
|
||||
default:
|
||||
throw new Error("Base ".concat(base, " not supported "));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a BigNumber to a formatted string representation.
|
||||
*
|
||||
* Syntax:
|
||||
*
|
||||
* format(value)
|
||||
* format(value, options)
|
||||
* format(value, precision)
|
||||
* format(value, fn)
|
||||
*
|
||||
* Where:
|
||||
*
|
||||
* {number} value The value to be formatted
|
||||
* {Object} options An object with formatting options. Available options:
|
||||
* {string} notation
|
||||
* Number notation. Choose from:
|
||||
* 'fixed' Always use regular number notation.
|
||||
* For example '123.40' and '14000000'
|
||||
* 'exponential' Always use exponential notation.
|
||||
* For example '1.234e+2' and '1.4e+7'
|
||||
* 'auto' (default) Regular number notation for numbers
|
||||
* having an absolute value between
|
||||
* `lower` and `upper` bounds, and uses
|
||||
* exponential notation elsewhere.
|
||||
* Lower bound is included, upper bound
|
||||
* is excluded.
|
||||
* For example '123.4' and '1.4e7'.
|
||||
* 'bin', 'oct, or
|
||||
* 'hex' Format the number using binary, octal,
|
||||
* or hexadecimal notation.
|
||||
* For example '0b1101' and '0x10fe'.
|
||||
* {number} wordSize The word size in bits to use for formatting
|
||||
* in binary, octal, or hexadecimal notation.
|
||||
* To be used only with 'bin', 'oct', or 'hex'
|
||||
* values for 'notation' option. When this option
|
||||
* is defined the value is formatted as a signed
|
||||
* twos complement integer of the given word size
|
||||
* and the size suffix is appended to the output.
|
||||
* For example
|
||||
* format(-1, {notation: 'hex', wordSize: 8}) === '0xffi8'.
|
||||
* Default value is undefined.
|
||||
* {number} precision A number between 0 and 16 to round
|
||||
* the digits of the number.
|
||||
* In case of notations 'exponential',
|
||||
* 'engineering', and 'auto',
|
||||
* `precision` defines the total
|
||||
* number of significant digits returned.
|
||||
* In case of notation 'fixed',
|
||||
* `precision` defines the number of
|
||||
* significant digits after the decimal
|
||||
* point.
|
||||
* `precision` is undefined by default.
|
||||
* {number} lowerExp Exponent determining the lower boundary
|
||||
* for formatting a value with an exponent
|
||||
* when `notation='auto`.
|
||||
* Default value is `-3`.
|
||||
* {number} upperExp Exponent determining the upper boundary
|
||||
* for formatting a value with an exponent
|
||||
* when `notation='auto`.
|
||||
* Default value is `5`.
|
||||
* {Function} fn A custom formatting function. Can be used to override the
|
||||
* built-in notations. Function `fn` is called with `value` as
|
||||
* parameter and must return a string. Is useful for example to
|
||||
* format all values inside a matrix in a particular way.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* format(6.4) // '6.4'
|
||||
* format(1240000) // '1.24e6'
|
||||
* format(1/3) // '0.3333333333333333'
|
||||
* format(1/3, 3) // '0.333'
|
||||
* format(21385, 2) // '21000'
|
||||
* format(12e8, {notation: 'fixed'}) // returns '1200000000'
|
||||
* format(2.3, {notation: 'fixed', precision: 4}) // returns '2.3000'
|
||||
* format(52.8, {notation: 'exponential'}) // returns '5.28e+1'
|
||||
* format(12400, {notation: 'engineering'}) // returns '12.400e+3'
|
||||
*
|
||||
* @param {BigNumber} value
|
||||
* @param {Object | Function | number | BigNumber} [options]
|
||||
* @return {string} str The formatted value
|
||||
*/
|
||||
export function format(value, options) {
|
||||
if (typeof options === 'function') {
|
||||
// handle format(value, fn)
|
||||
return options(value);
|
||||
}
|
||||
|
||||
// handle special cases
|
||||
if (!value.isFinite()) {
|
||||
return value.isNaN() ? 'NaN' : value.gt(0) ? 'Infinity' : '-Infinity';
|
||||
}
|
||||
var {
|
||||
notation,
|
||||
precision,
|
||||
wordSize
|
||||
} = normalizeFormatOptions(options);
|
||||
|
||||
// handle the various notations
|
||||
switch (notation) {
|
||||
case 'fixed':
|
||||
return toFixed(value, precision);
|
||||
case 'exponential':
|
||||
return toExponential(value, precision);
|
||||
case 'engineering':
|
||||
return toEngineering(value, precision);
|
||||
case 'bin':
|
||||
return formatBigNumberToBase(value, 2, wordSize);
|
||||
case 'oct':
|
||||
return formatBigNumberToBase(value, 8, wordSize);
|
||||
case 'hex':
|
||||
return formatBigNumberToBase(value, 16, wordSize);
|
||||
case 'auto':
|
||||
{
|
||||
// determine lower and upper bound for exponential notation.
|
||||
// TODO: implement support for upper and lower to be BigNumbers themselves
|
||||
var lowerExp = _toNumberOrDefault(options === null || options === void 0 ? void 0 : options.lowerExp, -3);
|
||||
var upperExp = _toNumberOrDefault(options === null || options === void 0 ? void 0 : options.upperExp, 5);
|
||||
|
||||
// handle special case zero
|
||||
if (value.isZero()) return '0';
|
||||
|
||||
// determine whether or not to output exponential notation
|
||||
var str;
|
||||
var rounded = value.toSignificantDigits(precision);
|
||||
var exp = rounded.e;
|
||||
if (exp >= lowerExp && exp < upperExp) {
|
||||
// normal number notation
|
||||
str = rounded.toFixed();
|
||||
} else {
|
||||
// exponential notation
|
||||
str = toExponential(value, precision);
|
||||
}
|
||||
|
||||
// remove trailing zeros after the decimal point
|
||||
return str.replace(/((\.\d*?)(0+))($|e)/, function () {
|
||||
var digits = arguments[2];
|
||||
var e = arguments[4];
|
||||
return digits !== '.' ? digits + e : e;
|
||||
});
|
||||
}
|
||||
default:
|
||||
throw new Error('Unknown notation "' + notation + '". ' + 'Choose "auto", "exponential", "fixed", "bin", "oct", or "hex.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a BigNumber in engineering notation. Like '1.23e+6', '2.3e+0', '3.500e-3'
|
||||
* @param {BigNumber} value
|
||||
* @param {number} [precision] Optional number of significant figures to return.
|
||||
*/
|
||||
export function toEngineering(value, precision) {
|
||||
// find nearest lower multiple of 3 for exponent
|
||||
var e = value.e;
|
||||
var newExp = e % 3 === 0 ? e : e < 0 ? e - 3 - e % 3 : e - e % 3;
|
||||
|
||||
// find difference in exponents, and calculate the value without exponent
|
||||
var valueWithoutExp = value.mul(Math.pow(10, -newExp));
|
||||
var valueStr = valueWithoutExp.toPrecision(precision);
|
||||
if (valueStr.includes('e')) {
|
||||
var BigNumber = value.constructor;
|
||||
valueStr = new BigNumber(valueStr).toFixed();
|
||||
}
|
||||
return valueStr + 'e' + (e >= 0 ? '+' : '') + newExp.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a number in exponential notation. Like '1.23e+5', '2.3e+0', '3.500e-3'
|
||||
* @param {BigNumber} value
|
||||
* @param {number} [precision] Number of digits in formatted output.
|
||||
* If not provided, the maximum available digits
|
||||
* is used.
|
||||
* @returns {string} str
|
||||
*/
|
||||
export function toExponential(value, precision) {
|
||||
if (precision !== undefined) {
|
||||
return value.toExponential(precision - 1); // Note the offset of one
|
||||
} else {
|
||||
return value.toExponential();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a number with fixed notation.
|
||||
* @param {BigNumber} value
|
||||
* @param {number} [precision=undefined] Optional number of decimals after the
|
||||
* decimal point. Undefined by default.
|
||||
*/
|
||||
export function toFixed(value, precision) {
|
||||
return value.toFixed(precision);
|
||||
}
|
||||
function _toNumberOrDefault(value, defaultValue) {
|
||||
if (isNumber(value)) {
|
||||
return value;
|
||||
} else if (isBigNumber(value)) {
|
||||
return value.toNumber();
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
39
node_modules/mathjs/lib/esm/utils/bignumber/nearlyEqual.js
generated
vendored
Normal file
39
node_modules/mathjs/lib/esm/utils/bignumber/nearlyEqual.js
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Compares two BigNumbers.
|
||||
* @param {BigNumber} a - First value to compare
|
||||
* @param {BigNumber} b - Second value to compare
|
||||
* @param {number} [relTol=1e-09] - The relative tolerance, indicating the maximum allowed difference relative to the larger absolute value. Must be greater than 0.
|
||||
* @param {number} [absTol=0] - The minimum absolute tolerance, useful for comparisons near zero. Must be at least 0.
|
||||
* @returns {boolean} whether the two numbers are nearly equal
|
||||
* @throws {Error} If `relTol` is less than or equal to 0.
|
||||
* @throws {Error} If `absTol` is less than 0.
|
||||
*
|
||||
* @example
|
||||
* nearlyEqual(1.000000001, 1.0, 1e-9); // true
|
||||
* nearlyEqual(1.000000002, 1.0, 0); // false
|
||||
* nearlyEqual(1.0, 1.009, undefined, 0.02); // true
|
||||
* nearlyEqual(0.000000001, 0.0, undefined, 1e-8); // true
|
||||
*/
|
||||
export function nearlyEqual(a, b) {
|
||||
var relTol = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1e-9;
|
||||
var absTol = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
|
||||
if (relTol <= 0) {
|
||||
throw new Error('Relative tolerance must be greater than 0');
|
||||
}
|
||||
if (absTol < 0) {
|
||||
throw new Error('Absolute tolerance must be at least 0');
|
||||
}
|
||||
// NaN
|
||||
if (a.isNaN() || b.isNaN()) {
|
||||
return false;
|
||||
}
|
||||
if (!a.isFinite() || !b.isFinite()) {
|
||||
return a.eq(b);
|
||||
}
|
||||
// use "==" operator, handles infinities
|
||||
if (a.eq(b)) {
|
||||
return true;
|
||||
}
|
||||
// abs(a-b) <= max(relTol * max(abs(a), abs(b)), absTol)
|
||||
return a.minus(b).abs().lte(a.constructor.max(a.constructor.max(a.abs(), b.abs()).mul(relTol), absTol));
|
||||
}
|
||||
Reference in New Issue
Block a user