How to use cos() in C++
In trigonometry, the cos function of a right-angled triangle is defined as the length of the adjacent side over the longest side, i.e., the hypotenuse. An image of this is shown below:
The cos function in C++ works precisely like the cosine function in trigonometry. The return value of the cos function is the cosine of an angle given in radian.
To use the cos function, the cmath header file needs to be included in the program. This is done in the following way:
#include <cmath>
Parameters
The cos function takes one parameter in radians.
Return value
The cos function returns the value between -1 and 1.
The function can have the return value of:
- float
- double
- long double
These types depend on the types of arguments the programmer gives to the functions. Below, Figure 2 shows the types of return types when different argument types are passed:
Example
The code below shows how the cos function functions in C++:
#include <iostream>#include <cmath>using namespace std;int main(){double x = 0.5;// result is in doublecout << "cos(x) = " << cos(x) << endl;return 0;}
Free Resources