Hi everyone,
I am taking a C++ 100 level beginners course and we just got this assgnment that I am stuck on. Here is the assginment description:
We are going to just compute the first 15 or so digits of π using doubles.
In any case, to compute the value of π, we need a formula for it. Let’s use the standard formula,
π = 16arctan(1/5) - 4arctan(1/239)
This is all well and good, but in order to compute π, we need to compute arctan now. We could try to use a standard function from cmath, but let’s write our own function to compute arctan. Mathematicians tell us an easy formula to use isarctan(x) = x - x^3/3 + x^5/5 - x^7/7 + x^9/9 - ...
The only parts that might need some thinking are the alternating signs, and the fact that the exponents and denominators both increment by 2.
Here is a sample output from the program:
Pi = 3.14159265358
Press any key to continue
In class, the professor told us to compare it to the programs we worked on to compute sin(x) and e^x. I have been referring to those programs I have but I still can't seem to solve it. Here is what I have so far:
#include <iostream>
using namespace std;
int main()
{
double denom;
double sign;
double power;
double old;
double arctan;
double x;
double y;
int count, i;
arctan = 0;
old = 1;
count = 1;
sign = 1;
x = 1/5;
y = 1/239;
while (old != arctan)
{
old = arctan;
denom = 1;
for (i = 2; i<=count; i++)
denom += 2;
power = 1;
for (i = 1; i <=count; i++)
power += 2;
arctan = arctan + sign*power/denom;
sign = -sign;
count += 2;
}
cout << "Pi = " << arctan << endl;
return 0;
}