Introducing Big.js: Arbitrary Precision Math for JavaScript
Numbers in the JavaScript language are inherently limited to 64-bit floating-point precision. In most programming scenarios this is ample precision, but some applications need something more. For those applications, programmers typically make use of an arbitrary-precision math library. However, a complete library of this type for JavaScript does not exist.
The wonderful jsbin.js tool by Tom Wu is a step in the right direction. It is essentially a port of the BigInteger API included in the Sun Java programming language. However, it sadly does not have support for fractional numbers (only integers).
For me, in my development of jQuery.validity, it is necessary for me to be able to check the mathematical inequality of numbers of any length. Particularly as a result of this bug report, it became clear that parsing strings as floats was not a viable way of checking inequalities. The report provides the following example:
// The validation rule is that the number // represented by a string should not be // greater than twelve. So, if a string // housed the value... var str = "12.00000000000000005"; // ... I should be able to detect whether it is // less than or equal to the number 12. // But to do so, parsing as a float does not do. parseFloat(str) <= 12 // Evaluates to true, should be false. 12.00000000000000005 <= 12 // Also, true. // JavaScript is truncating the number. // In fact... 12.00000000000000005 // ...itself evaluates to 12.
In a client-side validation framework (such as validity), this is not acceptable, because the web-application may deal with numbers which have greater precision than JavaScript can process. (The server may be programmed with higher precision or an arbitrary-precision library.)
The conclusion is that an arbitrary-precision library, capable of processing fractional numbers, is needed in the client-side JavaScript code. For this, I’ve written Big.js.
At the moment, Big.js is only able to perform inequality operations on numbers (i.e. lessThan, lessThanOrEqualTo, greaterThan, greaterThanOrEqualTo and equals) on positive and negative numbers.
For example:
// Numbers in Big.js are called with the Big() constructor:
var number1 = new Big("12.00000000000000005");
var number2 = new Big("12");
number1.lessThanOrEqualTo(number2); // False, like it should be.
// To boot...
number1.lessThan(number2); // False
number1.greaterThan(number2); // True
number1.greaterThanOrEqualTo(number2); // True
number1.equals(number2); // False
Inequality is all that is needed, technically, for the library to be useful in validation. Big.js is therefore in version 0.7.* (beta) at the time of writing this article. Version 0.8.* will have addition and subtraction support, and 0.9.* will have support for other mathematical operators. Finally culminating in version 1.0.* which will be considered “out of beta”.
The 0.7.0 beta is available on Github now under an MIT licence. More operators to be implemented soon!
Comments
How about a method toString(n) or just toHex() and toBinary()?
Do you think you could implement that soon?
Hi, AJ. Somehow I hadn’t thought of that, but that’s a really great idea. I’ll start working on it.
Thanks!
Cool. I’ve written some JavaScript for node to read register and write register values for quick testing on an embedded system and I forgot that JS can only handle 31-bit unsigned values.
I’ll post a link once I get it up on github.
Sweet! Node is exciting.
good points and the details are more specific than elsewhere, thanks.
- Norman
Amiable fill someone in on and this mail helped me alot in my college assignement. Say thank you you seeking your information.
First-class posting, I just offer it with my friend of Taiwan. I Stumble UP your web publication area, you?ll detect a development of visitors inside of 24 hours for targeted persons. Cheers
Of course, what a great site and informative posts, I will add backlink – bookmark this site? Regards,
Reader.
Super-Duper site! I am loving it!! Will come back again – taking you feeds also, Thanks.
Hello. Great job. I did not expect this on a Wednesday. This is a great story. Thanks!