Post your method of calculating pi.

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

Haircut

Platinum Member
Apr 23, 2000
2,248
0
0
Originally posted by: Haircut
You can use:

PI = 24 arctan(1/8) + 8 arctan(1/57) + 4 arctan(1/239)

where arctan(x) = x - x^3/3 + x^5/5 - x^7/7 + ...

This will converge much faster.
I have just checked and this method gets Pi accurate to 14 decimal places after 6 iterations.

 

xirtam

Diamond Member
Aug 25, 2001
4,693
0
0
Originally posted by: Tyler
I wrote a little script to calculate pi based on the series (-1)^(n+1) * (1/(2n-1)).
It does the first million iterations, and is fairly accurate, I get: 3.14159165358977

here's the script:
#!/usr/bin/perl -w
$pi = 0;
for($n = 1; $n <= 1000000; $n++){
$pi += (-1)**($n+1) * (1/(2 * $n - 1));
}
$pi *= 4;
print "$pi\n";

I'm sure there are more effecient ways than that, and there are those calcultors on line that can jsut keep spitting out pi to as many places as you like.

Anyone got any good pi computations?

notfred, it's "just"
notfred, it's "efficient"
notfred, it's "calculators"

But seriously, is the error in the calculation (errors bolded) -- 3.14159165358977 -- due to some kind of rounding bug?
 

Heisenberg

Lifer
Dec 21, 2001
10,621
1
0
The arctan method is usually how I do it if I need it in my code somewhere. Works well and is fast.
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Just learn the first few digits. Supposedly 40-something digits is all you need for a circle the size of the universe, to the accuracy of a proton.
 

xirtam

Diamond Member
Aug 25, 2001
4,693
0
0
Originally posted by: CTho9305
Just learn the first few digits. Supposedly 40-something digits is all you need for a circle the size of the universe, to the accuracy of a proton.

Link? I strongly doubt that. How big is the universe?
 

xirtam

Diamond Member
Aug 25, 2001
4,693
0
0
22/7 sucks. 3.1428... != 3.1415...

28-15=13 off, just in the first 5 digits. Not even close.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
#include <iostream>
#include <iomanip>

using namespace std;

int main(void)
{
cout << setprecision(16) << M_PI << endl;
return 0;
}

//And blazing fast!
 

Muzzan

Member
Apr 15, 2003
169
0
0
#include <stdio.h>
#include <math.h>

double f(double x)
{
return (double)sqrt(1 - x * x);
}

int main(void)
{
int a = 0, b = 1;
double i = 0, area = 0, w = 0.0000001;

for(i = a; i <= (double)b; i += w)
{
area += (double)( f(i) * w );

}

printf("pi = %f\n", area * 4);

return 0;
}

-----

pi = 3.141593

;)
 

Haircut

Platinum Member
Apr 23, 2000
2,248
0
0
Originally posted by: xirtam
Originally posted by: CTho9305
Just learn the first few digits. Supposedly 40-something digits is all you need for a circle the size of the universe, to the accuracy of a proton.

Link? I strongly doubt that. How big is the universe?
Actually that's not too far off.

The universe is approximately 10^10 lightyears across, which is around 10^26 metres.
A proton is approximately 10^-15 metres across, which means the universe is about 10^41 protons across so Pi to 40 decimal places would give the diameter of the universe to an accuracy of around 10 protons given the circumference.
 

AvesPKS

Diamond Member
Apr 21, 2000
4,729
0
0
Originally posted by: Dezign
COSINE! TANGENT! SECANT! SINE!

THREE POINT ONE FOUR ONE FIVE NINE!

GOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO PI!!!





:D

Have you ever heard of the Illinois Institute of Technology? You have just recited our school chant. I'm completely serious.
 

oboeguy

Diamond Member
Dec 7, 1999
3,907
0
76
I'm sure nobody's thought of this one: Monte Carlo simulation. It's a pathetic way to calculate pi (n has to be ridiculously huge for it to converge nicely), but it helps show-off your knowledge of probability and geometry.

4*sum(rand(n,1).^2 + rand(n,1).^2 <= 1)/n

The above is Matlab code, if you're wondering if I've gone crazy or not. .^ means component-wise exponentiation, and rand(n,1) is a vector of length n of random numbers in [0,1].

Edit: Oh yeah, and I'm summing over a logical array (figure it out).
 

Krugger

Senior member
Mar 22, 2001
820
0
0
Originally posted by: jbahseng
Originally posted by: Tyler
I wrote a little script to calculate pi based on the series (-1)^(n+1) * (1/2n-1).
It does the first million iterations, and is fairly accurate, I get: 3.14159165358977

Shouldn't it be 3.1415926535....?

yea, i feel like it should be 3.14159265358979 as well. just memorize it to 30 places or so, you can't really need more than that.
3.141592653589793238462643383 is all i remember from back in HS
 

KingNothing

Diamond Member
Apr 6, 2002
7,141
1
0
Originally posted by: xirtam
Originally posted by: Tyler
I wrote a little script to calculate pi based on the series (-1)^(n+1) * (1/(2n-1)).
It does the first million iterations, and is fairly accurate, I get: 3.14159165358977

here's the script:
#!/usr/bin/perl -w
$pi = 0;
for($n = 1; $n <= 1000000; $n++){
$pi += (-1)**($n+1) * (1/(2 * $n - 1));
}
$pi *= 4;
print "$pi\n";

I'm sure there are more effecient ways than that, and there are those calcultors on line that can jsut keep spitting out pi to as many places as you like.

Anyone got any good pi computations?

notfred, it's "just"
notfred, it's "efficient"
notfred, it's "calculators"

But seriously, is the error in the calculation (errors bolded) -- 3.14159165358977 -- due to some kind of rounding bug?

Stalking notfred again?

Dezign gets my vote for best post in this thread. :D
 

4Lclovergirl

Senior member
Mar 25, 2003
474
0
0
actually, i just use 3.14... or leave it as pi and odn't bother calculating it. I have only taken stat 215, calc I &II and Physics I and II in college, and it's worked well enough. I hope to never have to use it again!
 

WarCon

Diamond Member
Feb 27, 2001
3,920
0
0
Here is a site all about calculating PI ......PI

Just make sure you use Ice Cream on top of mine.....:D

Edit: A link from this page to a website with the PI written out to 400 million digits...........why I ask you why????? 400 million digits of PI
 

Mark R

Diamond Member
Oct 9, 1999
8,513
16
81
It depends what I'm doing - if it's just an estimate, then I take pi to be equal to 1. If I need precision better than 1 order of magnitude, then I take it as 3.

Nice and easy.