Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - More accurate value of Pi?

I type Math.PI; in Chrome Console and this is returned:

3.141592653589793

Then I type Math.PI - 3.141592653589793; and 0 was returned.

Is there a way to get a more accurate value (such as 3.1415926535897932384) of Math.PI in Javascript?

like image 633
chris97ong Avatar asked Mar 31 '14 10:03

chris97ong


2 Answers

I don't see why you would need PI to such accuracy but you can either :

a) Calculate PI yourself using Leibniz formula

b) Define PI yourself (using this library)

var PI = new bigdecimal.BigDecimal("3.141592653589793238462643383279");


You can find first 100,000 digits of PI here if you really need it.

like image 129
user3368484 Avatar answered Oct 06 '22 13:10

user3368484


The maximum decimal places in javascript is limited to 15.

So you cannot get more than 15 decimal places.

But you can get up to 20 decimal places by doing but its not accurate

Math.PI.toFixed(20); //3.14159265358979311600

That will give you a PI value with 20 decimal places.

Note: 20 is the maximum and 0 is the minimum for toFixed().

So trying Math.PI.toFixed(100) will throw an error.

like image 40
Amit Joki Avatar answered Oct 06 '22 13:10

Amit Joki