English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Reference Manual

C++ Library Function <cmath>

C ++tan() Function Usage and Example

This function returns the tangent value of the angle (parameter) in radians as returned by the tan() function in<cmath>defined in the header file.

[Math] tan x = tan(x)

tan() prototype (from C ++ 11standard starting)

double tan(double x);
float tan(float x);
long double tan(long double x);
double tan(T x); //is of integer type

tan() parameter

The tan() function takes a mandatory parameter in radians (can be positive, negative, or 0).

The return value of tan()

The tan() function returns[-∞, ∞]values within the range.

Example1:How does tan() work in C ++What is working in it?

#include <iostream>
#include <cmath>
using namespace std;
int main()
{ 
  long double x = 0.99999, result;
  result = tan(x);
  cout << "tan(x) = " << result << endl;
  
  double xDegrees = 60.0;
  //Utilize the tan() function to convert degrees to radians
  result = tan(xDegrees*3.14159/180);
  cout << "tan(x) = " << result << endl;
  return 0;
}

When running the program, the output is:

tan(x) = 1.55737
tan(x) = 1.73205

Example2: tan() function with integer type

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
  long int x = 6;
  double result;
  result = tan(x);
  cout << "tan(x) = " << result;
  
  return 0;
}

When running the program, the output is:

tan(x) = -0.291006

 C++ Library Function <cmath>