charto / bigfloat

Fast arbitrary precision math library for computational geometry.
MIT License
74 stars 4 forks source link

bigfloat

build status npm version dependency status install size license

bigfloat is a fast arbitrary precision math library optimized for computational geometry and geoinformatics. It provides binary floating point:

without ever losing any significant bits. Numbers are immutable in the above operations, so they return a new BigFloat. For efficiency, the following methods instead destructively change the value:

Division is deliberately unsupported, because its result is generally inexact. Please multiply by the reciprocal or use rational numbers instead. Note that floating point values in numerators and denominators are perfectly cromulent. If you need square roots or transcendental functions, use some other library.

There are two versions of the class, BigFloat32 and BigFloat53 with the same API but completely different internals as follows:

BigFloat32

BigFloat53

In both versions the least significant limb / component is stored first, because basic algorithms for arithmetic operations progress from the least to most significant digit while propagating carry. If carry causes the output to grow, adding a new limb at the end of the array is faster than adding it in the beginning.

TL;DR: Use BigFloat32 for long operations between arbitrary precision floats, portability and to avoid under / overflow. Use BigFloat53 for short calculations with many ordinary JavaScript numbers as inputs.

You may want to test with both to compare their speed and see if you run into overflow or any floating point portability issues on mobile platforms.

Optimization

In any longer iterated calculations involving multiplication, truncate should be called regularly because otherwise significant bits will keep accumulating. For example, squaring a number doubles the number of bits at every step, easily turning an algorithm with linear complexity into a quadratic one (both in speed and space).

To avoid surprises, the basic operations allocate new objects for storing results. A second parameter can be given, with a result BigFloat object of the same type (32 or 53). Its contents will be destructively overwritten with the result, to save a memory allocation. This avoids garbage collection related slowdowns in longer calculations.

Some care is needed in re-using temporary variables, because inputs cannot be simultaneously used as results:

x.add(y, w).sub(z, w)

fails because in the subtraction, w is both the subtrahend and the difference.

Existing objects can also be re-initialized with:

Additionally, BigFloat53 objects support initialization from results of operations between two JavaScript numbers:

These use very fast double double arithmetic (error free transformations).

Speed

It's fast, see the Mandelbrot benchmark. Here's some example results:

Native JavaScript IEEE 754:
████████████████████████████████ // ██ 80000 frames per minute

bigfloat:
████████████████████████████ 141 frames per minute

bignumber.js:
██████████ 48 frames per minute

big.js:
███████ 35 frames per minute

Getting started

git clone https://github.com/charto/bigfloat.git node_modules/bigfloat
cd node_modules/bigfloat && npm install
cd ../..
node

OR

npm install bigfloat
node

THEN

x = Math.pow(2, 53);
console.log(x + 1 - x); // Prints 0

BigFloat32 = require('bigfloat').BigFloat32;
console.log(new BigFloat32(x).add(1).sub(x).toString()); // Prints 1

License

The MIT License

Copyright (c) 2015- BusFaster Ltd

The paper doc/robustr.pdf is copyright JR Shewchuk and licenced as detailed inside under "About this Report".