1.1.0 • Published 3 years ago

double.js v1.1.0

Weekly downloads
8
License
MIT
Repository
github
Last release
3 years ago

double.js bundlephobia Codecov

Floating point expansion with 31 accurate decimal digits (106 bits), also known as double-double arithmetic or emulated float128. This library can be useful for fast calculation with extended precision. For example in orbital mechanics, computational geometry and numerically unstable algorithms such as performing triangulation, polygon clipping, inverting matrix and finding differentials.

Algorithm

Number stored as unevaluated sum of two javascript float numbers and uses error-free arithmetic algorithms. This brings accuracy and significant increase in performance in comparison to digit-wise approach, because this float arithmetic is implemented in hardware. Note that there are no theoretical limitations to javascript language since ECMAScript uses 64 bit IEEE 754 with round-to-nearest-even after each operation.

Benchmark

npm.io

You can check quality, performance and correctness of double.js library in your browser.

Usage

Include double.js script to webpage or install npm package. Here some basic examples

// example with ES6 modules, also you can use ES5
import { Double } from 'double.js';

// '0.3' - '0.1' == 0.2
console.log(new Double('0.3').sub(new Double('0.1')).toNumber());

// L = sqrt(a^2 + 10)
let L = a.sqr().add(10).sqrt();

// S(r) = 4/3 * PI * r^3
const S = (r) => new Double('4.1887902047863909846168578443726').mul(r.pown(3));

// f'(x) = (f(x+h) - f(x)) / h;
let dF = (x) => F(x.add(h)).sub(F(x)).div(h);

// |f'(x)| < 1 ? print(x)
if (dF(x).abs().lt(1)) { console.log(x.toExponential()); }

Further API details you can find in wiki page and check it in sandbox. Be careful when initializing a new floats, for example new Double(0.1) is ok for integer numbers, but you should use new Double('0.1') to get correct results for fractional numburs. All double-double arithmetic functions are accurate and tested, say me if you find something strange.

WebAssembly version

To get speed improvement with wasm, you need to write your entire algorithm with it, because Js<->Wasm interop is too heavy. For example I got x3 boost in Chrome, x3.5 in Safari and x7 in Firefox for mandelbrot set algo, but with hardcoded global variables.

WebGL/WebGPU versions

Just copy/paste the code with MIT copyright. (2do: testing)

Special thanks

To Jeffrey Sarnoff for help me with books and algorithms. Sergey Yanovich for fixing issues with toExponential(). To Max Graey for AssemblyScript remarks. To Yaffle for fixing benchmark.

Want more functions?

If you want more functions (trigonometric for example) open an issue.

References

  1. J.-M. Muller, etc. Tight and rigourous error bounds for basic building blocks of double-word arithmetic., 2017. [PDF]
  2. J.-M. Muller, N. Brisebarre, F. deDinechin, etc. Handbook of Floating-Point Arithmetic, Chapter 14, 2010.
  3. Theodorus Dekker. A floating-point technique for extending the available precision, 1971. [Viewer]
  4. David Monniaux The pitfalls of verifying floating-point computations, 2008 [PDF]
  5. Yozo Hida, Xiaoye Li, David Bailey. Library for Double-Double and Quad-Double Arithmetic, 2000. [PDF]
  6. Christoph Lauter Basic building blocks for a triple-double intermediate format, 2006. [PDF]
1.1.0

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.5.16

4 years ago

0.5.15

4 years ago

0.5.14

4 years ago

0.5.13

4 years ago

0.5.12

4 years ago

0.5.11

4 years ago

0.5.10

4 years ago

0.5.9

4 years ago

0.5.8

4 years ago

0.5.7

4 years ago

0.5.6

4 years ago

0.5.5

4 years ago

0.5.4

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.5.0

4 years ago

0.5.1

4 years ago

0.4.41

4 years ago

0.4.40

4 years ago

0.4.39

4 years ago

0.4.38

4 years ago

0.4.32

4 years ago

0.4.31

4 years ago

0.4.30

4 years ago

0.4.29

4 years ago

0.4.28

4 years ago

0.4.27

4 years ago

0.4.26

4 years ago

0.4.25

4 years ago

0.4.24

4 years ago

0.4.20

4 years ago

0.4.19

4 years ago

0.4.18

4 years ago

0.4.17

4 years ago

0.4.15

4 years ago

0.4.16

4 years ago

0.4.13

4 years ago

0.4.14

4 years ago

0.4.11

4 years ago

0.4.12

4 years ago

0.4.10

4 years ago

0.4.9

4 years ago

0.4.8

4 years ago

0.4.7

4 years ago

0.4.5

4 years ago

0.4.6

4 years ago

0.4.4

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.20

4 years ago

0.2.19

4 years ago

0.2.18

4 years ago

0.2.17

4 years ago

0.2.16

5 years ago

0.2.15

5 years ago

0.2.14

5 years ago

0.2.13

5 years ago

0.2.12

5 years ago

0.2.11

5 years ago

0.2.10

5 years ago

0.2.9

5 years ago

0.2.8

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.5

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.4

5 years ago

0.1.0

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

6 years ago

0.0.4

6 years ago