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

Pages: 1-

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 13:28

Oops, the t = s; is vestigial and redundant.

Name: Anonymous 2009-03-17 14:38

float estim(float x, float e){
    return Math.cos(x);
}

Name: Anonymous 2009-03-17 15:24

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

Name: Anonymous 2009-03-17 16:03

Use TRAH and FLASE constants to make your code more readable.

Name: Anonymous 2009-03-17 16:21

>>4
This way, it's going to oscillate between 1 and -1 without end!

Name: Anonymous 2009-03-17 22:47

abs(s-t)>e is not the right way to force a certain precision.  Calculate how many terms will get you the precision you need(my calculus is rusty but I know it can be done) then do that many iterations using a for loop. Or just make e indicate how many iterations to do and use a for loop.

Name: Anonymous 2009-03-18 6:07

Resurrection bump

Name: Anonymous 2010-12-17 1:23

Are you GAY?
Are you a NIGGER?
Are you a GAY NIGGER?

If you answered "Yes" to all of the above questions, then GNAA (GAY NIGGER ASSOCIATION OF AMERICA) might be exactly what you've been looking for!

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