approximating pi

B

bryo1001

Hi, im writing a prog to approximate pi as im teaching myself c++. I
was just wondering if i wanted to calculate pi to a certain tolerance,
say like (pi - .001) or some other tolerance, what kind of loop
structure can i use to go about this. I have the algorithm that
calculates pi already programed in, ie 4(1-1/3+1/5...). Any help would
be great! Thanks.
 
H

Howard

bryo1001 said:
Hi, im writing a prog to approximate pi as im teaching myself c++. I
was just wondering if i wanted to calculate pi to a certain tolerance,
say like (pi - .001) or some other tolerance, what kind of loop
structure can i use to go about this. I have the algorithm that
calculates pi already programed in, ie 4(1-1/3+1/5...). Any help would
be great! Thanks.

This is not a C++ question, really. In whatever language you use, your loop
will be alternately adding or subtracting 4/(1+2k), right? Well, when
4/(1+2k) (ignoring the sign) is less than your tolerance value, drop out of
the loop. Because each term you're adding alternates sign (zeroing in on
the "true" value) and is smaller than the previous one, you can guarantee
that if the current term is less than the tolerance value, you must be
within that tolerance already.

-Howard
 
B

Bushido Hacks

Approximating pi is much easir defined a a function rather than a
variable.

Consider your high school trigonometry.

pi = 180 degrees
pi/2 = 90 degrees
pi/4 = 45 degrees

x * x + y * y = r * r
tan(theta) = sin(theta) / cos(theta) = (r * sin(theta)) / (r *
cos(theta)) = y/x
where
x = r * cos(theta)
y = r * sin(theta)
where r is the radius, theta is an angle in radians

tan(theta) = y/x if and only if theta = arctan(y/x)
arctan is the inversere tangent. C++ calls it atan2(y,x)

If in a 45-45-90 right triangle, x = 1 and y = 1, then r = sqrt(2)

However, arctan(1/1) = pi/4

Therefore pi = 4 * arctan(1/1)

In C++, you want to do the following

#include <cmath>

double my_pi(void){return (4*atan2(1));};

PI might already be declared in the math libarary, so you should give
it a different name.

On this note, how would I define my own functions for arctangent and
square root in C++?
 
R

rajkumar

You have to understand that 1 - 1/3 + 1/5 will suffer round off errors
because the way floating point numbers are stored on a computer.

So the "certain tolerance" you want to calculate pi to is limited by
your computer.

Please consider using a arbitary precision library like say

http://www.swox.com/gmp/

to go beyond your computer limits
 
U

uououo

#include <stdlib.h>
#include <stdio.h>
long a=10000,b,c=2800,d,e,f[2801],g;
int main()
{
for(;b-c;f[b++]=a/5 );
for(; d=0,g=c*2; printf("%.4d",e+d/a),e=d%a,c-=14 )
for(b=c; d+=f*a,f=d%--g,d/=g--,--b; d*=b);
return 0;
}
 
K

Karl Heinz Buchegger

bryo1001 said:
Hi, im writing a prog to approximate pi as im teaching myself c++. I
was just wondering if i wanted to calculate pi to a certain tolerance,
say like (pi - .001) or some other tolerance, what kind of loop
structure can i use to go about this. I have the algorithm that
calculates pi already programed in, ie 4(1-1/3+1/5...). Any help would
be great! Thanks.

You could use this general iteration structure:

double LastValue = Initial_Value;
double ThisValue;

do {
LastValue = ThisValue;
// calculate a new ThisValue
} while( fabs( ThisValue - LastValue ) < Tolerance );

// do something with the calculated value

The idea is to compare the calculated value in the current loop
iteration with the calculated value in the previous loop iteration.
That's the purpose of 'LastValue' and 'ThisValue'. If this difference
is small enough to be acceptable, the loop terminates.

Now that you can do this, can you calculate pi using random numbers?
 
K

Karl Heinz Buchegger

Bushido said:
On this note, how would I define my own functions for arctangent and
square root in C++?

If efficiency is not your concern:
For square root you could eg. use the 'Newton method' to
solve for roots of the equation

2
x - a = 0

(where x denotes the unknown square root and a denotes the
number to get the square root from: x = sqrt(a) )

The Newton method works by calculating

f(x)
x = x - -------
n+1 n f'(x)

2
f(x) = x - a
f'(x) = 2x

Thus
x * x - a
n n
x = x - ---------------
n+1 n 2 * x
n

eg. a = 5

x = 2 (just choose a reasonable number)
0

x = 2.125
1

x = 2.2389705
2


As for calculating arctangent. I guess a Taylor expansion or something like
that could be done.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top