C++ Math tan() Function

Last Updated : 16 May 2026

Math tan() Function

In C++, the tan() function is defined in the <cmath> header file and is used to calculate the tangent of an angle specified in radians. The tangent of an angle in trigonometry is defined as the ratio of the opposite side to the adjacent side of a right-angled triangle.

The tan() function supports different data types such as float, double, long double, and complex numbers. It is commonly used in trigonometry, physics, engineering, graphics programming, and scientific calculations.

tan(X) = opposite/Adjacent

Syntax

It has the following syntax:

Parameters

It represents the value that is given in radians.

Return value

It returns the tangent of an angle given in radians.

Examples of Math tan() Function

Here, we are going to discuss several examples to demonstrate the Math tan() Function

Example 1: Find the Tangent of a Positive Angle Using tan()

This example demonstrates how to calculate the tangent of a positive angle by converting degrees into radians and using the tan() function.

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.

Example 2: Find the Tangent of a Negative Angle Using tan()

This example demonstrates how the tan() function calculates the tangent of a negative angle when the degree value is negative.

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.

Example 3: Calculate tan(45 Degrees) Using tan()

This example demonstrates how to calculate the tangent of 45 degrees using the tan() function in C++.

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.

Example 4: Use tanf() and tanl() Functions in C++

This example demonstrates how to use the tanf() and tanl() functions to calculate tangent values with different precision levels.

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++ Math tan() Function

Example 5: Find the Tangent of Complex Numbers Using tan()

This example demonstrates how to calculate the tangent of a complex number using the tan() function from theheader file.

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.