In this post today I am going to discuss C++ Math cos() functions.
The function is used to determine the cosine of an angle given in terms of a radian.
Syntax Of cos() function:
Consider a radian ‘x’. The syntax would be:
float cos(float x);
float cos(double x);
float cos(long double x);
double cos(integral x);
Parameter:
x: Value specified in terms of a radian.
Return value:
It returns the cosine of an angle in the range of[-1,1].
Example-1:
Let’s look at a basic case where x is positive.
#include <iostream> #include<math.h> using namespace std; int main() { double degree=60; double d=60*3.14/180; cout<<"Cosine of an angle is : "<<cos(d); return 0; }
Cosine of an angle is : 0.50046
When the degree is equal to60, the cos() function calculates the cosine of an angle.
Example-2:
Let’s look at a basic case where x is negative.
#include <iostream> #include<math.h> using namespace std; int main() { double degree= -90; double radian=degree*3.14/180; cout<<"Cosine of an angle is :"<<cos(radian); return 0; }
Output:
Cosine of an angle is :0.000796327
When the number is negative, the cos() function determines the cosine of the angle, but it stays the same as cos(-x) =cos (x).