double pow(int b, int e)
{
int x;
while (e != 0)
{
b *= x;
e--;
}
return b;
}
double factorial(double x)
{
int factorial = 1;
for (int i = 0; i <= x; ++i)
if (i == 0)
factorial = 1;
else
factorial = factorial * i;
return factorial;
}
void taylor1(double x)
{
double total = 1;
{for (int i = 0; i <= 5; i++)
{
if (i % 2 == 1)
total += ( pow(x,i*2+2) / factorial(i*2+2) );
else
total -= (pow(x, i*2+2) / factorial(i*2+2) );
}
}
cout << total;
cout << endl;
}
void taylor2(double x)
{
double total = 1;
int i = 0;
int count = 1;
double temp = 0;
do
{
{
if (i % 2 == 1)
{
temp = (pow(x, i * 2 + 2) / factorial(i * 2 + 2));
total += temp;
}
else {
temp = (pow(x, i * 2 + 2) / factorial(i * 2 + 2));
total -= temp;
}
}
count++;
i++;
} while (fabs(temp) >= .0001);
cout << "The last recoreded temporary value was: "<<temp << endl;
cout << "The computed value for cosine is : "<< total << endl;
cout << "It took " <<count << "values to calculate the value of the function to .0001 places"<< endl;
cout << endl;
}
void cosine(double x)
{
double value = 0;
value = cos(x);
cout << " The cosine of " << x << " radians is " << value << endl;
}
I see, I added an extra unneeded variable to the function, i.e. over complicating things. I still havent been able to figure out why the function taylor2 fails to return a value when rad is over 2, I suspected it had something to due with long vs double vs float but ive come up empty handed there as well.