1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2026-09-23 12:24:47 +00:00

#100 Add Decimal.sum method

This commit is contained in:
Michael Mclaughlin
2021-06-22 10:13:28 +01:00
parent 0e0dcaec27
commit 66a21ee9d1
6 changed files with 149 additions and 11 deletions

View File

@@ -2624,13 +2624,15 @@ function convertBase(str, baseIn, baseOut) {
*
*/
function cosine(Ctor, x) {
var k, y,
len = x.d.length;
var k, len, y;
if (x.isZero()) return x;
// Argument reduction: cos(4x) = 8*(cos^4(x) - cos^2(x)) + 1
// i.e. cos(x) = 8*(cos^4(x/4) - cos^2(x/4)) + 1
// Estimate the optimum number of times to use the argument reduction.
len = x.d.length;
if (len < 32) {
k = Math.ceil(len / 3);
y = (1 / tinyPow(4, k)).toString();
@@ -3660,7 +3662,9 @@ function sine(Ctor, x) {
var k,
len = x.d.length;
if (len < 3) return taylorSeries(Ctor, 2, x, x);
if (len < 3) {
return x.isZero() ? x : taylorSeries(Ctor, 2, x, x);
}
// Argument reduction: sin(5x) = 16*sin^5(x) - 20*sin^3(x) + 5*sin(x)
// i.e. sin(x) = 16*sin^5(x/5) - 20*sin^3(x/5) + 5*sin(x/5)
@@ -3951,6 +3955,7 @@ function truncate(arr, len) {
* sinh
* sqrt
* sub
* sum
* tan
* tanh
* trunc
@@ -4399,6 +4404,7 @@ function clone(obj) {
Decimal.sinh = sinh; // ES6
Decimal.sqrt = sqrt;
Decimal.sub = sub;
Decimal.sum = sum;
Decimal.tan = tan;
Decimal.tanh = tanh; // ES6
Decimal.trunc = trunc; // ES6
@@ -4793,6 +4799,28 @@ function sub(x, y) {
}
/*
* Return a new Decimal whose value is the sum of the arguments, rounded to `precision`
* significant digits using rounding mode `rounding`.
*
* Only the result is rounded, not the intermediate calculations.
*
* arguments {number|string|Decimal}
*
*/
function sum() {
var i = 0,
args = arguments,
x = new this(args[i]);
external = false;
for (; x.s && ++i < args.length;) x = x.plus(args[i]);
external = true;
return finalise(x, this.precision, this.rounding);
}
/*
* Return a new Decimal whose value is the tangent of `x`, rounded to `precision` significant
* digits using rounding mode `rounding`.