Taylor's Series of cosine x and sine x

Hi , I've been cracking my head with this Taylor's series problem , im new in c++ and need some help with my code , the output of both sine and cosine is 0 and 1 respectevely . So far I understand that the signs alternate but im not sure if my way of doing it is the correct one. Thanks in advance for your help .

float factorial(int w)
{

int y=1,z;

z=w;

int n =1;

while (n<=z){

y*=n;

n++;

}
return y ;

}




float sine( float x){

int fact;



for ( int j =0;j<100;j++){

for ( int i =1;i<10; i++){ {

if ( i%2!=0){

fact=factorial(i);


x+= (j%2==0 )? ((-(pow (x,i)))/fact) :((pow (x,i))/fact );//

}

}
return x;
}
Last edited on
forgot to specify this , the user will input the degrees in radians .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
float factorial(int w)
{

int y=1,z;

z=w;

int n =1;

while (n<=z){

y*=n;

n++;

}
return y ;

}




float sine( float x){

int fact;



for ( int j =0;j<100;j++){	

for ( int i =1;i<10; i++){	{

if ( i%2!=0){

fact=factorial(i);


x+= (j%2==0 )? ((-(pow (x,i)))/fact) :((pow (x,i))/fact );// 

}

}
return x;
}
Why do you have two for loops? Seems like for the Taylor series you only need one. Also, line 39 shouldn't be x... x should be the angle provided (the angle you want the sine or cosine of).
Thanks hyperfine , I managed to solved the problem , you are right the second for loop wasn't necessary at all .
Topic archived. No new replies allowed.