1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2026-01-20 07:49:24 +00:00
This commit is contained in:
Hunter Wilhelm 2025-12-20 17:24:30 -07:00 committed by GitHub
commit cb15300ecb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 23 additions and 8 deletions

1
decimal.d.ts vendored
View File

@ -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;

1
decimal.global.d.ts vendored
View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -861,22 +861,25 @@ a.equals(b) // true</pre>
<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>

View File

@ -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);
@ -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);
@ -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));
});