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

1. ## 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;
}

}```

2. 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

3. > 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)

4. 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?

-

5. Originally Posted by megafiddle

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. 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. Originally Posted by megafiddle
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. 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. Originally Posted by tabstop
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?

10. Originally Posted by SirPrattlepod
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. 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. 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)

13. Originally Posted by tabstop
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.

14. Can someone ask grumpy to un-ignore me? :`-(

15. 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```