https://github.com/sindresorhus/math-sum
Sum numbers
https://github.com/sindresorhus/math-sum
Last synced: 28 days ago
JSON representation
Sum numbers
- Host: GitHub
- URL: https://github.com/sindresorhus/math-sum
- Owner: sindresorhus
- License: mit
- Created: 2015-07-05T16:16:05.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2023-11-04T18:23:07.000Z (about 2 years ago)
- Last Synced: 2024-04-14T06:09:33.306Z (over 1 year ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 9
- Watchers: 7
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
- awesome-nodejs-precise - math-sum - sum .svg?style=social&label=Star&maxAge=2592000?style=flat-square)]() | Sum numbers. | (Packages / Math)
- awesome-nodejs-cn - math-sum - Sum numbers. (Number / Math)
README
# math-sum
> Sum numbers
## Install
```sh
npm install math-sum
```
## Usage
```js
import sum from 'math-sum';
sum(5, 5);
//=> 10
sum([1, 2, 3, 4]);
//=> 10
```
### BigInt
```js
import sum from 'math-sum';
sum.bigInt(5, 5);
//=> 10n
sum.bigInt([1, 2, 3, 4]);
//=> 10n
sum.bigInt(1, 2n, 3);
//=> 6n
const largeNumber = 9_007_199_254_740_993n; // Number.MAX_SAFE_INTEGER + 2
sum.bigInt(largeNumber, largeNumber);
//=> 18_014_398_509_481_986n
```
The `sum.bigInt()` method accepts both regular numbers and BigInts and always returns a BigInt. Regular numbers are converted to BigInts automatically. This is useful when you need to sum numbers that exceed JavaScript's safe integer range.
> [!NOTE]
> The method throws a `RangeError` if any value is a non-integer number (e.g., `3.14`).
## Related
- [math-average](https://github.com/sindresorhus/math-average) - Get the average of numbers
- [math-clamp](https://github.com/sindresorhus/math-clamp) - Clamp a number