1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-10-27 20:34:12 +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

@ -3959,6 +3959,7 @@
* sinh * sinh
* sqrt * sqrt
* sub * sub
* sum
* tan * tan
* tanh * tanh
* trunc * trunc
@ -4407,6 +4408,7 @@
Decimal.sinh = sinh; // ES6 Decimal.sinh = sinh; // ES6
Decimal.sqrt = sqrt; Decimal.sqrt = sqrt;
Decimal.sub = sub; Decimal.sub = sub;
Decimal.sum = sum;
Decimal.tan = tan; Decimal.tan = tan;
Decimal.tanh = tanh; // ES6 Decimal.tanh = tanh; // ES6
Decimal.trunc = trunc; // 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 * Return a new Decimal whose value is the tangent of `x`, rounded to `precision` significant
* digits using rounding mode `rounding`. * digits using rounding mode `rounding`.

View File

@ -2624,13 +2624,15 @@ function convertBase(str, baseIn, baseOut) {
* *
*/ */
function cosine(Ctor, x) { function cosine(Ctor, x) {
var k, y, var k, len, y;
len = x.d.length;
if (x.isZero()) return x;
// Argument reduction: cos(4x) = 8*(cos^4(x) - cos^2(x)) + 1 // 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 // 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. // Estimate the optimum number of times to use the argument reduction.
len = x.d.length;
if (len < 32) { if (len < 32) {
k = Math.ceil(len / 3); k = Math.ceil(len / 3);
y = (1 / tinyPow(4, k)).toString(); y = (1 / tinyPow(4, k)).toString();
@ -3660,7 +3662,9 @@ function sine(Ctor, x) {
var k, var k,
len = x.d.length; 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) // 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) // 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 * sinh
* sqrt * sqrt
* sub * sub
* sum
* tan * tan
* tanh * tanh
* trunc * trunc
@ -4399,6 +4404,7 @@ function clone(obj) {
Decimal.sinh = sinh; // ES6 Decimal.sinh = sinh; // ES6
Decimal.sqrt = sqrt; Decimal.sqrt = sqrt;
Decimal.sub = sub; Decimal.sub = sub;
Decimal.sum = sum;
Decimal.tan = tan; Decimal.tan = tan;
Decimal.tanh = tanh; // ES6 Decimal.tanh = tanh; // ES6
Decimal.trunc = trunc; // 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 * Return a new Decimal whose value is the tangent of `x`, rounded to `precision` significant
* digits using rounding mode `rounding`. * digits using rounding mode `rounding`.

View File

@ -102,6 +102,7 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
<li><a href="#Dsinh" >sinh</a></li> <li><a href="#Dsinh" >sinh</a></li>
<li><a href="#Dsqrt" >sqrt</a></li> <li><a href="#Dsqrt" >sqrt</a></li>
<li><a href="#Dsub" >sub</a></li> <li><a href="#Dsub" >sub</a></li>
<li><a href="#Dsum" >sum</a></li>
<li><a href="#Dtan" >tan</a></li> <li><a href="#Dtan" >tan</a></li>
<li><a href="#Dtanh" >tanh</a></li> <li><a href="#Dtanh" >tanh</a></li>
<li><a href="#Dtrunc" >trunc</a></li> <li><a href="#Dtrunc" >trunc</a></li>
@ -601,7 +602,7 @@ a.equals(b) // true</pre>
<h5 id="Dmax"> <h5 id="Dmax">
max<code class='inset'>.max([x [, y, ...]]) <i>&rArr; Decimal</i></code> max<code class='inset'>.max(x [, y, ...]) <i>&rArr; Decimal</i></code>
</h5> </h5>
<p> <p>
<code>x</code>: <i>number|string|Decimal</i><br /> <code>x</code>: <i>number|string|Decimal</i><br />
@ -613,7 +614,7 @@ a.equals(b) // true</pre>
<h5 id="Dmin"> <h5 id="Dmin">
min<code class='inset'>.min([x [, y, ...]]) <i>&rArr; Decimal</i></code> min<code class='inset'>.min(x [, y, ...]) <i>&rArr; Decimal</i></code>
</h5> </h5>
<p> <p>
<code>x</code>: <i>number|string|Decimal</i><br /> <code>x</code>: <i>number|string|Decimal</i><br />
@ -833,7 +834,7 @@ a.equals(b) // true</pre>
<h5 id="Dsub">sub<code class='inset'>.sub(x, y) <i>&rArr; Decimal</i></code></h5> <h5 id="Dsub">sub<code class='inset'>.sub(x, y) <i>&rArr; Decimal</i></code></h5>
<p> <p>
<code>x</code>: <i>number|string|Decimal</i><br /> <code>x</code>: <i>number|string|Decimal</i><br />
<code>y</code>: <i>number|string|Decimal</i> <code>y</code>: <i>number|string|Decimal</i>
@ -845,7 +846,26 @@ a.equals(b) // true</pre>
<h5 id="Dtan">tan<code class='inset'>.tan(x) <i>&rArr; Decimal</i></code></h5> <h5 id="Dsum">sum<code class='inset'>.sum(x [, y, ...]) <i>&rArr; Decimal</i></code></h5>
<p>
<code>x</code>: <i>number|string|Decimal</i><br />
<code>y</code>: <i>number|string|Decimal</i>
</p>
<p>
Returns a new Decimal whose value is the sum of the <code>arguments</code>,
rounded to <a href='#precision'><code>precision</code></a> significant digits using
rounding mode <a href='#rounding'><code>rounding</code></a>.<br />
Only the result is rounded, not the intermediate summations.
</p>
<pre>
x = 5
y = '16'
z = new Decimal(-11)
Decimal.sum(x, y, z) // '10'</pre>
<h5 id="Dtan">tan<code class='inset'>.tan(x) <i>&rArr; Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p> <p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#tan'>tangent</a></code>.</p> <p>See <code><a href='#tan'>tangent</a></code>.</p>
<pre>a = Decimal.tan(x) <pre>a = Decimal.tan(x)

64
test/modules/sum.js Normal file
View File

@ -0,0 +1,64 @@
if (typeof T === 'undefined') require('../setup');
T('sum', function () {
var expected;
function t() {
T.assertEqualDecimal(expected, Decimal.sum.apply(Decimal, arguments));
}
expected = new Decimal(0);
t('0');
t('0', new Decimal(0));
t(1, 0, '-1');
t(0, new Decimal('-10'), 0, 0, 0, 0, 0, 10);
t(11, -11);
t(1, '2', new Decimal(3), new Decimal('4'), -10);
t(new Decimal(-10), '9', new Decimal(0.01), 0.99);
expected = new Decimal(10);
t('10');
t('0', new Decimal('10'));
t(10, 0);
t(0, 0, 0, 0, 0, 0, 10);
t(11, -1);
t(1, '2', new Decimal(3), new Decimal('4'));
t('9', new Decimal(0.01), 0.99);
expected = new Decimal(600);
t(100, 200, 300);
t('100', '200', '300');
t(new Decimal(100), new Decimal(200), new Decimal(300));
t(100, '200', new Decimal(300));
t(99.9, 200.05, 300.05);
expected = new Decimal(NaN);
t(NaN);
t('1', NaN);
t(100, 200, NaN);
t(NaN, 0, '9', new Decimal(0), 11, Infinity);
t(0, new Decimal('-Infinity'), '9', new Decimal(NaN), 11);
t(4, '-Infinity', 0, '9', new Decimal(0), Infinity, 2);
expected = new Decimal(Infinity);
t(Infinity);
t(1, '1e10000000000000000000000000000000000000000', '4');
t(100, 200, 'Infinity');
t(0, new Decimal('Infinity'), '9', new Decimal(0), 11);
t(0, '9', new Decimal(0), 11, Infinity);
t(4, new Decimal(Infinity), 0, '9', new Decimal(0), Infinity, 2);
expected = new Decimal(-Infinity);
t(-Infinity);
t(1, '-1e10000000000000000000000000000000000000000', '4');
t(100, 200, '-Infinity');
t(0, new Decimal('-Infinity'), '9', new Decimal(0), 11);
t(0, '9', new Decimal(0), 11, -Infinity);
t(4, new Decimal(-Infinity), 0, '9', new Decimal(0), -Infinity, 2);
});

View File

@ -59,6 +59,7 @@
'sin', 'sin',
'sinh', 'sinh',
'sqrt', 'sqrt',
'sum',
'tan', 'tan',
'tanh', 'tanh',
'times', 'times',

View File

@ -46,6 +46,7 @@ console.log('\n Testing decimal.js\n');
'sin', 'sin',
'sinh', 'sinh',
'sqrt', 'sqrt',
'sum',
'tan', 'tan',
'tanh', 'tanh',
'times', 'times',