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!
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.
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).