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.
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 - and + .
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:
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 radiansdouble x = 0.5;// result is in doublecout << "tan(x) = " << tan(x) << endl;return 0;}
Free Resources