Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Beginner C help

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

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;
}

Name: Anonymous 2009-03-17 15:24

Ok, I've helped him.
sg = -sg;

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List