mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
Minor refactoring
This commit is contained in:
parent
14b41b202e
commit
8757e9febb
@ -83,33 +83,33 @@ exports.tau = memoize(function (BigNumber) {
|
||||
* asec(x) = acos(1/x)
|
||||
*
|
||||
* @param {BigNumber} x
|
||||
* @param {function} Big BigNumber constructor
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @param {boolean} reciprocal is sec
|
||||
* @returns {BigNumber} arccosine or arcsecant of x
|
||||
*/
|
||||
exports.arccos_arcsec = function (x, Big, reciprocal) {
|
||||
exports.arccos_arcsec = function (x, BigNumber, reciprocal) {
|
||||
if (reciprocal) {
|
||||
if (x.abs().lt(Big.ONE)) {
|
||||
if (x.abs().lt(BigNumber.ONE)) {
|
||||
throw new Error('asec() only has non-complex values for |x| >= 1.');
|
||||
}
|
||||
} else if (x.abs().gt(Big.ONE)) {
|
||||
} else if (x.abs().gt(BigNumber.ONE)) {
|
||||
throw new Error('acos() only has non-complex values for |x| <= 1.');
|
||||
}
|
||||
if (x.eq(-1)) {
|
||||
return exports.pi(Big);
|
||||
return exports.pi(BigNumber);
|
||||
}
|
||||
|
||||
var precision = Big.precision;
|
||||
Big.config({precision: precision + 4});
|
||||
var precision = BigNumber.precision;
|
||||
BigNumber.config({precision: precision + 4});
|
||||
|
||||
if (reciprocal) {
|
||||
x = Big.ONE.div(x);
|
||||
x = BigNumber.ONE.div(x);
|
||||
}
|
||||
|
||||
var acos = exports.arctan_arccot(Big.ONE.minus(x.times(x)).sqrt()
|
||||
.div(x.plus(Big.ONE)), Big).times(2);
|
||||
var acos = exports.arctan_arccot(BigNumber.ONE.minus(x.times(x)).sqrt()
|
||||
.div(x.plus(BigNumber.ONE)), BigNumber).times(2);
|
||||
|
||||
Big.config({precision: precision});
|
||||
BigNumber.config({precision: precision});
|
||||
return acos.toDP(precision - 1);
|
||||
};
|
||||
|
||||
@ -117,60 +117,60 @@ exports.arccos_arcsec = function (x, Big, reciprocal) {
|
||||
* Calculate the arcsine or arccosecant of x
|
||||
*
|
||||
* @param {BigNumber} x
|
||||
* @param {function} Big BigNumber constructor
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @param {boolean} reciprocal is csc
|
||||
* @returns {BigNumber} arcsine or arccosecant of x
|
||||
*/
|
||||
exports.arcsin_arccsc = function (x, Big, reciprocal) {
|
||||
exports.arcsin_arccsc = function (x, BigNumber, reciprocal) {
|
||||
if (x.isNaN()) {
|
||||
return new Big(NaN);
|
||||
return new BigNumber(NaN);
|
||||
}
|
||||
|
||||
var precision = Big.precision;
|
||||
var precision = BigNumber.precision;
|
||||
var absX = x.abs();
|
||||
if (reciprocal) {
|
||||
if (absX.lt(Big.ONE)) {
|
||||
if (absX.lt(BigNumber.ONE)) {
|
||||
throw new Error('acsc() only has non-complex values for |x| >= 1.');
|
||||
}
|
||||
|
||||
Big.config({precision: precision + 2});
|
||||
x = Big.ONE.div(x);
|
||||
Big.config({precision: precision});
|
||||
BigNumber.config({precision: precision + 2});
|
||||
x = BigNumber.ONE.div(x);
|
||||
BigNumber.config({precision: precision});
|
||||
|
||||
absX = x.abs();
|
||||
} else if (absX.gt(Big.ONE)) {
|
||||
} else if (absX.gt(BigNumber.ONE)) {
|
||||
throw new Error('asin() only has non-complex values for |x| <= 1.');
|
||||
}
|
||||
|
||||
// Get x below 0.58
|
||||
if (absX.gt(0.8)) {
|
||||
Big.config({precision: precision + 4});
|
||||
BigNumber.config({precision: precision + 4});
|
||||
|
||||
// arcsin(x) = sign(x)*(Pi/2 - arcsin(sqrt(1 - x^2)))
|
||||
var sign = x.s;
|
||||
var halfPi = exports.pi(Big.constructor({precision: precision + 4})).div(2);
|
||||
x = halfPi.minus(exports.arcsin_arccsc(Big.ONE.minus(x.times(x)).sqrt(), Big));
|
||||
var halfPi = exports.pi(BigNumber.constructor({precision: precision + 4})).div(2);
|
||||
x = halfPi.minus(exports.arcsin_arccsc(BigNumber.ONE.minus(x.times(x)).sqrt(), BigNumber));
|
||||
x.s = sign;
|
||||
|
||||
x.constructor = Big;
|
||||
Big.config({precision: precision});
|
||||
x.constructor = BigNumber;
|
||||
BigNumber.config({precision: precision});
|
||||
return x.toDP(precision - 1);
|
||||
}
|
||||
var wasReduced = absX.gt(0.58);
|
||||
if (wasReduced) {
|
||||
Big.config({precision: precision + 8});
|
||||
BigNumber.config({precision: precision + 8});
|
||||
|
||||
// arcsin(x) = 2*arcsin(x / (sqrt(2)*sqrt(sqrt(1 - x^2) + 1)))
|
||||
x = x.div(new Big(2).sqrt().times(Big.ONE.minus(x.times(x)).sqrt()
|
||||
.plus(Big.ONE).sqrt()));
|
||||
x = x.div(new BigNumber(2).sqrt().times(BigNumber.ONE.minus(x.times(x)).sqrt()
|
||||
.plus(BigNumber.ONE).sqrt()));
|
||||
|
||||
Big.config({precision: precision});
|
||||
BigNumber.config({precision: precision});
|
||||
}
|
||||
|
||||
// Avoid overhead of Newton's Method if feasible
|
||||
var ret = (precision <= 60 || ((x.dp() <= Math.log(precision)) && x.lt(0.05)))
|
||||
? arcsin_taylor(x, precision)
|
||||
: arcsin_newton(x, Big);
|
||||
: arcsin_newton(x, BigNumber);
|
||||
|
||||
if (wasReduced) {
|
||||
return ret.times(2);
|
||||
@ -182,57 +182,57 @@ exports.arcsin_arccsc = function (x, Big, reciprocal) {
|
||||
* Calculate the arctangent or arccotangent of x
|
||||
*
|
||||
* @param {BigNumber} x
|
||||
* @param {DecimalFactory} Big current BigNumber constructor
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @param {boolean} reciprocal is cot
|
||||
* @returns {BigNumber} arctangent or arccotangent of x
|
||||
*/
|
||||
exports.arctan_arccot = function (x, Big, reciprocal) {
|
||||
exports.arctan_arccot = function (x, BigNumber, reciprocal) {
|
||||
if (x.isNaN()) {
|
||||
return new Big(NaN);
|
||||
return new BigNumber(NaN);
|
||||
}
|
||||
if ((!reciprocal && x.isZero()) || (reciprocal && !x.isFinite())) {
|
||||
return new Big(0);
|
||||
return new BigNumber(0);
|
||||
}
|
||||
|
||||
var precision = Big.precision;
|
||||
var precision = BigNumber.precision;
|
||||
if ((!reciprocal && !x.isFinite()) || (reciprocal && x.isZero())) {
|
||||
var halfPi = exports.pi(Big.constructor({precision: precision + 2})).div(2).toDP(precision - 1);
|
||||
halfPi.constructor = Big;
|
||||
var halfPi = exports.pi(BigNumber.constructor({precision: precision + 2})).div(2).toDP(precision - 1);
|
||||
halfPi.constructor = BigNumber;
|
||||
halfPi.s = x.s;
|
||||
|
||||
return halfPi;
|
||||
}
|
||||
|
||||
Big.config({precision: precision + 4});
|
||||
BigNumber.config({precision: precision + 4});
|
||||
|
||||
if (reciprocal) {
|
||||
x = Big.ONE.div(x);
|
||||
x = BigNumber.ONE.div(x);
|
||||
}
|
||||
|
||||
var absX = x.abs();
|
||||
if (absX.lte(0.875)) {
|
||||
var ret = arctan_taylor(x);
|
||||
|
||||
ret.constructor = Big;
|
||||
Big.config({precision: precision});
|
||||
return ret.toDP(Big.precision - 1);
|
||||
ret.constructor = BigNumber;
|
||||
BigNumber.config({precision: precision});
|
||||
return ret.toDP(BigNumber.precision - 1);
|
||||
}
|
||||
if (absX.gte(1.143)) {
|
||||
// arctan(x) = sign(x)*((PI / 2) - arctan(1 / |x|))
|
||||
var halfPi = exports.pi(Big.constructor({precision: precision + 4})).div(2);
|
||||
var ret = halfPi.minus(arctan_taylor(Big.ONE.div(absX)));
|
||||
var halfPi = exports.pi(BigNumber.constructor({precision: precision + 4})).div(2);
|
||||
var ret = halfPi.minus(arctan_taylor(BigNumber.ONE.div(absX)));
|
||||
ret.s = x.s;
|
||||
|
||||
ret.constructor = Big;
|
||||
Big.config({precision: precision});
|
||||
return ret.toDP(Big.precision - 1);
|
||||
ret.constructor = BigNumber;
|
||||
BigNumber.config({precision: precision});
|
||||
return ret.toDP(BigNumber.precision - 1);
|
||||
}
|
||||
|
||||
// arctan(x) = arcsin(x / [sqrt(1 + x^2)])
|
||||
x = x.div(x.times(x).plus(1).sqrt());
|
||||
|
||||
Big.config({precision: precision});
|
||||
return exports.arcsin_arccsc(x, Big);
|
||||
BigNumber.config({precision: precision});
|
||||
return exports.arcsin_arccsc(x, BigNumber);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -240,34 +240,34 @@ exports.arctan_arccot = function (x, Big, reciprocal) {
|
||||
*
|
||||
* @param {BigNumber} y
|
||||
* @param {BigNumber} x
|
||||
* @param {DecimalFactory} Big current BigNumber constructor
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @returns {BigNumber} arctangent of y, x
|
||||
*/
|
||||
exports.arctan2 = function (y, x, Big) {
|
||||
var precision = Big.precision;
|
||||
exports.arctan2 = function (y, x, BigNumber) {
|
||||
var precision = BigNumber.precision;
|
||||
|
||||
if (x.isZero()) {
|
||||
if (y.isZero()) {
|
||||
return new Big(NaN);
|
||||
return new BigNumber(NaN);
|
||||
}
|
||||
|
||||
var halfPi = exports.pi(Big.constructor({precision: precision + 2})).div(2).toDP(precision - 1);
|
||||
halfPi.constructor = Big;
|
||||
var halfPi = exports.pi(BigNumber.constructor({precision: precision + 2})).div(2).toDP(precision - 1);
|
||||
halfPi.constructor = BigNumber;
|
||||
halfPi.s = y.s;
|
||||
|
||||
return halfPi;
|
||||
}
|
||||
|
||||
Big.config({precision: precision + 2});
|
||||
BigNumber.config({precision: precision + 2});
|
||||
|
||||
var ret = exports.arctan_arccot(y.div(x), Big, false);
|
||||
var ret = exports.arctan_arccot(y.div(x), BigNumber, false);
|
||||
if (x.isNegative()) {
|
||||
var pi = exports.pi(Big);
|
||||
var pi = exports.pi(BigNumber);
|
||||
ret = y.isNegative() ? ret.minus(pi) : ret.plus(pi);
|
||||
}
|
||||
|
||||
ret.constructor = Big;
|
||||
Big.config({precision: precision});
|
||||
ret.constructor = BigNumber;
|
||||
BigNumber.config({precision: precision});
|
||||
return ret.toDP(precision - 1);
|
||||
};
|
||||
|
||||
@ -283,43 +283,43 @@ exports.arctan2 = function (y, x, Big) {
|
||||
* acsch(x) = asinh(1 / x)
|
||||
*
|
||||
* @param {BigNumber} x
|
||||
* @param {DecimalFactory} Big current BigNumber constructor
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @param {boolean} mode sine function if true, cosine function if false
|
||||
* @param {boolean} reciprocal is sec or csc
|
||||
* @returns {BigNumber} hyperbolic arccosine, arcsine, arcsecant, or arccosecant of x
|
||||
*/
|
||||
exports.acosh_asinh_asech_acsch = function (x, Big, mode, reciprocal) {
|
||||
exports.acosh_asinh_asech_acsch = function (x, BigNumber, mode, reciprocal) {
|
||||
if (x.isNaN()) {
|
||||
return new Big(NaN);
|
||||
return new BigNumber(NaN);
|
||||
}
|
||||
if (reciprocal && x.isZero()) {
|
||||
return new Big(Infinity);
|
||||
return new BigNumber(Infinity);
|
||||
}
|
||||
if (!mode) {
|
||||
if (reciprocal) {
|
||||
if (x.isNegative() || x.gt(Big.ONE)) {
|
||||
if (x.isNegative() || x.gt(BigNumber.ONE)) {
|
||||
throw new Error('asech() only has non-complex values for 0 <= x <= 1.');
|
||||
}
|
||||
} else if (x.lt(Big.ONE)) {
|
||||
} else if (x.lt(BigNumber.ONE)) {
|
||||
throw new Error('acosh() only has non-complex values for x >= 1.');
|
||||
}
|
||||
}
|
||||
|
||||
var precision = Big.precision;
|
||||
Big.config({precision: precision + 4});
|
||||
var precision = BigNumber.precision;
|
||||
BigNumber.config({precision: precision + 4});
|
||||
|
||||
var y = new Big(x);
|
||||
y.constructor = Big;
|
||||
var y = new BigNumber(x);
|
||||
y.constructor = BigNumber;
|
||||
|
||||
if (reciprocal) {
|
||||
y = Big.ONE.div(y);
|
||||
y = BigNumber.ONE.div(y);
|
||||
}
|
||||
|
||||
var x2PlusOrMinus = (mode) ? y.times(y).plus(Big.ONE) : y.times(y).minus(Big.ONE);
|
||||
var x2PlusOrMinus = (mode) ? y.times(y).plus(BigNumber.ONE) : y.times(y).minus(BigNumber.ONE);
|
||||
var ret = y.plus(x2PlusOrMinus.sqrt()).ln();
|
||||
|
||||
Big.config({precision: precision});
|
||||
return new Big(ret.toPrecision(precision));
|
||||
BigNumber.config({precision: precision});
|
||||
return new BigNumber(ret.toPrecision(precision));
|
||||
};
|
||||
|
||||
/**
|
||||
@ -330,20 +330,20 @@ exports.acosh_asinh_asech_acsch = function (x, Big, mode, reciprocal) {
|
||||
* acoth(x) = atanh(1 / x)
|
||||
*
|
||||
* @param {BigNumber} x
|
||||
* @param {DecimalFactory} Big current BigNumber constructor
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @param {boolean} reciprocal is sec or csc
|
||||
* @returns {BigNumber} hyperbolic arctangent or arccotangent of x
|
||||
*/
|
||||
exports.atanh_acoth = function (x, Big, reciprocal) {
|
||||
exports.atanh_acoth = function (x, BigNumber, reciprocal) {
|
||||
if (x.isNaN()) {
|
||||
return new Big(NaN);
|
||||
return new BigNumber(NaN);
|
||||
}
|
||||
|
||||
var absX = x.abs();
|
||||
if (absX.eq(Big.ONE)) {
|
||||
return new Big(x.isNegative() ? -Infinity : Infinity);
|
||||
if (absX.eq(BigNumber.ONE)) {
|
||||
return new BigNumber(x.isNegative() ? -Infinity : Infinity);
|
||||
}
|
||||
if (absX.gt(Big.ONE)) {
|
||||
if (absX.gt(BigNumber.ONE)) {
|
||||
if (!reciprocal) {
|
||||
throw new Error('atanh() only has non-complex values for |x| <= 1.');
|
||||
}
|
||||
@ -352,22 +352,22 @@ exports.atanh_acoth = function (x, Big, reciprocal) {
|
||||
}
|
||||
|
||||
if (x.isZero()) {
|
||||
return new Big(0);
|
||||
return new BigNumber(0);
|
||||
}
|
||||
|
||||
var precision = Big.precision;
|
||||
Big.config({precision: precision + 4});
|
||||
var precision = BigNumber.precision;
|
||||
BigNumber.config({precision: precision + 4});
|
||||
|
||||
var y = new Big(x);
|
||||
y.constructor = Big;
|
||||
var y = new BigNumber(x);
|
||||
y.constructor = BigNumber;
|
||||
|
||||
if (reciprocal) {
|
||||
y = Big.ONE.div(y);
|
||||
y = BigNumber.ONE.div(y);
|
||||
}
|
||||
var ret = Big.ONE.plus(y).div(Big.ONE.minus(y)).ln().div(2);
|
||||
var ret = BigNumber.ONE.plus(y).div(BigNumber.ONE.minus(y)).ln().div(2);
|
||||
|
||||
Big.config({precision: precision});
|
||||
return new Big(ret.toPrecision(precision));
|
||||
BigNumber.config({precision: precision});
|
||||
return new BigNumber(ret.toPrecision(precision));
|
||||
};
|
||||
|
||||
/**
|
||||
@ -379,19 +379,19 @@ exports.atanh_acoth = function (x, Big, reciprocal) {
|
||||
* http://www.tc.umn.edu/~ringx004/sidebar.html
|
||||
*
|
||||
* @param {BigNumber} x
|
||||
* @param {DecimalFactory} Big current BigNumber constructor
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @param {number} mode cosine function if 0, sine function if 1
|
||||
* @param {boolean} reciprocal is sec or csc
|
||||
* @returns {BigNumber} cosine, sine, secant, or cosecant of x
|
||||
*/
|
||||
exports.cos_sin_sec_csc = function (x, Big, mode, reciprocal) {
|
||||
exports.cos_sin_sec_csc = function (x, BigNumber, mode, reciprocal) {
|
||||
if (x.isNaN() || !x.isFinite()) {
|
||||
return new Big(NaN);
|
||||
return new BigNumber(NaN);
|
||||
}
|
||||
var precision = Big.precision;
|
||||
var precision = BigNumber.precision;
|
||||
|
||||
// Avoid changing the original value
|
||||
var y = new Big(x);
|
||||
var y = new BigNumber(x);
|
||||
|
||||
// sin(-x) == -sin(x), cos(-x) == cos(x)
|
||||
var isNeg = y.isNegative();
|
||||
@ -401,17 +401,17 @@ exports.cos_sin_sec_csc = function (x, Big, mode, reciprocal) {
|
||||
|
||||
// Apply ~log(precision) guard bits
|
||||
var precPlusGuardDigits = precision + (Math.log(precision) | 0) + 3;
|
||||
Big.config({precision: precPlusGuardDigits});
|
||||
BigNumber.config({precision: precPlusGuardDigits});
|
||||
|
||||
y = reduceToPeriod(y, Big.constructor({precision: precPlusGuardDigits}), mode); // Make this destructive
|
||||
y[0].constructor = Big;
|
||||
y = reduceToPeriod(y, BigNumber.constructor({precision: precPlusGuardDigits}), mode); // Make this destructive
|
||||
y[0].constructor = BigNumber;
|
||||
if (y[1]) {
|
||||
y = y[0];
|
||||
if (reciprocal && y.isZero()) {
|
||||
y = new Big(Infinity);
|
||||
y = new BigNumber(Infinity);
|
||||
}
|
||||
|
||||
Big.config({precision: precision});
|
||||
BigNumber.config({precision: precision});
|
||||
return y;
|
||||
}
|
||||
|
||||
@ -419,11 +419,11 @@ exports.cos_sin_sec_csc = function (x, Big, mode, reciprocal) {
|
||||
y = y[0];
|
||||
if (mode) {
|
||||
ret = cos_sin_taylor(y.div(3125), mode);
|
||||
Big.config({precision: Math.min(precPlusGuardDigits, precision + 15)});
|
||||
BigNumber.config({precision: Math.min(precPlusGuardDigits, precision + 15)});
|
||||
|
||||
var five = new Big(5);
|
||||
var sixteen = new Big(16);
|
||||
var twenty = new Big(20);
|
||||
var five = new BigNumber(5);
|
||||
var sixteen = new BigNumber(16);
|
||||
var twenty = new BigNumber(20);
|
||||
for (var i = 0; i < 5; ++i) {
|
||||
var ret2 = ret.times(ret);
|
||||
var ret3 = ret2.times(ret);
|
||||
@ -438,7 +438,7 @@ exports.cos_sin_sec_csc = function (x, Big, mode, reciprocal) {
|
||||
}
|
||||
} else {
|
||||
var div_factor, loops;
|
||||
if (y.abs().lt(Big.ONE)) {
|
||||
if (y.abs().lt(BigNumber.ONE)) {
|
||||
div_factor = 64;
|
||||
loops = 3;
|
||||
} else {
|
||||
@ -447,23 +447,23 @@ exports.cos_sin_sec_csc = function (x, Big, mode, reciprocal) {
|
||||
}
|
||||
|
||||
ret = cos_sin_taylor(y.div(div_factor), mode);
|
||||
Big.config({precision: Math.min(precPlusGuardDigits, precision + 8)});
|
||||
BigNumber.config({precision: Math.min(precPlusGuardDigits, precision + 8)});
|
||||
|
||||
var eight = new Big(8);
|
||||
var eight = new BigNumber(8);
|
||||
for (; loops > 0; --loops) {
|
||||
var ret2 = ret.times(ret);
|
||||
var ret4 = ret2.times(ret2);
|
||||
ret = eight.times(ret4.minus(ret2)).plus(Big.ONE);
|
||||
ret = eight.times(ret4.minus(ret2)).plus(BigNumber.ONE);
|
||||
}
|
||||
}
|
||||
|
||||
if (reciprocal) {
|
||||
ret = (ret.e <= -precision)
|
||||
? new Big(Infinity)
|
||||
: Big.ONE.div(ret);
|
||||
? new BigNumber(Infinity)
|
||||
: BigNumber.ONE.div(ret);
|
||||
}
|
||||
|
||||
Big.config({precision: precision});
|
||||
BigNumber.config({precision: precision});
|
||||
return ret.toDP(precision - 1);
|
||||
};
|
||||
|
||||
@ -475,27 +475,27 @@ exports.cos_sin_sec_csc = function (x, Big, mode, reciprocal) {
|
||||
* cot(x) = cos(x) / sin(x)
|
||||
*
|
||||
* @param {BigNumber} x
|
||||
* @param {DecimalFactory} Big current BigNumber constructor
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @param {boolean} reciprocal is cot
|
||||
* @returns {BigNumber} tangent or cotangent of x
|
||||
*/
|
||||
exports.tan_cot = function (x, Big, reciprocal) {
|
||||
exports.tan_cot = function (x, BigNumber, reciprocal) {
|
||||
if (x.isNaN()) {
|
||||
return new Big(NaN);
|
||||
return new BigNumber(NaN);
|
||||
}
|
||||
|
||||
var precision = Big.precision;
|
||||
var pi = exports.pi(Big.constructor({precision: precision + 2}));
|
||||
var precision = BigNumber.precision;
|
||||
var pi = exports.pi(BigNumber.constructor({precision: precision + 2}));
|
||||
var halfPi = pi.div(2).toDP(precision - 1);
|
||||
pi = pi.toDP(precision - 1);
|
||||
|
||||
var y = reduceToPeriod(x, Big, 1)[0];
|
||||
var y = reduceToPeriod(x, BigNumber, 1)[0];
|
||||
if (y.abs().eq(pi)) {
|
||||
return new Big(Infinity);
|
||||
return new BigNumber(Infinity);
|
||||
}
|
||||
|
||||
Big.config({precision: precision + 4});
|
||||
var sin = exports.cos_sin_sec_csc(y, Big, 1, false);
|
||||
BigNumber.config({precision: precision + 4});
|
||||
var sin = exports.cos_sin_sec_csc(y, BigNumber, 1, false);
|
||||
var cos = sinToCos(sin);
|
||||
|
||||
sin = sin.toDP(precision);
|
||||
@ -512,8 +512,8 @@ exports.tan_cot = function (x, Big, reciprocal) {
|
||||
|
||||
var tan = (reciprocal) ? cos.div(sin) : sin.div(cos);
|
||||
|
||||
Big.config({precision: precision});
|
||||
return new Big(tan.toPrecision(precision));
|
||||
BigNumber.config({precision: precision});
|
||||
return new BigNumber(tan.toPrecision(precision));
|
||||
};
|
||||
|
||||
/**
|
||||
@ -532,34 +532,34 @@ exports.tan_cot = function (x, Big, reciprocal) {
|
||||
* = 2 / (e^x - 1/e^x)
|
||||
*
|
||||
* @param {BigNumber} x
|
||||
* @param {DecimalFactory} Big current BigNumber constructor
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @param {boolean} mode sinh function if true, cosh function if false
|
||||
* @param {boolean} reciprocal is sech or csch
|
||||
* @returns {BigNumber} hyperbolic cosine, sine, secant. or cosecant of x
|
||||
*/
|
||||
exports.cosh_sinh_csch_sech = function (x, Big, mode, reciprocal) {
|
||||
exports.cosh_sinh_csch_sech = function (x, BigNumber, mode, reciprocal) {
|
||||
if (x.isNaN()) {
|
||||
return new Big(NaN);
|
||||
return new BigNumber(NaN);
|
||||
}
|
||||
if (!x.isFinite()) {
|
||||
if (reciprocal) {
|
||||
return new Big(0);
|
||||
return new BigNumber(0);
|
||||
}
|
||||
return new Big((mode) ? x : Infinity);
|
||||
return new BigNumber((mode) ? x : Infinity);
|
||||
}
|
||||
|
||||
var precision = Big.precision;
|
||||
Big.config({precision: precision + 4});
|
||||
var precision = BigNumber.precision;
|
||||
BigNumber.config({precision: precision + 4});
|
||||
|
||||
var y = new Big(x);
|
||||
y.constructor = Big;
|
||||
var y = new BigNumber(x);
|
||||
y.constructor = BigNumber;
|
||||
|
||||
y = y.exp();
|
||||
y = (mode) ? y.minus(Big.ONE.div(y)) : y.plus(Big.ONE.div(y));
|
||||
y = (reciprocal) ? new Big(2).div(y) : y.div(2);
|
||||
y = (mode) ? y.minus(BigNumber.ONE.div(y)) : y.plus(BigNumber.ONE.div(y));
|
||||
y = (reciprocal) ? new BigNumber(2).div(y) : y.div(2);
|
||||
|
||||
Big.config({precision: precision});
|
||||
return new Big(y.toPrecision(precision));
|
||||
BigNumber.config({precision: precision});
|
||||
return new BigNumber(y.toPrecision(precision));
|
||||
};
|
||||
|
||||
/**
|
||||
@ -574,30 +574,30 @@ exports.cosh_sinh_csch_sech = function (x, Big, mode, reciprocal) {
|
||||
* = (e^x + 1/e^x) / (e^x - 1/e^x)
|
||||
*
|
||||
* @param {BigNumber} x
|
||||
* @param {DecimalFactory} Big current BigNumber constructor
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @param {boolean} reciprocal is coth
|
||||
* @returns {BigNumber} hyperbolic tangent or cotangent of x
|
||||
*/
|
||||
exports.tanh_coth = function (x, Big, reciprocal) {
|
||||
exports.tanh_coth = function (x, BigNumber, reciprocal) {
|
||||
if (x.isNaN()) {
|
||||
return new Big(NaN);
|
||||
return new BigNumber(NaN);
|
||||
}
|
||||
if (!x.isFinite()) {
|
||||
return new Big(x.s);
|
||||
return new BigNumber(x.s);
|
||||
}
|
||||
|
||||
var precision = Big.precision;
|
||||
Big.config({precision: precision + 4});
|
||||
var precision = BigNumber.precision;
|
||||
BigNumber.config({precision: precision + 4});
|
||||
|
||||
var y = new Big(x);
|
||||
y.constructor = Big;
|
||||
var y = new BigNumber(x);
|
||||
y.constructor = BigNumber;
|
||||
|
||||
var posExp = y.exp();
|
||||
var negExp = Big.ONE.div(posExp);
|
||||
var negExp = BigNumber.ONE.div(posExp);
|
||||
var ret = posExp.minus(negExp);
|
||||
ret = (reciprocal) ? posExp.plus(negExp).div(ret) : ret.div(posExp.plus(negExp));
|
||||
|
||||
Big.config({precision: precision});
|
||||
BigNumber.config({precision: precision});
|
||||
return ret.toDP(precision - 1);
|
||||
};
|
||||
|
||||
@ -611,23 +611,23 @@ exports.tanh_coth = function (x, Big, reciprocal) {
|
||||
* x_(i+1) = x_i - (sin(x_i) - N)/cos(x_i)
|
||||
*
|
||||
* @param {BigNumber} x
|
||||
* @param {DecimalFactory} Big current BigNumber constructor
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @returns {BigNumber} arc sine of x
|
||||
*/
|
||||
function arcsin_newton(x, Big) {
|
||||
var oldPrecision = Big.precision;
|
||||
function arcsin_newton(x, BigNumber) {
|
||||
var oldPrecision = BigNumber.precision;
|
||||
|
||||
// Calibration variables, adjusted from MAPM
|
||||
var tolerance = -(oldPrecision + 4);
|
||||
var maxp = oldPrecision + 8 - x.e;
|
||||
var localPrecision = 25 - x.e;
|
||||
var maxIter = Math.max(Math.log(oldPrecision + 2) * 1.442695 | 0 + 5, 5);
|
||||
Big.config({precision: localPrecision});
|
||||
BigNumber.config({precision: localPrecision});
|
||||
|
||||
var i = 0;
|
||||
var curr = new Big(Math.asin(x.toNumber()) + '');
|
||||
var curr = new BigNumber(Math.asin(x.toNumber()) + '');
|
||||
do {
|
||||
var tmp0 = exports.cos_sin_sec_csc(curr, Big, 1, false);
|
||||
var tmp0 = exports.cos_sin_sec_csc(curr, BigNumber, 1, false);
|
||||
var tmp1 = sinToCos(tmp0);
|
||||
if (!tmp0.isZero()) {
|
||||
tmp0.s = curr.s;
|
||||
@ -637,7 +637,7 @@ function arcsin_newton(x, Big) {
|
||||
curr = curr.minus(tmp2);
|
||||
|
||||
localPrecision = Math.min(2*localPrecision, maxp);
|
||||
Big.config({precision: localPrecision});
|
||||
BigNumber.config({precision: localPrecision});
|
||||
} while ((2*tmp2.e >= tolerance) && !tmp2.isZero() && (++i <= maxIter))
|
||||
|
||||
if (i == maxIter) {
|
||||
@ -645,7 +645,7 @@ function arcsin_newton(x, Big) {
|
||||
'Try with a higher precision.');
|
||||
}
|
||||
|
||||
Big.config({precision: oldPrecision});
|
||||
BigNumber.config({precision: oldPrecision});
|
||||
return curr.toDP(oldPrecision - 1);
|
||||
}
|
||||
|
||||
@ -660,18 +660,18 @@ function arcsin_newton(x, Big) {
|
||||
* @returns {BigNumber} arc sine of x
|
||||
*/
|
||||
function arcsin_taylor(x, precision) {
|
||||
var Big = x.constructor;
|
||||
Big.config({precision: precision + Math.log(precision) | 0 + 4});
|
||||
var BigNumber = x.constructor;
|
||||
BigNumber.config({precision: precision + Math.log(precision) | 0 + 4});
|
||||
|
||||
var one = new Big(1);
|
||||
var one = new BigNumber(1);
|
||||
var y = x;
|
||||
var yPrev = NaN;
|
||||
var x2 = x.times(x);
|
||||
var polyNum = x;
|
||||
var constNum = new Big(one);
|
||||
var constDen = new Big(one);
|
||||
var constNum = new BigNumber(one);
|
||||
var constDen = new BigNumber(one);
|
||||
|
||||
var bigK = new Big(one);
|
||||
var bigK = new BigNumber(one);
|
||||
for (var k = 3; !y.equals(yPrev); k += 2) {
|
||||
polyNum = polyNum.times(x2);
|
||||
|
||||
@ -679,11 +679,11 @@ function arcsin_taylor(x, precision) {
|
||||
constDen = constDen.times(bigK.plus(one));
|
||||
|
||||
yPrev = y;
|
||||
bigK = new Big(k);
|
||||
bigK = new BigNumber(k);
|
||||
y = y.plus(polyNum.times(constNum).div(bigK.times(constDen)));
|
||||
}
|
||||
|
||||
Big.config({precision: precision});
|
||||
BigNumber.config({precision: precision});
|
||||
return y.toDP(precision - 1);
|
||||
}
|
||||
|
||||
@ -753,28 +753,27 @@ function cos_sin_taylor(x, mode) {
|
||||
* Reduce x within a period of pi (0, pi] with guard digits.
|
||||
*
|
||||
* @param {BigNumber} x
|
||||
* @param {function} Big BigNumber constructor
|
||||
* @param {function} BigNumber BigNumber constructor
|
||||
* @param {number} mode
|
||||
* @returns {Array} [Reduced x, is tau multiple?]
|
||||
*/
|
||||
function reduceToPeriod(x, Big, mode) {
|
||||
var pi = exports.pi(Big.constructor({precision: Big.precision + 2}));
|
||||
var tau = exports.tau(Big);
|
||||
function reduceToPeriod(x, BigNumber, mode) {
|
||||
var pi = exports.pi(BigNumber.constructor({precision: BigNumber.precision + 2}));
|
||||
var tau = exports.tau(BigNumber);
|
||||
if (x.abs().lte(pi.toDP(x.dp()))) {
|
||||
return [x, false];
|
||||
}
|
||||
|
||||
var Big = x.constructor;
|
||||
// Catch if input is tau multiple using pi's precision
|
||||
if (x.div(pi.toDP(x.dp())).toNumber() % 2 == 0) {
|
||||
return [new Big(mode ^ 1), true];
|
||||
return [new BigNumber(mode ^ 1), true];
|
||||
}
|
||||
|
||||
var y = x.mod(tau);
|
||||
|
||||
// Catch if tau multiple with tau's precision
|
||||
if (y.toDP(x.dp(), 1).isZero()) {
|
||||
return [new Big(mode ^ 1), true];
|
||||
return [new BigNumber(mode ^ 1), true];
|
||||
}
|
||||
|
||||
if (y.gt(pi)) {
|
||||
@ -788,7 +787,7 @@ function reduceToPeriod(x, Big, mode) {
|
||||
}
|
||||
}
|
||||
|
||||
y.constructor = Big;
|
||||
y.constructor = x.constructor;
|
||||
return [y, false];
|
||||
}
|
||||
|
||||
@ -801,12 +800,12 @@ function reduceToPeriod(x, Big, mode) {
|
||||
* @returns {BigNumber} sine as cosine
|
||||
*/
|
||||
function sinToCos(sinVal) {
|
||||
var Big = sinVal.constructor;
|
||||
var precision = Big.precision;
|
||||
Big.config({precision: precision + 2});
|
||||
var BigNumber = sinVal.constructor;
|
||||
var precision = BigNumber.precision;
|
||||
BigNumber.config({precision: precision + 2});
|
||||
|
||||
var ret = Big.ONE.minus(sinVal.times(sinVal)).sqrt();
|
||||
var ret = BigNumber.ONE.minus(sinVal.times(sinVal)).sqrt();
|
||||
|
||||
Big.config({precision: precision});
|
||||
BigNumber.config({precision: precision});
|
||||
return ret.toDP(precision - 1);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user