JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 27
  • Score
    100M100P100Q60103F
  • License UNLICENSED

A library for arbitrary precision floating point arithmetic.

Package Exports

  • bigfloat.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 (bigfloat.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

bigfloat.js

A library for arbitrary precision decimal floating point arithmetic that can exactly represent all decimal fractions, unlike JavaScript's number data type which is 64-bit binary floating point.

Based on the original work by Douglas Crockford. His original implementation made use of a big integer library that was also included in his book How JavaScript Works. This implementation is built upon the bigint data type that was added to the language, and it runs pretty fast on V8.

Because of this it requires Node >= 10.4.0 or a Chrome >= 67 based browser. Firefox requires a flag to be enabled manually.

Note: This library is a work in progress and shouldn't be used in production.

import bigfloat from "bigfloat.js";

0.1 + 0.2 === 0.3;                     // false
bigfloat.evaluate("0.1 + 0.2 == 0.3"); // true

0.1 + 0.2;                      // 0.30000000000000004
bigfloat.evaluate("0.1 + 0.2"); // "0.3"

1 + Number.EPSILON / 2;                         // 1
bigfloat.evaluate(`1 + ${Number.EPSILON / 2}`); // "1.00000000000000011102230246251565"

2 ** 2 ** 2 ** 2 ** 2;                      // Infinity
bigfloat.evaluate("2 ** 2 ** 2 ** 2 ** 2"); // "2003529930406846464979072351560255750447825475569751...(More than 19 thousand digits)

It also understands scientific e-notation:

bigfloat.evaluate("1 + 2.220446049250313e-16"); // "1.0000000000000002220446049250313"

This library provides a set of functions for basic operations, and an evaluate() function that makes bigfloat operations behind the scenes. The first operation shown above can also be performed by making use of the other provided functions like this:

const { eq, add, make } = bigfloat;
eq(
  add(make("0.1"), make("0.2")),
  make("0.3")
); // true

Installation

npm install bigfloat.js --save

The bigfloat object

{
  coefficient: 522299n,
  exponent: -4
}

The coefficient is a bigint that contains all of the digits that make up the number. The exponent is a number that indicates where to place the decimal point. This bigfloat object represents the decimal value 52.2299

evaluate(expression, precision)

This function takes an expression in string form, and a negative integer for precision (default is -24) and returns a string:

bigfloat.evaluate("10 / 3", -5); // "3.33333"

Or a boolean:

bigfloat.evaluate(`4 >= ${Math.PI}`); // true

The tokens that make up the expression can be:

  • Parenthesis: (,)
  • Number: Decimal form or scientific e-notation
  • Operator: Arithmetic +,-,/,,* Relational ===,==,!==,!=,<,>,<=,>=

It would be nice to have a transpiler that replaces JavaScript numbers and operators for bigfloat function calls, but it seemed to me very convenient to have this functionality available at runtime.

make(number)

This function takes a number in a string or number form and returns a bigfloat object.

bigfloat.make(53.23);   // { coefficient: 522299n, exponent: -4 }
bigfloat.make("12000"); // { coefficient: 12000n, exponent: 0 }

string(bigfloat)

This function takes a bigfloat object and returns a string containing the decimal representation of the number. The conversion is exact.

bigfloat.string({ coefficient: 522299n, exponent: -4 }); // "53.23"

Other useful functions

  • add(augend, addend)
  • sub(minuend, substrahend)
  • mul(multiplicand, multiplier)
  • div(dividend, divisor, precision)
  • exponentiation(base, exponent)
  • eq(comparahend, comparator)
  • lt(comparahend, comparator)
  • gt(comparahend, comparator)
  • sqrt(n)
  • abs(n)
  • fraction(n)
  • integer(n)
  • is_big_float(n)
  • is_negative(n)
  • is_positive(n)
  • is_zero(n)
  • neg(n)
  • normalize(n)
  • number(n)
  • scientific(n)

Changelog

1.1.8

  • Exponentiation operators(^, **) are now right-associative.

1.1.9

  • Added a sqrt() function.

1.1.10

  • Added an exponentiation() function.
  • Exponentiation operations now support non-integer exponents.