I’m writing a program that is using the functions sin() and cos() from the math.h library. However, I noticed I was getting funky results. After searching around and checking my math multiple times, I decided to do a simple check with this:
int main()
{
cout << "sin(45.0) = " << sin(45) << endl;
cout << "cos(45.0) = " << cos(45) << endl;
return 0;
}
And I get this output:
sin(45) = 0.850904
cos(45) = 0.525322
These should be equal right? Is there something special about the math.h library? Am I doing something wrong?
Here are the equations in WolframAlpha:
You should use
cmathin C++, rather than the old C header.std::sin()andstd::cos()both take a floating point value representing an angle in radian.The GCC version of this file includes a handy constant for π, (which does not exist in the C++ standard but) which will make it easier for you to convert degrees to radian:
See it run!