In the C++ programming language, the math tan function finds the tangent of an angle specified in terms of radians. It is part of the <cmath> header file. It can take arguments of type float, double, or long double, and return a value equivalent to the tangent of the given angle. The tangent of an angle x in mathematics is defined as the side opposite to the angle divided by the side adjacent to it in a right-angled triangle, written as:
tan(X) = opposite/Adjacent
Syntax
It has the following syntax:
float tan(float x);
double tan(double x);
long double tan(long double x);
double tan(integral x);
In this syntax,
- x: It represents the value that is given in radians.
- It returns the tangent of an angle given in radians.
Return value
Key Points to Remember
There are several key points of the math tan function in C++ . Some of them are as follows:
- We can use M_PI (from <cmath>) for π when doing degree-to-radian conversions.
- It is not defined at odd multiples of π/2.
- It returns the result as a float.
- It can operate on float, double, long double, and complex arguments.
- It contains several overloaded functions that provide type safety.
- The tanf and tanl functions are provided with specific precision.
- The tangent function is periodic every π radians.
C++ Simple math tan function Example
Let us take the simple example to illustrate the math tan function in C++.
Example
#include <iostream>
#include<math.h>
using namespace std; //using standard namespace
int main() //main function
{
float degree=10;
float radian=degree*3.14/180;
cout<<"Tangent of an angle is : "<<tan(radian);
return 0;
}
Output:
Tangent of an angle is : 0.176236
Explanation:
In this example, we have taken a float degree of 10. After that, we use the tan function that finds the tangent of an angle when the value of degrees is equal to 10.
C++ Example to Find the Tangent of an Angle
Let us take an example to illustrate the math tan function when the value of the degree is negative.
Example
#include <iostream>
#include<math.h>
using namespace std; //using standard namespace
int main() //main function
{
float degree= -60;
float radian=degree*3.14/180;
cout<<"Tangent of an angle is :"<<tan(radian);
return 0;
}
Output:
Tangent of an angle is :-1.72993
Explanation:
In this example, we have taken a float degree -60. After that, we have taken the tan function that finds the tangent of an angle when the value of the degree is negative, i.e., -60.
C++ Example to Calculate tan(45 Degrees) Using the tan Function
Now, we are taking an instance to demonstrate how to calculate the tan (45 degrees) using the tan function in C++.
Example
#include <iostream>
#include <cmath>
using namespace std; //using standard namespace
int main() { //main function
double a = 0.785398;
double result = tan(a);
cout << "tan(45 degrees) = " << result << endl;
return 0;
}
Output:
tan(45 degrees) = 1
Explanation:
In this example, we find the tangent of 45 degrees by using the tan function from the <cmath> library. The number 0.785398 is the radian value of 45 degrees (as π/4≈0.785398). The function tan(a) calculates the tangent of this angle and places the result in the variable result.
tanf and tanl functions
There are two functions of the math tan function in C++. These are as follows:
tanf Function
In the C++ programming language, the tanf function is commonly utilized to calculate the tangent of an angle in radians and returns the answer in a float type. It is the single-precision (float) implementation of the usual tan function.
tanl Function
In the C++ programming language, the tanl function is commonly utilized to calculate the tangent of an angle (in radians) and returns the result as a long double value. It is the extended-precision tan function, which provides greater precision for large or extremely small arguments.
C++ Example for tanf and tanl
Now, we are taking an instance to illustrate the tanf and tanl functions in C++.
Example
#include <iostream>
#include <cmath>
using namespace std; //using standard namespace
int main() { //main function
float a = 0.5f;
long double b = 0.5L;
cout << "tanf(0.5) = " << tanf(a) << endl;
cout << "tanl(0.5) = " << tanl(b) << endl;
return 0;
}
Output:
tanf(0.5) = 0.546302
tanl(0.5) = 0.546302
Explanation:
In this example, we illustrate the tanf and tanl functions to calculate the tangent of 0.5 radians at various levels of precision. The variable a is a float and is sent to tanf, and the latter returns the result as single precision. The variable b is a long double and is sent to tanl, and the latter returns the result with higher precision.
Tangent of Complex Numbers
In C++, the tangent of a complex number is determined using the tan function from the <complex> header. It calculates the tangent of a complex number z=a+bi (where a is the real part and b is the imaginary part) and gives a complex result.
Mathematically, it is expressed as:
C++ Example to find the Tangent of Complex Numbers using the math tan function
Now, we are taking an instance to demonstrate how to find the tangent of complex numbers using the math tan function in C++.
Example
#include <iostream>
#include <complex>
using namespace std; //using standard namespace
int main() { //main function
complex<double> a(2.0, 3.0);
cout << "tan(2 + 3i) = " << tan(a) << endl;
return 0;
}
Output:
tan(2 + 3i) = (-0.00376403,1.00324)
Explanation:
In this example, we find the tangent of the complex number 2+3i based on the tan function from the <complex> library. The variable a is the complex number whose real part is 2 and imaginary part is 3. After that, the function tan(a) is utilized to find its complex tangent and returns another complex number, which is shown in the output.
Conclusion
In conclusion, the tan function is a built-in mathematical function in the STL. It is commonly used to find the tangent of the given radians. When we utilize the math tan function, the degree angles should be changed to radians for correct results. It is also helpful to return the floating-point value that represents the sine and cosine of the given angle. It can be useful to perform trigonometric calculations in several fields, including engineering, scientific, and graphics applications.
C++ Math tan Function FAQs
1) What is the use of the math tan function in C++?
The C++ math function is commonly used to find the tangent of an angle.
2) Does the tan function take the angle in radians or degrees in C++?
In C++, the tan function uses radians to take the angles rather than degrees.
3) How are tan and atan different from each other?
The main difference between the tan and the atan function is that the tan function is commonly used to return the tangent of angle x. In contrast, the atan(x) function is commonly used to return the angle whose tangent is x (inverse tangent).
4) Why is the tan function occasionally slower than sin or cos?
In C++, certain implementations of the standard library calculate the tan(x) function internally in terms of sin(x)/cos(x). The tan function contains two trigonometric evaluations and a division operation. It makes this slower than the standalone sin or cos functions, which can perform only one trigonometric calculation.
5) What is the return type of the tan function in C++?
The return type of the tan function depends on the input. It can be
- Float: It returns the float value.
- Double: It returns the double value.
- long double: It returns the long double value.