Thread: Sine (Sin) Algorithm Help

  1. #46
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    30
    Quote Originally Posted by StaticKyle View Post
    Okay, I implemented yours, laserlight, and I tested it and the answer that I got with setting the loop count or n to 15 wasn't close to the value that I got from my calculated value.
    well in that case try this
    Code:
    double sine(float x)
    {
      int i,n;
      int sign = -1;
      double sum = x,power=x,fact = 1.0,psum=0.0;
      for (i = 1; fabs(fabs(sum) - fabs(psum)) > 0.0000000001; ++i)
      {
        psum=sum;
        power = power * x *x;
        n = (2*i+1); 
        fact = fact * n * (n-1); 
        sum += sign * power/fact;
        sign = -sign;
      }
      printf("n value is %d\n",i);
      return sum;
    }
    the angle is expressed in radians ,Am i right?
    Last edited by chakra; 05-10-2008 at 11:05 PM. Reason: the angle is expressed in radians ,Am i right?

  2. #47
    Registered User
    Join Date
    May 2008
    Posts
    27
    Yes, I am given the number in degrees, but I convert it to radians.

    I plug 45 into the program, and it returns .707107
    but when I plug it into my calculator, I get .8509035245

  3. #48
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    sin(45 radians)=0.8509
    sin(45 degrees)=0.7071

    What I meant to say was: Maybe your calculator is in radian mode.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #49
    Registered User
    Join Date
    May 2008
    Posts
    27
    Lol, awesome. Thank you. Now I just feel like a retard.

    Now to try and figure out the other method. Is there any way for me to simply use this one and do an if else statement?

  5. #50
    Registered User
    Join Date
    May 2008
    Posts
    27
    YAY! I just want everyone to know that I got it working right. Thanks for all of the help guys!

  6. #51
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's good. What is your current working solution?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #52
    Registered User
    Join Date
    May 2008
    Posts
    27
    Code:
    double sine(float angle)
    {
    	#define EPSILON 0.000001
    	#define PI 3.14159265358979323846264338327950288419716939937510582097494459
    	double x = (angle * PI) / 180;
    	int i;
    	int sign = 1;
    	double sum = 0.0;
    	double temp = 0.0;
    	double temp2 = 0.0;
    	for (i = 0; i < 14; ++i)
    	{
    	    sum += sign * (pow(x, 2 * i + 1) / factorial(2*i+1));
    	    printf("%f  ", sum);
    	    printf("%d \n", i);
    	    if (sign > 0)
    		{
    			temp = sum;
    		}
    		else
    		{
    			temp2 = sum;
    		}
    		if (temp - temp2 < EPSILON)
    		{
    			i = 14;
    		}
    		printf("%f ", temp);
    		printf("%f \n", temp2);
    	    sign = -sign;
    	}
    	return sum;
    }
    It probably isn't efficient, but it is due tomorrow, and so I did what I could. I am also having a problem with using multiple libraries (files...) with a single header file. I try to do what I did with this one and it does something strange with the library files. Like, it compiles the first go around, and then I try to compile the main program again so that it works and it gives me an error.

  8. #53
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It probably isn't efficient, but it is due tomorrow, and so I did what I could.
    Well, it's better to try than to be spoonfed.

    If you are interested, my answer follows. If it is still not yet past your deadline, I hope that you have the honesty not to copy

    Consider the sine computation expressed in summation notation:
    From n = 0 to infinity
    sine(x) = sum of (-1)^n * (x^(2n+1) / (2n+1)!)

    (-1)^n just gives us the sign of the term, so it is not so interesting. What is interesting is the term itself, excluding the sign. As mentioned, it is expensive to calculate the power and the factorial on each iteration, and we risk overflow. What we can do is express the term in relation to the previous term, i.e., a recurrence relation:

    0th term = x
    nth term = (n-1)th term * (x^2 / (2n * (2n+1)))

    Now, x^2 is a loop invariant: it does not change on each iteration. (2n * (2n+1)) is relatively quick to compute and will not overflow unless n is large. Consequently, (x^2 / (2n * (2n+1))) tends to 0 as n tends to infinity. So, all we need to do is keep looping as long as (x^2 / (2n * (2n+1))) is greater than 0.000001.

    As such, a solution presents itself:
    Code:
    #include <stdio.h>
    
    const double EPSILON = 0.000001;
    
    double sine_nonneg(double x)
    {
        double term = x;
        double sum = x;
        int sign = -1;
        double x_squared = x * x;
        int i;
    
        for (i = 1; term > EPSILON; ++i)
        {
            term *= x_squared / ((i + i) * (i + i + 1));
            sum += sign * term;
            sign = -sign;
        }
    
        return sum;
    }
    
    double sine(double x)
    {
        return x < 0.0 ? -sine_nonneg(-x) : sine_nonneg(x);
    }
    
    int main(void)
    {
        printf("&#37;f\n", sine(-12.34));
        return 0;
    }
    I chose to use the sine_nonneg function so as to more easily handle the sine of a negative angle by having sine_nonneg compute the sine of the corresponding non-negative angle.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM