For my book, How JavaScript Works,
I created a module, big_integer.js
, that makes and performs operations on integers of any size.
Since then, the ECMAScript standard has acquired a BigInt
type, which, being native,
can run faster and consume less memory.
I have revised big_integer.js
to use BigInt
as its underlying implementation.
I also created two other modules, big_float.js
and big_rational.js
.
The big_float.js
module uses big_integer.js
to implement decimal floating point,
similar to DEC64
but with virtually unlimited range and resolution.
It benefits significantly from the big_integer.js
speed up.
Decimal floating point is much better than the binary floating point that JavaScript provides because it can exactly
represent decimal fractions, including money.
The big_float.js
module can exactly represent every value that binary floating point can,
but binary floating point can not exactly represent 0.01
.
The world should stop using binary floating point.
The big_rational.js
module can exactly represent everything that big_float.js
can,
and can also exactly represent virtually all rational fractions, starting with 1/3
.
Rational arithmetic is truly awesome, but there is a potential problem: The numerators and denominators
can grow quickly in some computations, creating a big performance problem. So for most purposes,
decimal floating point suffices. But if you want to make the world's best, most accurate calculator,
nothing is better than big rationals.
I am also releasing the tests for those modules. The tests demonstrate the use of JSCheck.