Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating Pi in Javascript!

Status
Not open for further replies.

sdfx

Programmer
Mar 15, 2001
2
US
Please help! I found a script that uses the following method to calculate pi:

1/pi = sqrt(8)/9801 * infinite sum 0 to infinity ((4n)!*(1103 + 26390n)/((n!)^4*396^(4n))


Here's the code:
I would appreciate it if someone could comment the code (it's not too long) so that I could learn how it works. Thank you for all the help.


-Mike
 
Hi sdfx!
If you just need PI constant you don't even need to calculate anything:

pi = Math.PI;

Remember - PI should be uppercased!
 
Yea...that will give you 12 digits. I need to calculate at least 1,000.
 
I don't believe that javascript will give you 1000 digits of precision.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
"It will if its a string"???

How do you do calculations with a string?

If you need to calculate pi to 1000 decimal places, how do you do that with a string instead of a number?
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
well, if your only point in calculating PI, is to calculate PI to 1000 digits... you can just append each digit to a string... and the string will hold as many digits as you like. luciddream@subdimension.com
 
That's assuming you can calculate each successive digit without exceeding the capacity of of the numbers you are using to do the calculations. I guess you'd have to use some sort of iterative calculations to do this.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top