C++ Math Tan Function - C++ Programming Tutorial
C++ Course / Math Functions / C++ Math Tan Function

C++ Math Tan Function

BLUF: Mastering C++ Math Tan Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Math Tan Function

C++ is renowned for its efficiency. Learn how C++ Math Tan Function enables low-level control and high-performance computing in the tutorial below.

In C++, the tan function in the math library calculates the tangent of an angle provided in radians. This function is included in the cmath header file. tan works with float, double, or long double data types, returning the tangent value corresponding to the input angle. The mathematical definition of tangent for an angle x is the ratio of the side opposite to the angle to the side adjacent to it in a right triangle, expressed as:

tan(X) = opposite/Adjacent

Syntax

It has the following syntax:

Example

float tan(float x);  

double tan(double x);  

long double tan(long double x);  

double tan(integral x);

In this specific syntax,

  • x signifies the numerical value provided in radians.

It provides the tangent of an angle specified in radians.

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's consider a basic example to demonstrate the functionality of the tan mathematical function in C++.

Example

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:

Output

Tangent of an angle is : 0.176236

Explanation:

In this instance, a floating point angle of 10 degrees is selected. Subsequently, the tan method is applied to calculate the tangent of a 10-degree angle.

C++ Example to Find the Tangent of an Angle

Let's consider an instance to demonstrate the mathematical tan function when the degree value is negative.

Example

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:

Output

Tangent of an angle is :-1.72993

Explanation:

In this instance, we are considering a negative angle of -60 degrees. Subsequently, the tan function is applied to calculate the tangent of this negative angle.

C++ Example to Calculate tan(45 Degrees) Using the tan Function

Now, let's consider an example to illustrate the process of determining the tangent of 45 degrees by employing the tan function within the C++ programming language.

Example

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:

Output

tan(45 degrees) = 1

Explanation:

In this instance, the tangent of a 45-degree angle is determined using the tan function from the math library. The radian equivalent of 45 degrees, approximately π/4 which is 0.785398, is utilized for this calculation. By applying the tan(a) function, the tangent of the angle is computed and stored in the variable named result.

tanf and tanl functions

There are two purposes of the math tan function in C++. These are outlined below:

tanf Function

In C++ programming, the tanf function is frequently used to compute the tangent of an angle in radians and provides the result as a float data type. This function serves as the float version of the standard tan function.

tanl Function

In C++ programming, the tanl function is frequently used to compute the tangent of an angle measured in radians, yielding a long double outcome. This function serves as an extended-precision version of tan, offering enhanced accuracy for significant or very minute angles.

C++ Example for tanf and tanl

Presently, we will provide an example to demonstrate the functionality of the tanf and tanl functions within C++.

Example

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:

Output

tanf(0.5) = 0.546302

tanl(0.5) = 0.546302

Explanation:

In this instance, we demonstrate the utilization of the tanf and tanl functions to compute the tangent of 0.5 radians with different degrees of accuracy. The variable a, defined as a float, is passed to tanf which provides the result in single precision. On the other hand, the variable b, defined as a long double, is passed to tanl which returns a more precise result.

Tangent of Complex Numbers

In C++, the tangent of a complex number is calculated by utilizing the tan function from the <complex> header. This function is responsible for computing the tangent of a complex number z=a+bi (where 'a' represents the real part and 'b' represents the imaginary part), yielding a complex outcome.

Mathematically, it is expressed as:

C++ Example to find the Tangent of Complex Numbers using the math tan function

Now, let's consider an example to illustrate how to calculate the tangent of complex numbers by utilizing the tan function in the C++ programming language.

Example

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:

Output

tan(2 + 3i) = (-0.00376403,1.00324)

Explanation:

In this instance, we calculate the tangent of the complex number 2+3i using the tan function from the math library. The variable a represents the complex number with a real component of 2 and an imaginary component of 3. Subsequently, the tan(a) function is employed to determine its complex tangent, resulting in the output displaying another complex number.

Conclusion

In summary, the tan function is an inherent mathematical function within the Standard Template Library (STL). Its primary purpose is to calculate the tangent of specified radians. When employing the tan function from the math library, ensure that degree measurements are converted to radians to ensure accurate outputs. Additionally, this function can provide the floating-point values for both sine and cosine of the angle in question. This functionality proves beneficial for conducting trigonometric computations across various domains such as engineering, scientific research, and graphic design.

C++ Math tan Function FAQs

The math tan function in C++ is used to calculate the tangent of a given angle in radians.

The C++ math function is frequently employed to calculate the tangent of an angle.

The tan function in C++ operates on angles provided in radians, not degrees.

In the C++ programming language, the tan function operates with angles in radians instead of degrees.

3) What sets tan apart from atan in terms of functionality?

The primary contrast between the tan and atan functions lies in their purposes. While the tan function is frequently employed to calculate the tangent of an angle x, the atan(x) function is typically utilized to determine the angle whose tangent is x (inverse tangent).

4) What is the reason behind the sporadic slower performance of the tan function compared to sin or cos?

In C++, specific versions of the standard library internally compute the tan(x) function by using sin(x)/cos(x). The tan function involves two trigonometric calculations and a division operation, resulting in slower performance compared to the individual sin or cos functions, which require only one trigonometric evaluation.

5) What data type does the tan function in C++ return?

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.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience