How to display the highest number of decimals in javascript?

Asked

Viewed 62 times

2

I was doing some experiments and I would like to calculate the 1,000 first decimal places of the Euler constant (as demonstrated on this website http://www.profcardy.com/cardicas/constantes.php), then did this program in javascript:

(function euler(){

var f;
var c = 0;
var n = 1;
var fx = 0;

while (c <= 10000){

    f = (1+(1/n));

    fx = Math.pow(f,n);

    console.log(Number.MAX_VALUE, fx);
    n++;
    c++;
}
})();

When running on the console the last result was 1.7976931348623157e+308 2.7181459404132355. How do I display them to the 1,000 first decimal places as in the example given on the site? It can be in javascript or any other language. I found that question Decimal places in Javascript, but without the resolution I need.

1 answer

1

Hello,

Understanding the question you can consider the following:

JS uses a binary floating point-based numeric type and this means there will be no numerical accuracy. The maximum is Number.MAX_VALUE (1.7976931348623157e+308). Which considers an accuracy of up to 15 decimal places exact.

Considering this. So what you need is a library of arbitrary precision decimal number manipulation. Use the library javascript-bignum.

An example of how it works can be checked here: How to calculate PI with "n" decimal places in Javascript?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.