Skip to content
whatgoodisaroad edited this page Sep 14, 2010 · 6 revisions

Big.js is an arbitrary precision library for JavaScript

The Problem

One of the limitations of computing math in JavaScript is that you are bound to a fixed precision. All numbers in JavaScript are stored as 64-bit floating-point numbers with a range of -5e-324 to 1.7976931348623157e+308. This means that integers with any more than 15 digits are unreliable and frequently rounded, and that fractional numbers are even worse.

This is not commonly a problem. In general, most programming scenarios (especially in browser scripting) an extreme level of numerical precision is unnecessary. However, for those applications which do need precision, most programming languages provide an arbitrary-precision math library, which store and operate on numbers as a sequences of digits rather than their binary representation. This breaks these numbers out of fixed precision and allows any degree of accuracy at a cost of efficiency.

Big.js intends to provide this set of tools for JavaScript.

The Game-Plan

Progressively, over the major releases, we intend to implement more mathematical operations in Big.js. For the first release, Big.js only supports inequality tests on positive and negative numbers. Following, we’ll support addition, subtraction, multiplication and so on.

Example

Here’s the problem:

12.00000000000000005 <= 12 // True, should be false, but rounding occurs.

To solve this problem we can use Big.js. Here’s an example of creating a Big.js number and comparing it to another:

var number1 = new Big("12.00000000000000005");
var number2 = new Big("12");
number1.lessThanOrEqualTo(number2); // False, like it should be.