Friday, May 21, 2010

C++ sin^2(x) Weird ><?

Hello Experts, I'm trying to code this function sin^2(x0


and i believe it to be: pow(sin(x),2);


However, when i do that... I get the wrong answer which is suppose to be 0.25





Here is my code:





#include %26lt;stdio.h%26gt;


#include %26lt;math.h%26gt;


#define PI 3.141592654


int main()


{


double x_deg, x, y, n, k, Y, TV;


printf("\nEnter x_deg: ");


scanf("%g", %26amp;x_deg);


x = PI*x_deg/180;


Y = pow(sin(x),2);


printf("True value of sin^2(x) = %g\n", Y);


}





I'm suppose to transform the deg to rad, which i did in the program


but i still not able to get 0.25 for my answer, here is what i got when i run it:





Enter x_deg: 30.0


True value of sin^2(x) = 0.72305





So offfffff %26gt;%26lt; OMG can someone help me out pls? thank you so much

C++ sin^2(x) Weird %26gt;%26lt;?
change this line :


scanf("%g", %26amp;x_deg) to





scanf("%lf", %26amp;x_deg)








It should work well now.
Reply:Check in the help if sin function in C++ takes in degree or radian. I think it is degrees. Double check it
Reply:You declared x_deg as a double but read it as a float.





...


float x_deg;


double x, y, n, k, Y, TV;


...


No comments:

Post a Comment