Name: Anonymous 2009-03-17 13:27
This function to estimate cos(x) based on given precision e exits after one repetition of the do/while loop. What's wrong here?
cos(x) ~= 1 – x^2/2! + x^4/4! -...+x^(2i)/(2i)!
cos(x) ~= 1 – x^2/2! + x^4/4! -...+x^(2i)/(2i)!
float estim(float x, float e)
{
float s = 1, t = 1;
int sg = 1, i = 1;
do
{
t = s;
if (sg) s -= (fpow(x, 2*i)/fact(2*i));
else s += (fpow(x, 2*i)/fact(2*i));
i++;
sg = !sg;
} while (abs(s-t)>e);
return s;
}