Giter Club home page Giter Club logo

double.js's Introduction

double.js

bundlephobia Build Status Codecov

Floating point expansion with 31 accurate decimal digits (100+ bits), also known as double-double arithmetic or emulated float128. 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 uses 64 bit IEEE 754 with round-to-nearest-even after each operation and without FMA instruction.

Benchmark

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, x4.5 in Safari and x7.5 in Firefox for mandelbrot set algo.

Special thanks

To Jeffrey Sarnoff for help me with books and algorithms.

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]

double.js's People

Contributors

munrocket avatar renovate-bot avatar riim avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.