Hello
I have a question regarding this program in c.
My assignement is as follows :
*read the angle in degrees
*convert that angle in radians
*Apply the formula for calculating cosine function with factorials
*Then, automate the above calculating procedure by using loops
*Afterwords calculate the absolute error between those two cases
#include<stdio.h>
#include<math.h>
int main(void)
{
double s_alpha;
double s_alpha2=0;
double j;
short angle,i;
double rad;
long q;
do
{
printf("\nInput angle [Degrees] : ");
scanf("%hd",&angle);
if(angle>0&&angle<360)
{
break;
}
printf("\nNot allowed_repeat\n");
}while(angle<=0||angle>=360);
rad=angle*(M_PI/180);
s_alpha=1-((pow(rad,2))/(1*2)+
(pow(rad,4))/(1*2*3*4)-
(pow(rad,6))/(1*2*3*4*5*6)+
(pow(rad,8))/(1*2*3*4*5*6*7*8)-
(pow(rad,10))/(1*2*3*4*5*6*7*8*9*10)+
(pow(rad,12))/(1*2*3*4*5*6*7*8*9*10*11*12)); //factorials by 'hand'
int count=0;
for(i=2;i<13;i+=2)
{
q=1.0;
j=1.0;
while(j<=i) //factorials in a loop
{
q=q*j++;
}
count++;
if(count%2==0)
{
j=(pow(rad,i)/q);
s_alpha2-=j;
}
else
{
j=(pow(rad,i)/q);
s_alpha2+=j;
}
}
s_alpha2=1-s_alpha2;
printf("\nAngle of %hd deg. = %g rad\n",angle,rad);
for(i=0;i<27;i++)
{
printf("-");
}
printf("\nOutput: \nCalculated via function : \t%lf",s_alpha);
printf("\nCalculated via loop : \t\t%lf",s_alpha2);
printf("\nAbs error : \t\t\t%lf",s_alpha2-s_alpha);
getchar();
return 0;
}
What I would be much interested in learning is that is it possible for such a large error to appear while comparing those two cases. I also understand that there could also be a defect with the code, so any input is appreciated. Thank you very much !