C++

The C++ cos() Function with Example

The cos() function is a mathematical function that returns a cosine of the input angle. In C++, it is a part of the math.h library and finds the cosine of the input angle. The cos() function requires an angle to be passed as an argument and computes the cosine value of that angle, which is then returned by the function.

How does the cos() Function Work in C++?

The cos() in C++ is a part of the math.h library and it takes an angle as a parameter. The angle is specified in radians. The cos() then returns the cosine of the input angle using a mathematical formula and returns the value. The formula used by the cos() function is:

cos(x) = adjacent/hypotenuse

Where x is the angle, adjacent is the side length which is adjacent to the angle x of the triangle and hypotenuse is the measurement of the hypotenuse side of a right-angle triangle. The cos() in C++ gives cosine of angle x using this formula.

Syntax of cos() Function
The syntax of the cos() function is as follows:

float cos(float a);  
double cos(double a);  
long double cos(long double a);  
double cos(integral a);

Where a is the angle in radians, and the return type is a double-precision floating-point value that represents the cosine of the angle.

Return Type
The cos() function returns a double-precision floating-point value that represents the cosine of the angle.

Parameter
The cos() function takes one parameter: an angle in radians for which the cosine value needs to be calculated.

Example 1: cos() function in C++
Here is an example of how to use the cos() function in C++:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
  double angle = 45;
  double radians = angle * (3.14 / 180);
  double cos_value = cos(radians);
  cout << "The cosine value of " << angle << " degrees is " << cos_value << endl;
  return 0;
}

In this example, we first define an angle with the value of 45. We then convert this angle into radians using the formula radians = angle * (3.14 / 180). This is necessary because the cos() function in C++ takes the angle in radians as a parameter. We then call the cos() function with the angle in radians and store the result in the cos_value variable. Next, we printed the result using the cout statement.

Example 1: cos() function in C++ through User Input
The following code will take user input and calculates the cosine of an angle using cos() function:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
    double angle;
    cout << "Enter an angle in radians: ";
    cin >> angle;
    cout << "cos(" << angle << ") = " << cos(angle) << endl;
    return 0;
}

This is a C++ code that takes user input for an angle in radians and calculates the cosine of the angle using the cos() function. The result is then printed on the console.

Conclusion

The cos() in C++ finds the cosine of an angle. It is a part of the math.h library and takes an angle in radians as a parameter. The cos() returns the cosine of angle using a mathematical formula and returns the value. It can be used in various applications, including graphics and scientific computing.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.