JSPM

  • Created
  • Published
  • Downloads 74
  • Score
    100M100P100Q69003F
  • License MIT

Floating point expansions with 32 accurate decimal digits, also known as double-double arithmetic.

Package Exports

  • double.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (double.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

double.js

bundlephobia Build Status Codecov

Floating point expansions with 31 accurate decimal digits, also known as double-double arithmetic. This library can be useful for fast calculation with extended precision. For example in 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 from references below. 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 version 1 uses 64 bit IEEE 754 with round-to-nearest after each operations.

Benchmark

double.js

You can check calculation time and quality of different libraries in your browser.

Usage

Include double.js script to webpage or install npm package. Most of all functions have static and instance methods. Instance methods more handy. Static methods are faster but you need to control memory allocation by yourself. Result of static methods always returned in first variable, that's why they mutate it. If you want to avoid mutation you need to clone it before usage.

import { Double } from 'double.js';

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

// S(r) = 4/3 * PI * r^3
const S = (r) => new Double('4.18879020478639098461685784437267').mul(r.pow(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.toNumber()); }

You can play with library in sandbox. API details you can find in wiki page.

Current status

Functions mult21/div21 and parseDouble/toString not properly accurate with back and forth operations. Anyway it stable with long repetitive calculations. Give me feedback if you think that this library is useful.

Special thanks

To Jeffrey Sarnoff for help me with books and algorithm.

References

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