C++ std::tan() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

In C++, std::tan(x) calculates the tangent of an angle. In a right triangle, the tangent is the side opposite the angle divided by the side next to it. It is also the slope of a line that rises at that angle.

The angle must be in radians. Unlike sine and cosine, the tangent has no limit: it grows without end as the angle gets close to 90 degrees. It is used in surveying, in graphics, and to find the height of something from a distance.

std::tan() is declared in <cmath> and is part of the group Trigonometry. Trigonometric functions use radians, not degrees. To convert degrees, multiply by std::numbers::pi / 180 (C++20).

Syntax

Include <cmath> and call std::tan. Start with these lines.

C++
double a = std::tan(0.0);                    // 0
double b = std::tan(std::numbers::pi / 4);   // 1 (45 degrees)
float  c = std::tan(0.5f);                   // float version
CallWhat it does
std::tan(0.0)The tangent of 0 radians. Returns 0.
std::tan(std::numbers::pi / 4)pi / 4 radians is 45 degrees. Returns about 1.
std::tan(35 * std::numbers::pi / 180)A degree angle after conversion. Returns about 0.700208.

Angles are in radians. Convert degrees with deg * std::numbers::pi / 180. std::numbers::pi needs <numbers> and C++20. With older compilers write const double pi = 3.14159265358979323846; yourself.

Library declarations
C++
float       tan(float x);
double      tan(double x);
long double tan(long double x);
double      tan(Integer x);   // for integer types

Parameters

ParameterDescription
xRequired. The angle in radians. It can be float, double, long double, or an integer.

Return Value

Return ValueDescription
TangentThe value of sin(x) / cos(x). It can be any real number.
Very largeNear pi / 2 the result is huge, such as 1.63312e+16, but not inf.
NaNReturned when x is infinity or NaN.

C++ Examples

Example 1 — Tangent of Common Angles

Convert each angle from degrees to radians, then print the tangent with four decimals.

C++
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numbers>

int main() {
    std::cout << std::fixed << std::setprecision(4);

    for (int deg : {0, 30, 45, 60}) {
        double rad = deg * std::numbers::pi / 180.0;
        std::cout << "tan(" << deg << " deg) = " << std::tan(rad) << '\n';
    }
    return 0;
}

How It Works

  1. The loop converts every angle to radians and passes it to std::tan().
  2. tan(0) is 0. At 45 degrees the rise equals the run, so the tangent is exactly 1.0000.
  3. The values grow faster as the angle increases: 0.5774 at 30 degrees and 1.7321 at 60 degrees. The last one is sqrt(3).

Example 2 — The Problem Near 90 Degrees

Watch the tangent explode as the angle gets close to 90 degrees.

C++
#include <cmath>
#include <iostream>
#include <numbers>

int main() {
    const double toRad = std::numbers::pi / 180.0;

    std::cout << "tan(89.9 deg) = " << std::tan(89.9 * toRad) << '\n';
    std::cout << "tan(90 deg)   = " << std::tan(90.0 * toRad) << '\n';
    std::cout << "tan(90 deg) is inf: " << std::isinf(std::tan(90.0 * toRad)) << '\n';
    return 0;
}

How It Works

  1. At 89.9 degrees the line is almost straight up, and the slope is already about 573.
  2. At 90 degrees the real tangent is infinite, but the program prints 1.63312e+16, because pi is not stored exactly.
  3. std::isinf() prints 0, which proves that the result is huge but still a normal number. Do not rely on getting inf here.

Example 3 — Height of a Tree from a Distance

You stand 20 metres from a tree and measure the angle up to its top. The height is distance * tan(angle).

C++
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numbers>

int main() {
    double distance = 20.0;
    const double toRad = std::numbers::pi / 180.0;

    std::cout << std::fixed << std::setprecision(2);
    for (int angle : {35, 50}) {
        std::cout << "Angle " << angle << ": height "
                  << distance * std::tan(angle * toRad) << " m\n";
    }
    return 0;
}

How It Works

  1. The tangent is the height divided by the distance on the ground, so multiplying it by 20 gives the height.
  2. At 35 degrees, tan is about 0.7002, so the tree is 14.00 m tall.
  3. A bigger angle means a taller tree: at 50 degrees tan is about 1.1918, giving 23.84 m. This shows how surveyors measure heights.

Frequently Asked Questions

std::tan(x) returns the tangent of the angle x, which is sin(x) divided by cos(x). The angle must be in radians.
The tangent of x as float, double, or long double. Near 90 degrees it becomes a huge number. If x is infinity or NaN, the result is NaN.
In math it is undefined (infinite). In C++ you get a very large number such as 1.63312e+16, because pi / 2 is not exact. It does not return inf. Avoid angles close to 90 degrees.
tan(x) equals sin(x) / cos(x). That is why it gets huge when cos(x) is close to zero, near 90 and 270 degrees.
Include <cmath> and write std::tan(). That always works and picks the C++ overloads. The global name ::tan often works too, but the standard does not promise it, and it may only take a double.

Next: tanh()

Keep going through the <cmath> functions. Hyperbolic tangent of a number.

tanh() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful