Skip to content

Commit 66a21ee

Browse files
committed
#100 Add Decimal.sum method
1 parent 0e0dcae commit 66a21ee

File tree

6 files changed

+149
-11
lines changed

6 files changed

+149
-11
lines changed

decimal.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,9 +2629,9 @@
26292629
*/
26302630
function cosine(Ctor, x) {
26312631
var k, len, y;
2632-
2632+
26332633
if (x.isZero()) return x;
2634-
2634+
26352635
// Argument reduction: cos(4x) = 8*(cos^4(x) - cos^2(x)) + 1
26362636
// i.e. cos(x) = 8*(cos^4(x/4) - cos^2(x/4)) + 1
26372637

@@ -3665,10 +3665,10 @@
36653665
function sine(Ctor, x) {
36663666
var k,
36673667
len = x.d.length;
3668-
3668+
36693669
if (len < 3) {
36703670
return x.isZero() ? x : taylorSeries(Ctor, 2, x, x);
3671-
}
3671+
}
36723672

36733673
// Argument reduction: sin(5x) = 16*sin^5(x) - 20*sin^3(x) + 5*sin(x)
36743674
// i.e. sin(x) = 16*sin^5(x/5) - 20*sin^3(x/5) + 5*sin(x/5)
@@ -3959,6 +3959,7 @@
39593959
* sinh
39603960
* sqrt
39613961
* sub
3962+
* sum
39623963
* tan
39633964
* tanh
39643965
* trunc
@@ -4407,6 +4408,7 @@
44074408
Decimal.sinh = sinh; // ES6
44084409
Decimal.sqrt = sqrt;
44094410
Decimal.sub = sub;
4411+
Decimal.sum = sum;
44104412
Decimal.tan = tan;
44114413
Decimal.tanh = tanh; // ES6
44124414
Decimal.trunc = trunc; // ES6
@@ -4801,6 +4803,28 @@
48014803
}
48024804

48034805

4806+
/*
4807+
* Return a new Decimal whose value is the sum of the arguments, rounded to `precision`
4808+
* significant digits using rounding mode `rounding`.
4809+
*
4810+
* Only the result is rounded, not the intermediate calculations.
4811+
*
4812+
* arguments {number|string|Decimal}
4813+
*
4814+
*/
4815+
function sum() {
4816+
var i = 0,
4817+
args = arguments,
4818+
x = new this(args[i]);
4819+
4820+
external = false;
4821+
for (; x.s && ++i < args.length;) x = x.plus(args[i]);
4822+
external = true;
4823+
4824+
return finalise(x, this.precision, this.rounding);
4825+
}
4826+
4827+
48044828
/*
48054829
* Return a new Decimal whose value is the tangent of `x`, rounded to `precision` significant
48064830
* digits using rounding mode `rounding`.

decimal.mjs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,13 +2624,15 @@ function convertBase(str, baseIn, baseOut) {
26242624
*
26252625
*/
26262626
function cosine(Ctor, x) {
2627-
var k, y,
2628-
len = x.d.length;
2627+
var k, len, y;
2628+
2629+
if (x.isZero()) return x;
26292630

26302631
// Argument reduction: cos(4x) = 8*(cos^4(x) - cos^2(x)) + 1
26312632
// i.e. cos(x) = 8*(cos^4(x/4) - cos^2(x/4)) + 1
26322633

26332634
// Estimate the optimum number of times to use the argument reduction.
2635+
len = x.d.length;
26342636
if (len < 32) {
26352637
k = Math.ceil(len / 3);
26362638
y = (1 / tinyPow(4, k)).toString();
@@ -3660,7 +3662,9 @@ function sine(Ctor, x) {
36603662
var k,
36613663
len = x.d.length;
36623664

3663-
if (len < 3) return taylorSeries(Ctor, 2, x, x);
3665+
if (len < 3) {
3666+
return x.isZero() ? x : taylorSeries(Ctor, 2, x, x);
3667+
}
36643668

36653669
// Argument reduction: sin(5x) = 16*sin^5(x) - 20*sin^3(x) + 5*sin(x)
36663670
// 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) {
39513955
* sinh
39523956
* sqrt
39533957
* sub
3958+
* sum
39543959
* tan
39553960
* tanh
39563961
* trunc
@@ -4399,6 +4404,7 @@ function clone(obj) {
43994404
Decimal.sinh = sinh; // ES6
44004405
Decimal.sqrt = sqrt;
44014406
Decimal.sub = sub;
4407+
Decimal.sum = sum;
44024408
Decimal.tan = tan;
44034409
Decimal.tanh = tanh; // ES6
44044410
Decimal.trunc = trunc; // ES6
@@ -4793,6 +4799,28 @@ function sub(x, y) {
47934799
}
47944800

47954801

4802+
/*
4803+
* Return a new Decimal whose value is the sum of the arguments, rounded to `precision`
4804+
* significant digits using rounding mode `rounding`.
4805+
*
4806+
* Only the result is rounded, not the intermediate calculations.
4807+
*
4808+
* arguments {number|string|Decimal}
4809+
*
4810+
*/
4811+
function sum() {
4812+
var i = 0,
4813+
args = arguments,
4814+
x = new this(args[i]);
4815+
4816+
external = false;
4817+
for (; x.s && ++i < args.length;) x = x.plus(args[i]);
4818+
external = true;
4819+
4820+
return finalise(x, this.precision, this.rounding);
4821+
}
4822+
4823+
47964824
/*
47974825
* Return a new Decimal whose value is the tangent of `x`, rounded to `precision` significant
47984826
* digits using rounding mode `rounding`.

doc/API.html

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
<li><a href="#Dsinh" >sinh</a></li>
103103
<li><a href="#Dsqrt" >sqrt</a></li>
104104
<li><a href="#Dsub" >sub</a></li>
105+
<li><a href="#Dsum" >sum</a></li>
105106
<li><a href="#Dtan" >tan</a></li>
106107
<li><a href="#Dtanh" >tanh</a></li>
107108
<li><a href="#Dtrunc" >trunc</a></li>
@@ -601,7 +602,7 @@ <h5 id="Dlog10">log10<code class='inset'>.log10(x) <i>&rArr; Decimal</i></code><
601602

602603

603604
<h5 id="Dmax">
604-
max<code class='inset'>.max([x [, y, ...]]) <i>&rArr; Decimal</i></code>
605+
max<code class='inset'>.max(x [, y, ...]) <i>&rArr; Decimal</i></code>
605606
</h5>
606607
<p>
607608
<code>x</code>: <i>number|string|Decimal</i><br />
@@ -613,7 +614,7 @@ <h5 id="Dmax">
613614

614615

615616
<h5 id="Dmin">
616-
min<code class='inset'>.min([x [, y, ...]]) <i>&rArr; Decimal</i></code>
617+
min<code class='inset'>.min(x [, y, ...]) <i>&rArr; Decimal</i></code>
617618
</h5>
618619
<p>
619620
<code>x</code>: <i>number|string|Decimal</i><br />
@@ -833,7 +834,7 @@ <h5 id="Dsqrt">sqrt<code class='inset'>.sqrt(x) <i>&rArr; Decimal</i></code></h5
833834

834835

835836

836-
<h5 id="Dsub">sub<code class='inset'>.sub(x, y) <i>&rArr; Decimal</i></code></h5>
837+
<h5 id="Dsub">sub<code class='inset'>.sub(x, y) <i>&rArr; Decimal</i></code></h5>
837838
<p>
838839
<code>x</code>: <i>number|string|Decimal</i><br />
839840
<code>y</code>: <i>number|string|Decimal</i>
@@ -845,7 +846,26 @@ <h5 id="Dsub">sub<code class='inset'>.sub(x, y) <i>&rArr; Decimal</i></code></h5
845846

846847

847848

848-
<h5 id="Dtan">tan<code class='inset'>.tan(x) <i>&rArr; Decimal</i></code></h5>
849+
<h5 id="Dsum">sum<code class='inset'>.sum(x [, y, ...]) <i>&rArr; Decimal</i></code></h5>
850+
<p>
851+
<code>x</code>: <i>number|string|Decimal</i><br />
852+
<code>y</code>: <i>number|string|Decimal</i>
853+
</p>
854+
<p>
855+
Returns a new Decimal whose value is the sum of the <code>arguments</code>,
856+
rounded to <a href='#precision'><code>precision</code></a> significant digits using
857+
rounding mode <a href='#rounding'><code>rounding</code></a>.<br />
858+
Only the result is rounded, not the intermediate summations.
859+
</p>
860+
<pre>
861+
x = 5
862+
y = '16'
863+
z = new Decimal(-11)
864+
Decimal.sum(x, y, z) // '10'</pre>
865+
866+
867+
868+
<h5 id="Dtan">tan<code class='inset'>.tan(x) <i>&rArr; Decimal</i></code></h5>
849869
<p><code>x</code>: <i>number|string|Decimal</i></p>
850870
<p>See <code><a href='#tan'>tangent</a></code>.</p>
851871
<pre>a = Decimal.tan(x)

test/modules/sum.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
if (typeof T === 'undefined') require('../setup');
2+
3+
T('sum', function () {
4+
var expected;
5+
6+
function t() {
7+
T.assertEqualDecimal(expected, Decimal.sum.apply(Decimal, arguments));
8+
}
9+
10+
expected = new Decimal(0);
11+
12+
t('0');
13+
t('0', new Decimal(0));
14+
t(1, 0, '-1');
15+
t(0, new Decimal('-10'), 0, 0, 0, 0, 0, 10);
16+
t(11, -11);
17+
t(1, '2', new Decimal(3), new Decimal('4'), -10);
18+
t(new Decimal(-10), '9', new Decimal(0.01), 0.99);
19+
20+
expected = new Decimal(10);
21+
22+
t('10');
23+
t('0', new Decimal('10'));
24+
t(10, 0);
25+
t(0, 0, 0, 0, 0, 0, 10);
26+
t(11, -1);
27+
t(1, '2', new Decimal(3), new Decimal('4'));
28+
t('9', new Decimal(0.01), 0.99);
29+
30+
expected = new Decimal(600);
31+
32+
t(100, 200, 300);
33+
t('100', '200', '300');
34+
t(new Decimal(100), new Decimal(200), new Decimal(300));
35+
t(100, '200', new Decimal(300));
36+
t(99.9, 200.05, 300.05);
37+
38+
expected = new Decimal(NaN);
39+
40+
t(NaN);
41+
t('1', NaN);
42+
t(100, 200, NaN);
43+
t(NaN, 0, '9', new Decimal(0), 11, Infinity);
44+
t(0, new Decimal('-Infinity'), '9', new Decimal(NaN), 11);
45+
t(4, '-Infinity', 0, '9', new Decimal(0), Infinity, 2);
46+
47+
expected = new Decimal(Infinity);
48+
49+
t(Infinity);
50+
t(1, '1e10000000000000000000000000000000000000000', '4');
51+
t(100, 200, 'Infinity');
52+
t(0, new Decimal('Infinity'), '9', new Decimal(0), 11);
53+
t(0, '9', new Decimal(0), 11, Infinity);
54+
t(4, new Decimal(Infinity), 0, '9', new Decimal(0), Infinity, 2);
55+
56+
expected = new Decimal(-Infinity);
57+
58+
t(-Infinity);
59+
t(1, '-1e10000000000000000000000000000000000000000', '4');
60+
t(100, 200, '-Infinity');
61+
t(0, new Decimal('-Infinity'), '9', new Decimal(0), 11);
62+
t(0, '9', new Decimal(0), 11, -Infinity);
63+
t(4, new Decimal(-Infinity), 0, '9', new Decimal(0), -Infinity, 2);
64+
});

test/test.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
'sin',
6060
'sinh',
6161
'sqrt',
62+
'sum',
6263
'tan',
6364
'tanh',
6465
'times',

test/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ console.log('\n Testing decimal.js\n');
4646
'sin',
4747
'sinh',
4848
'sqrt',
49+
'sum',
4950
'tan',
5051
'tanh',
5152
'times',

0 commit comments

Comments
 (0)