calculate pi

Hey guys,
my professor wants me to write a program that calculates pi to about 15 digits, and i am just stumped. He wants us to use the formula pi=4arctan(1/5)-arctan(1/239), but i cant just call the function, i have to write my own. Here is what he gave us in class;

#define _USE_MATH_DEFINES
#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
double oldval, newval, fact, xtothen, x;
int n, i;

cout << "Enter x: ";
cin >> x;

newval = 0;
oldval = 1;
n = 0;

while(oldval != newval)
{
oldval = newval;

//compute n**x
xtothen = 1;
for (i = 1; i<=n; i++)
xtothen *= x;

//compute n!
fact = 1;
for (i = 1; i<=n; i++)
fact +=i;

newval = oldval + xtothen / fact;

n++;

}

cout << "The answer is " << newval << endl;

return 0;


}

he said we should be able to use this to write it. Would anyone be kind enough to help me out? I'm so stumped... Thank you!
> he said we should be able to use this to write it

What he gave you in class computes
1 + x + x2/2! + x3/3! + x4/4! + ...

This is the Maclaurin series for ex; when x==1, this becomes
e = 1 + x + 12/2! + 13/3! + 14/4! + ...

and can be used to compute the value of e.


What you are required to do is to use Machin's formula
PI/4 = 4 * arctan(1/5) - arctan(1/239)

to compute the value of PI;

the Maclaurin series expansion for arctan(x) is:
arctan(x) = x - x3/3 + x5/5 - x7/7 + x9/9 - ...

https://ccrma.stanford.edu/~jos/pasp/Arctangent_Series_Expansion.html

So write a function to compute arctan(x) to the required accuracy using this series, and use this function to calculate the value of PI.
heres what i have but when i return k it makes an int of it and that int becomes 0 because the rounding. so the ouput is 0. if someone knows how to fix this tell me please.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "stdafx.h"
#include <cstdlib>
#include <cmath>
#include <stdio.h>

int arctan(float x) 
{
	float i,k;
	for(i=1;i<=31;i=i+2)
	{
      if(int((x+1))%4==0)
	  {
		  k=k-(pow(x,i)/i);
	  }
	  else
	  {
		  k=k+(pow(x,i)/i);
	  }
	}
	return(k);
}

int main() 
{
long double pi,arc1,arc2;
arc1=arctan(1/5);
arc2=arctan(1/239);
pi=(16*arc1)-(4*arc2);
printf("pi= %Lf \n",pi);
system("pause");
return 0;
}
1
2
3
4
// If you don't put the point zero 1/5 integer is zero
// and 1/239 is zero
arc1=arctan(1.0/5.0);
arc2=arctan(1.0/239.0);

Also, init k to zero float i,k(0); and make the return type a float or double but not int.

Edit: I don't think the if statement is working like you think. Its always false.
You just want to ping-pong between 1 and -1, so try
1
2
3
4
5
float val(1.0);
for (...) {
// Just put the val in the right spot
val *= -1.0;
}

I get
pi= 3.141592706 
Last edited on
@btfrenchy
here is the full code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstdlib>
#include <cmath>
#include <stdio.h>

float arctan(float x) 
{
	float i,k,val(1.0);
	for(i=1;i<=99;i=i+2)
	{
      
		  k=k+(val*(pow(x,i)/i));
		  val*=-1;
	
	}
	return(k);
}

int main() 
{
long double pi,arc1,arc2;
arc1=arctan(1.0/5.0);
arc2=arctan(1.0/239.0);
pi=((4*arc1)-(arc2))*4;
printf("pi= %.7Lf \n",pi);//if the length is higher then 7 the numbers are wrong
system("pause");
return 0;
}
Last edited on
In the series
arctan(x) = x - x3/3 + x5/5 - x7/7 + x9/9 - ...

Let:
termi = numreratori / denominatori

The following recurrence relation holds:
termi+1 = numreratori * ( x * -x ) / ( denominatori + 2 )


I would think that your professor expects you to exploit this recurrence relation (the class room code does use the recurrence relation in the expansion of e).
thanks guys, this is kind of helpful. I'm still a little confused, you helped me a ton. Thank you.
Topic archived. No new replies allowed.