Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions decimal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export declare class Decimal {
static sqrt(n: Decimal.Value): Decimal;
static sub(x: Decimal.Value, y: Decimal.Value): Decimal;
static sum(...n: Decimal.Value[]): Decimal;
static sum(n: Decimal.Value[]): Decimal;
static tan(n: Decimal.Value): Decimal;
static tanh(n: Decimal.Value): Decimal;
static trunc(n: Decimal.Value): Decimal;
Expand Down
1 change: 1 addition & 0 deletions decimal.global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ export declare class Decimal {
static sqrt(n: DecimalValue): Decimal;
static sub(x: DecimalValue, y: DecimalValue): Decimal;
static sum(...n: Decimal.Value[]): Decimal;
static sum(n: Decimal.Value[]): Decimal;
static tan(n: DecimalValue): Decimal;
static tanh(n: DecimalValue): Decimal;
static trunc(n: DecimalValue): Decimal;
Expand Down
3 changes: 2 additions & 1 deletion decimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4856,11 +4856,12 @@
* Only the result is rounded, not the intermediate calculations.
*
* arguments {number|string|bigint|Decimal}
* arguments {(number|string|bigint|Decimal)[]}
*
*/
function sum() {
var i = 0,
args = arguments,
args = arguments.length === 1 && Array.isArray(arguments[0]) ? arguments[0] : arguments,
x = new this(args[i]);

external = false;
Expand Down
3 changes: 2 additions & 1 deletion decimal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4851,11 +4851,12 @@ function sub(x, y) {
* Only the result is rounded, not the intermediate calculations.
*
* arguments {number|string|bigint|Decimal}
* arguments {(number|string|bigint|Decimal)[]}
*
*/
function sum() {
var i = 0,
args = arguments,
args = arguments.length === 1 && Array.isArray(arguments[0]) ? arguments[0] : arguments,
x = new this(args[i]);

external = false;
Expand Down
15 changes: 9 additions & 6 deletions doc/API.html
Original file line number Diff line number Diff line change
Expand Up @@ -861,22 +861,25 @@ <h5 id="Dsub">sub<code class='inset'>.sub(x, y) <i>&rArr; Decimal</i></code></h5



<h5 id="Dsum">sum<code class='inset'>.sum(x [, y, ...]) <i>&rArr; Decimal</i></code></h5>
<h5 id="Dsum">sum<code class='inset'>.sum(x [, y, ...]) <i>&rArr; Decimal</i></code><br />
<code class='inset'>.sum([x, y, ...]) <i>&rArr; Decimal</i></code></h5>
<p>
<code>x</code>: <i>number|string|bigInt|Decimal</i><br />
<code>y</code>: <i>number|string|bigInt|Decimal</i>
<code>y</code>: <i>number|string|bigInt|Decimal</i><br />
<code>[x, y, ...]</code>: <i>Array&lt;number|string|bigInt|Decimal&gt;</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 />
Returns a new Decimal whose value is the sum of the arguments (either passed as
variadic arguments or as an array), 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>
Decimal.sum(x, y, z) // '10'
Decimal.sum([x, y, z]) // '10'</pre>



Expand Down
8 changes: 8 additions & 0 deletions test/modules/sum.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ T('sum', function () {
t(1, 0, '-1');
t(0, new Decimal('-10'), 0, 0, 0, 0, 0, 10);
t(11, -11);
t([11, -11]);
t([0]);
t(1, '2', new Decimal(3), new Decimal('4'), -10);
t(new Decimal(-10), '9', new Decimal(0.01), 0.99);

Expand All @@ -24,6 +26,7 @@ T('sum', function () {
t(10, 0);
t(0, 0, 0, 0, 0, 0, 10);
t(11, -1);
t([11, -1]);
t(1, '2', new Decimal(3), new Decimal('4'));
t('9', new Decimal(0.01), 0.99);

Expand Down Expand Up @@ -61,4 +64,9 @@ T('sum', function () {
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);
t([4, new Decimal(-Infinity), 0, '9', new Decimal(0), -Infinity, 2]);

expected = new Decimal(100e3);

t(Array.from({ length: 100e3 }).fill(1));
});