Thread: program for calculation of the sine of an angle using the sine series

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    27

    program for calculation of the sine of an angle using the sine series

    hello everyone!
    there seems to be some logical error in this program. i get unusually large values for any angle like even sine 90 degree.
    Code:
    /*c program to compute the sine series*/
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    
    void main()
    {
    	  float x,sum=0,fraction=0;
    	  int n,i,j;                /*the declarations*/
    	  int factorial(int);
    
    	  clrscr();
    
    	  printf("\n Enter degree to calculate:");
    	  scanf("%f",&x);          /*reading the angle to be calculated*/
    	  printf("\n Enter number of terms:");
    	  scanf("%d",&n);          /* upto how many terms to calculate the value*/
    
    	  for(i=1,j=0;i<=n,j<n;i=i+2,j++)
    	  {
    			 fraction=pow(-1,j)*(float)(pow(x,i)/factorial(i));/*each term*/
    			 sum+=fraction;       /* summing up*/
    	  }
    
    	  printf("\n sine(%f)=%f",x,sum);
    	  getch();
    }
    
    int factorial(int n) /* the factorial calculating function*/
    {
    		int i;
    		int fact=1;
    		if(n==0)return 1;
    		else
    		{
    			  for(i=1;i<=n;i++)
    			  {
    					  fact*=i;
    			  }
                              return fact;
    		}
    		
    }
    please help!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    get rid of pow and factorial function

    if you have calculated fraction on i-th iteration - next could be calculated rather simply using it.
    something like

    Code:
    fraction = - fraction * x * x / i / (i-1);

    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

    conio.h is non-standard - get rid of it as well
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > there seems to be some logical error in this program. i get unusually large values for any angle like even sine 90 degree.
    Well yes, because the series usually take input as radians, not degrees.

    So for 90', you would typically type in something like 1.57 (otherwise known as PI/2)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Even 90 radians should give a sin result between 0 and 1.

    Isn't there a modulo function missing?

    eg, angle = angle%90 (for an angle in degress)

    Or is it supposed to be just one quadrant?

    -
    Last edited by megafiddle; 10-26-2013 at 05:45 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by megafiddle View Post

    eg, angle = angle%90 (for an angle in degress)

    -
    That would imply that sin 100 = sin 10, which it doesn't.

    The series expression for sin is only valid for radians, so if the end user is to type their input in degrees, the program will have to convert to radians before doing the series.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Thanks for the correction, that should have been angle = angle%360, plus you would need additional testing for quadrants.
    My example was for degrees, as I believe those were intended to be the required input units.

    The program above then, should limit input to 90 degrees or pi/2 radians?

    -

  7. #7
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by megafiddle View Post
    The program above then, should limit input to 90 degrees or pi/2 radians?
    I think the only thing that needs to be done is convert the input angle (in degrees) to radians because the Taylor series method for calculating sine uses radians and not degrees

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The series is good for all x. It will be quicker to converge for small x, but it will get there in the end.

  9. #9
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by tabstop View Post
    The series is good for all x. It will be quicker to converge for small x, but it will get there in the end.
    That's correct, but if you enter 90 (degrees) the series isn't going to converge to the value of sin(pi/2) which is the answer the user is after...

    Edit: Or am I missing something obvious?
    Last edited by SirPrattlepod; 10-26-2013 at 06:24 PM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by SirPrattlepod View Post
    That's correct, but if you enter 90 (degrees) the series isn't going to converge to the value of sin(pi/2) which is the answer the user is after...

    Edit: Or am I missing something obvious?
    The programmer will have to do that conversion.

  11. #11
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    I just noticed something else

    Code:
    for(i=1,j=0;i<=n,j<n;i=i+2,j++)
    What does the expression i <= n, j < n evaluate to? Notice this is using the comma operator.

  12. #12
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Ok, I've fixed your code and there are several issues

    a) If the user is inputting the angle in degrees the first convert their input to radians
    b) See my previous post
    c) Your method for calculating the sum of the series is wrong (note that terms should be either added or subtracted)
    d) The factorial function is going to overflow very quickly; you may want to use different integer types; e.g. unsigned long factorial(unsigned n)
    e) Even with the larger integer types factorial() is going to overflow very quickly; limit the value for 'n' that is acceptable

    Suggestions
    a) Fix the issues above
    b) Put the code that calculates sine into a function (this will, for a start, make testing easier)
    c) "Normalise" the angle to within the range 0 <= angle <= 2*pi radians so that the series converges faster AND mitigates the fact that you're going to have to limit 'n'

    Further suggestions
    a) Write a test function that compares the value of your sine function (you remembered to make it a function, right?) and the value that the math library gives for the same angle and checks that they are the same (given a reasonable error / precision)
    b) You may be able to exit the summation loop early (e.g. if sum == previous_sum then there is no need to keep going on)
    Last edited by SirPrattlepod; 10-26-2013 at 07:31 PM. Reason: added further suggestions

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by tabstop View Post
    The series is good for all x. It will be quicker to converge for small x, but it will get there in the end.
    Not if done in software and any of the intermediate calculations overflow, it won't.

    Once an integer overflow occurs (say, when calculating factorial()) or a floating point overflow occurs (say, when computing pow(90.0, i)) the game is over.

    pow(90.0, i) will overflow for values of i over about 160 (some variation, depending on floating point representation).

    Even worse, factorial(n) function will overflow a 16-bit int if n is greater than 7, and will overflow a 32-bit int if n exceeds 12. Even with long long ints (C99 or later) factorial() will overflow if n exceeds 20.

    Apart from that, the series is for radians (as Salem said) and the termination condition of the loop is suspicious.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Can someone ask grumpy to un-ignore me? :`-(

  15. #15
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Code:
          Angle     Expect        n=1        n=2        n=3        n=4        n=5        n=6   Err. n=6
         
              0    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000
             10    0.17365    0.17365    0.17365    0.17365    0.17365    0.17365    0.17365    0.00000
             20    0.34202    0.34198    0.34202    0.34202    0.34202    0.34202    0.34202    0.00000
             30    0.50000    0.49967    0.50000    0.50000    0.50000    0.50000    0.50000    0.00000
             40    0.64279    0.64142    0.64280    0.64279    0.64279    0.64279    0.64279    0.00000
             50    0.76604    0.76190    0.76612    0.76604    0.76604    0.76604    0.76604    0.00000
             60    0.86603    0.85580    0.86630    0.86602    0.86603    0.86603    0.86603    0.00000
             70    0.93969    0.91780    0.94048    0.93968    0.93969    0.93969    0.93969    0.00000
             80    0.98481    0.94258    0.98681    0.98475    0.98481    0.98481    0.98481    0.00000
             90    1.00000    0.92483    1.00452    0.99984    1.00000    1.00000    1.00000    0.00000
            100    0.98481    0.85923    0.99419    0.98440    0.98482    0.98481    0.98481    0.00000
            110    0.93969    0.74047    0.95782    0.93875    0.93972    0.93969    0.93969    0.00000
            120    0.86603    0.56322    0.89905    0.86397    0.86611    0.86602    0.86603    0.00000
            130    0.76604    0.32218    0.82327    0.76185    0.76624    0.76604    0.76604    0.00000
            140    0.64279    0.01201    0.73786    0.63468    0.64324    0.64277    0.64279    0.00000
            150    0.50000   -0.37258    0.65227    0.48503    0.50095    0.49996    0.50000    0.00000
            160    0.34202   -0.83692    0.57824    0.31548    0.34394    0.34192    0.34202    0.00000
            170    0.17365   -1.38633    0.52991    0.12826    0.17737    0.17344    0.17366    0.00001
            180    0.00000   -2.02612    0.52404   -0.07522    0.00693   -0.00044    0.00002    0.00002
            190   -0.17365   -0.17365   -0.17365   -0.17365   -0.17365   -0.17365   -0.17365   -0.00000
            200   -0.34202   -0.34198   -0.34202   -0.34202   -0.34202   -0.34202   -0.34202   -0.00000
            210   -0.50000   -0.49967   -0.50000   -0.50000   -0.50000   -0.50000   -0.50000   -0.00000
            220   -0.64279   -0.64142   -0.64280   -0.64279   -0.64279   -0.64279   -0.64279   -0.00000
            230   -0.76604   -0.76190   -0.76612   -0.76604   -0.76604   -0.76604   -0.76604   -0.00000
            240   -0.86603   -0.85580   -0.86630   -0.86602   -0.86603   -0.86603   -0.86603   -0.00000
            250   -0.93969   -0.91780   -0.94048   -0.93968   -0.93969   -0.93969   -0.93969   -0.00000
            260   -0.98481   -0.94258   -0.98681   -0.98475   -0.98481   -0.98481   -0.98481   -0.00000
            270   -1.00000   -0.92483   -1.00452   -0.99984   -1.00000   -1.00000   -1.00000   -0.00000
            280   -0.98481   -0.85923   -0.99419   -0.98440   -0.98482   -0.98481   -0.98481    0.00000
            290   -0.93969   -0.74047   -0.95782   -0.93875   -0.93972   -0.93969   -0.93969    0.00000
            300   -0.86603   -0.56322   -0.89905   -0.86397   -0.86611   -0.86602   -0.86603    0.00000
            310   -0.76604   -0.32218   -0.82327   -0.76185   -0.76624   -0.76604   -0.76604    0.00000
            320   -0.64279   -0.01201   -0.73786   -0.63468   -0.64324   -0.64277   -0.64279   -0.00000
            330   -0.50000    0.37258   -0.65227   -0.48503   -0.50095   -0.49996   -0.50000   -0.00000
            340   -0.34202    0.83692   -0.57824   -0.31548   -0.34394   -0.34192   -0.34202   -0.00000
            350   -0.17365    1.38633   -0.52991   -0.12826   -0.17737   -0.17344   -0.17366   -0.00001

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sine Rule calculation.
    By headshot119 in forum C++ Programming
    Replies: 3
    Last Post: 09-23-2009, 01:22 PM
  2. Sine series summation
    By kashya in forum C Programming
    Replies: 3
    Last Post: 12-17-2008, 08:00 PM
  3. Computing the sine of an angle
    By Bearcat in forum C Programming
    Replies: 1
    Last Post: 02-13-2002, 12:46 PM
  4. Sine Wave Program Help
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-19-2001, 12:33 AM
  5. sine C program with Power Series
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-08-2001, 10:46 AM

Tags for this Thread