GithubHelp home page GithubHelp logo

dmitriz / bigfloat Goto Github PK

View Code? Open in Web Editor NEW

This project forked from charto/bigfloat

0.0 2.0 0.0 40 KB

Fast arbitrary precision math library for computational geometry.

License: MIT License

TypeScript 100.00%

bigfloat's Introduction

bigfloat

npm version

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

  • conversion from JavaScript number type x = new BigFloat(123.456)
  • addition x.add(y)
  • subtraction x.sub(y)
  • multiplication x.mul(y)
  • comparison x.deltaFrom(y) alias x.cmp(y)
  • conversion to string in base 2, 10 or 16 x.toString(10)

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 destructuvely change the value:

  • x.truncate(limbs) rounds the fractional digits towards zero, to limbs * 32 bits.
  • x.round(digits) rounds approximately to digits decimal places (to enough limbs to hold them).

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

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

Internals

Numbers are represented in 32-bit limbs (digits in base 2^32) somewhat like in the GMP library. The least significant limb 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.

bigfloat is optimized for exponents relatively close to zero, so the location of the decimal point is always present in the limb array, even if that introduces otherwise insignificant leading or trailing zero digits.

License

The MIT License

Copyright (c) 2015 BusFaster Ltd

bigfloat's People

Contributors

jjrv avatar

Watchers

 avatar  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.