Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yaffle/biginteger
Yet another implementaion of arbitrary-precision integers in pure JavaScript. Small. Well tested.
https://github.com/yaffle/biginteger
Last synced: 1 day ago
JSON representation
Yet another implementaion of arbitrary-precision integers in pure JavaScript. Small. Well tested.
- Host: GitHub
- URL: https://github.com/yaffle/biginteger
- Owner: Yaffle
- License: unlicense
- Created: 2014-02-01T10:14:22.000Z (almost 11 years ago)
- Default Branch: gh-pages
- Last Pushed: 2024-10-11T18:24:52.000Z (about 1 month ago)
- Last Synced: 2024-10-31T03:05:13.063Z (15 days ago)
- Language: JavaScript
- Homepage:
- Size: 1.4 MB
- Stars: 42
- Watchers: 5
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
BigInteger
==========Yet another BigInteger class in JavaScript
This library performs arithmetic operations on integers of arbitrary size.To use it from a web browser:
``````
To use it from the node.js:
```
npm install Yaffle/BigInteger
```
Then:
```
var BigInteger = require("js-big-integer").BigInteger;
```The API is terrible, but small integers are stored as primitive numbers, so operations on small integers are faster.
The API was updated to match the API provided by https://github.com/GoogleChromeLabs/jsbiOperation | `BigInteger` | `Number` | `BigInt` (https://github.com/tc39/proposal-bigint)
-----------------------|--------------------------------------|----------------------------------|---------------------------------------------------
Conversion from String | `BigInteger.BigInt(string)` | `Number(string)` | `BigInt(string)`
Conversion from Number | `BigInteger.BigInt(number)` | N/A | `BigInt(number)`
Conversion to String | `a.toString(radix)` | `a.toString(radix)` | `a.toString(radix)`
Conversion to Number | `a.toNumber()` | N/A | `Number(bigint)`
Addition | `BigInteger.add(a, b)` | `a + b` | `a + b`
Subtraction | `BigInteger.subtract(a, b)` | `a - b` | `a - b`
Multiplication | `BigInteger.multiply(a, b)` | `0 + a * b` | `a * b`
Division | `BigInteger.divide(a, b)` | `0 + Math.trunc(a / b)` | `a / b`
Remainder | `BigInteger.remainder(a, b)` | `0 + a % b` | `a % b`
Exponentiation | `BigInteger.exponentiate(a, b)` | `0 + a**b` | `a**b`
Negation | `BigInteger.unaryMinus(a)` | `0 - a` | `-a`
Comparison | `BigInteger.equal(a, b)` | `a === b` | `a === b`
... | `BigInteger.lessThan(a, b)` | `a < b` | `a < b`
... | `BigInteger.greaterThan(a, b)` | `a > b` | `a > b`
... | `BigInteger.notEqual(a, b)` | `a !== b` | `a !== b`
... | `BigInteger.lessThanOrEqual(a, b)` | `a <= b` | `a <= b`
... | `BigInteger.greaterThanOrEqual(a, b)`| `a >= b` | `a >= b`
Signed Right Shift | `BigInteger.signedRightShift(a, b)` | `a >> b` | `a >> b`
Left Shift | `BigInteger.leftShift(a, b)` | `a << b` | `a << b`Example
=======
```javascriptvar factorial = function (n) {
var result = BigInteger.BigInt(1);
var i = 0;
while (++i <= n) {
result = BigInteger.multiply(result, BigInteger.BigInt(i));
}
return result;
};console.log(factorial(30).toString(10));
```
Other pure JavaScript implementations:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.Benchmark: