A plugin for babel to transform `x * y` into something like `JSBI.multiply(x, y)` to support bigints.
npm install babel-plugin-transform-bigintBigInts:
javascript
const a = BigInt(Number.MAX_SAFE_INTEGER);
const b = 2n;
a + b;
a - b;
a * b;
a / b;
a % b;
a ** b;
a << b;
a >> b;
a & b;
a | b;
a ^ b;
-a;
~a;
a === b;
a < b;
a <= b;
a > b;
a >= b;
a.toString();
Number(a);
`
Compiled output using JSBI:
`
const a = JSBI.BigInt(Number.MAX_SAFE_INTEGER);
const b = JSBI.BigInt(2);
JSBI.add(a, b);
JSBI.subtract(a, b);
JSBI.multiply(a, b);
JSBI.divide(a, b);
JSBI.remainder(a, b);
JSBI.exponentiate(a, b);
JSBI.leftShift(a, b);
JSBI.signedRightShift(a, b);
JSBI.bitwiseAnd(a, b);
JSBI.bitwiseOr(a, b);
JSBI.bitwiseXor(a, b);
JSBI.unaryMinus(a);
JSBI.bitwiseNot(a);
JSBI.equal(a, b);
JSBI.lessThan(a, b);
JSBI.lessThanOrEqual(a, b);
JSBI.greaterThan(a, b);
JSBI.greaterThanOrEqual(a, b);
a.toString();
JSBI.toNumber(a);
`
Playground:
===========
1. Open https://babeljs.io/en/repl
2. Turn off all presets.
3. "Add plugin" @babel/babel-plugin-syntax-bigint
4. "Add plugin" babel-plugin-transform-bigint
¡ It is buggy !
Example:
========
1. Create a folder "example".
2. Create a file test.js:
`javascript
// floor(log2(n)), n >= 1
function ilog2(n) {
let i = 0n;
while (n >= 2n(2ni)) {
i += 1n;
}
let e = 0n;
let t = 1n;
while (i >= 0n) {
let b = 2n**i;
if (n >= t 2n*b) {
t = 2n*b;
e += b;
}
i -= 1n;
}
return e;
}
// floor(sqrt(S)), S >= 0, https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
function sqrt(S) {
let e = ilog2(S);
if (e < 2n) {
return 1n;
}
let f = e / 4n + 1n;
let x = (sqrt(S / 2n(f 2n)) + 1n) 2nf;
let xprev = x + 1n;
while (xprev > x) {
xprev = x;
x = (x + S / x) / 2n;
}
return xprev;
}
function squareRoot(value, decimalDigits) {
return (sqrt(BigInt(value) 10n(BigInt(decimalDigits) 2n + 2n)) + 5n).toString();
}
`
3. Use babel:
`sh
npm init
npm install --save https://github.com/Yaffle/babel-plugin-transform-bigint
npm install --save-dev @babel/core @babel/cli
npm install jsbi --save
npm install --global http-server
npx babel --plugins=babel-plugin-transform-bigint test.js > test-transformed.js
http-server -p 8081
`
4. Comment out the next line in test-transformed.js:
`javascript
import JSBI from "./jsbi.mjs";
`
5. Create a file test.html.
`html
`
6. Open http://127.0.0.1:8081/test.html` in a web browser.