'Problems with math.h

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:

sin(45)

cos(45)



Solution 1:[1]

sin and cos expect input in radians not degrees.
try this:
sin(degrees * pi / 180)

Solution 2:[2]

Trigonometric functions use radians, not degrees.

Solution 3:[3]

sin() and cos() treat your parameter as Radians not Degrees.

Solution 4:[4]

Its strange because I cannot get 0 when using cos(1.5707963267948966) which is the radian value for 90 degrees.

    long double degrees = 90.0;
    long double radians = glm::radians(degrees);
    long double result = cos(radians);

The result is equal to 6.1232339957367660e-017 - not kidding. glm is calculating radians correctly as well, checked it against google. I used long doubles to make sure it wasn't somehow rounding problems but its not. Just thought this info might help somehow

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ignacio Vazquez-Abrams
Solution 3 Jonathan Leffler
Solution 4 Tomster954