c++ – cos() function gives wrong result

Question:

#include "stdafx.h"
#include <cmath>
#include <iostream>

int main()
{
   double d = 50.879840;
   std::cout << cos(d) << std::endl;
   return 0;
}

The program gives the result 0.817144 , although on any engineering calculator the result is 0.630948 , and this is correct. Where is the mistake?

Answer:

The cos , sin , and similar functions take a parameter in radians, not degrees.
Convert the value from degrees to radians, and everything will be correct:

double d = 50.879840;
std::cout << cos(d / 180 * Pi) << std::endl;
Scroll to Top