1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2026-09-23 20:35:04 +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

@@ -2629,9 +2629,9 @@
*/
function cosine(Ctor, x) {
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
@@ -3665,10 +3665,10 @@
function sine(Ctor, x) {
var k,
len = x.d.length;
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)
@@ -3959,6 +3959,7 @@
* sinh
* sqrt
* sub
* sum
* tan
* tanh
* trunc
@@ -4407,6 +4408,7 @@
Decimal.sinh = sinh; // ES6
Decimal.sqrt = sqrt;
Decimal.sub = sub;
Decimal.sum = sum;
Decimal.tan = tan;
Decimal.tanh = tanh; // ES6
Decimal.trunc = trunc; // ES6
@@ -4801,6 +4803,28 @@
}
/*
* 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`.