How to use tan() in C++

In trigonometry, the tangent function of a right-angled triangle is the ratio between the adjacent and opposite sides of a right triangle.

Figure 1

The tan function in C++ works precisely like the tangent in trigonometry. The return value of the tan function is the tangent of an angle given in radian.

To use the tan function, the cmath header file needs to be included in the program, as shown below:

#include <cmath>

Parameters

The tan function takes one parameter, i.e., the angle. This parameter should be a numeric number and be in a radian.

Return value

The tan function returns the value between - \infty and + \infty .

The function can have the return value of the following three types:

  • float
  • double
  • long double

These types depend on the type of arguments the programmer gives to the functions.

Figure 2 below shows the types of return types when different argument types are passed:

Figure 2

Example

The code below shows how the tan function works in C++:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
// the value of x is in radians
double x = 0.5;
// result is in double
cout << "tan(x) = " << tan(x) << endl;
return 0;
}
Copyright ©2024 Educative, Inc. All rights reserved