rguides

tan()

tan(x)

The tan() function computes the trigonometric tangent of numeric values in R. It operates element-wise on vectors and accepts angles in radians.

Syntax

tan(x)

Parameters

ParameterTypeDefaultDescription
xnumericNumeric vector or single value representing an angle in radians

The tangent function is a C-level primitive in R, so it operates on numeric vectors without any iteration overhead. It handles NA propagation correctly — if an input element is NA, the corresponding output is also NA. The function also works with complex numbers, returning the complex tangent according to standard branch-cut rules, though the examples below focus on real-valued inputs which cover the vast majority of applied use cases.

Examples

Basic usage

# Compute tangent for a single angle (pi/4 = 45 degrees)
tan(pi / 4)
# [1] 1

tan(0)
# [1] 0

tan(pi)
# [1] 1.224606e-16  # essentially 0

tan() operates element-wise, so passing a vector of angles returns a vector of tangents — each element computed independently. You can calculate tangents for an entire sequence in a single call without a loop. Passing c(0, pi/6, pi/4, pi/3) yields the tangents for 0°, 30°, 45°, and 60° in one expression. The vectorized form is cleaner and faster than calling tan() four times individually.

Working with vectors

# Compute tangent for multiple angles
angles <- c(0, pi/6, pi/4, pi/3, pi/2)
tan(angles)
# [1] 0.0000000 0.5773503 1.0000000 1.7320508        Inf

Note that tan(pi/2) returns Inf because cosine approaches zero at π/2. Division by a value that rounds to zero in floating-point arithmetic produces infinity rather than an error. The tangent function has vertical asymptotes at every odd multiple of π/2 — it is mathematically undefined at those points — but R handles the edge case gracefully by returning Inf or -Inf with a warning rather than halting execution.

Converting degrees to radians

# Convert degrees to radians before using trig functions
deg_to_rad <- function(deg) deg * pi / 180

tan(deg_to_rad(45))
# [1] 1

tan(deg_to_rad(30))
# [1] 0.5773503

Trigonometric functions underpin many periodic modeling tasks in R. The tangent function specifically can be used to generate asymmetric periodic patterns — unlike sine and cosine which oscillate smoothly between -1 and 1, the tangent sweeps from negative infinity to positive infinity across each period. When plotting tangent-derived data, you must avoid the asymptote points by restricting the domain to intervals that exclude odd multiples of π/2.

Common patterns

Creating periodic data

# Generate tangent wave (avoid pi/2 multiples)
x <- seq(0, pi/2 - 0.1, length.out = 50)
y <- tan(x)
plot(x, y, type = "l")

tan() in practice

tan() computes the tangent, the ratio of sine to cosine: tan(x) == sin(x) / cos(x). It is defined for all x except where cos(x) == 0, at which points it returns Inf or -Inf (with a warning for NaN at exactly those points in floating-point arithmetic). In practice, cos(pi/2) is approximately 6e-17 rather than exactly 0, so tan(pi/2) returns approximately 1.6e16 rather than Inf.

tan() has period pi, unlike sin() and cos() which have period 2*pi. It is used in geometry for computing slopes from angles, converting between angle-based and slope-based parameterizations, and in probability distributions like the Cauchy distribution, which is defined as the ratio of two independent standard normal variables.

atan() (arctangent) is the inverse function. atan(tan(x)) recovers x for values in the range (-pi/2, pi/2). For full-circle angle recovery from (x, y) coordinates, use atan2(y, x), which correctly handles all four quadrants. atan() is bounded by (-pi/2, pi/2), while atan2() returns values in (-pi, pi].

tan() vectorizes over its input: tan(c(0, pi/4, pi/3)) returns c(0, 1, sqrt(3)). It propagates NA and returns NaN for NaN input.

tan() accepts radians. The function is undefined at pi/2, 3*pi/2, etc., where cosine equals zero; R returns Inf or -Inf at these points due to floating-point behavior rather than raising an error. atan() is the inverse; atan2(y, x) computes the angle correctly for all four quadrants. tan() is vectorized and propagates NA. For degree-based input, convert with x * pi / 180 before calling.

tan() accepts values in radians. Near the vertical asymptotes at (2k+1) * pi/2, the floating-point result is a large finite number rather than exactly Inf because pi is stored as an approximation. For example, tan(pi/2) returns approximately 1.633e+16, not Inf. Test for near-vertical values by checking abs(cos(x)) < 1e-10 rather than relying on tan(x) == Inf.

To convert degrees to radians before computing tangent, multiply by pi / 180. The inverse function atan() returns values in (-pi/2, pi/2). Use atan2(y, x) for the four-quadrant inverse tangent that maps Cartesian coordinates to angles in (-pi, pi] — this is the correct form for computing bearings and angles in 2D geometry.

tan() is vectorized and propagates NA. It is a C primitive and operates efficiently on large numeric vectors without the overhead of an apply loop.

Computing slopes from angles

# Surveying: compute the gradient (rise over run) from an angle
angles_deg <- c(5, 10, 15, 30, 45)
angles_rad <- angles_deg * pi / 180

# Slope as a percentage (rise / run * 100)
slopes_percent <- tan(angles_rad) * 100
data.frame(angle_deg = angles_deg, slope_percent = round(slopes_percent, 1))
#   angle_deg slope_percent
# 1         5           8.7
# 2        10          17.6
# 3        15          26.8
# 4        30          57.7
# 5        45         100.0

# A 45° angle equals a 100% slope (1:1 rise-to-run ratio)

The tangent converts an angle into a slope ratio directly, which is why road-grade signs expressing gradients as percentages are computed with tan(). A 5° incline corresponds to an 8.7% grade — the road rises 8.7 metres for every 100 metres of horizontal travel. At exactly 45°, the rise equals the run, producing a 100% slope. Surveying, civil engineering, and computer graphics all use this relationship to convert between angular and Cartesian representations of inclination.

See also