hi. 2 all
anyone can tell what is the wrong with this code? the result of this program is :
cos(60)=-0.952413
sin(60)=-0.304811

``````#include <iostream.h>
#include <math.h>
main()
{
float x,y,z;
y=cos(60);
z=sin(60);
cout<<"cos(60)="<<y<<endl;
cout<<"sin(60)="<<z<<endl;
}``````

cheers

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

4
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by n.utiu

I don't see a problem there. I've been using Borland C++ for my home assignments, and your code compiled on it without errors. Then again, it is an outdated compiler, and you are probably using a different one.

Maybe you want to put getch() in the end, so that window does not close, when program is finished, and you can view the results?

The math.h functions take an angle in radians. Use the conversion pi radians is to 180 degrees to convert your angles before sending them in.

And the one that solves this mystery

math.h trigonometric functions take parameters in radians. To convert radians to degrees :
cos (60 degrees) is equivalent to

``cos (PI*60/180)``

PI - predefined constant in math.h

Edited by n.utiu: n/a

PI - predefined constant in math.h

I think it's M_PI (although it may be redefined elsewhere).

I think it's M_PI (although it may be redefined elsewhere).

Yeah, sorry. The constant in cmath is M_PI.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.