Reputation: 31810
In JavaScript, the function alert((3*3*Math.sqrt(2))/13)
prints 0.9790709277967582
, which is only an approximation of the expression's true value. Is there any way to perform arithmetic operations in JavaScript like this one without loss of precision, so that something like 3*3*sqrt(2))/13
would output 9*(2)^(1/2)/13
, which is the exact value, instead of 0.9790709277967582
, which is only an approximation?
Upvotes: 2
Views: 1483
Reputation: 59283
There's a library for this here: https://github.com/whatgoodisaroad/Big-js/downloads
It has a bunch of functions like myNum.add(otherNum)
, so it might be a little harder to use. However, if you need infinite precision it will have to do.
Example:
var number1 = new Big("12.00000000000000005");
var number2 = new Big("12");
number1.lessThanOrEqualTo(number2); // False, like it should be.
Upvotes: 2