Python math.cos() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

math.cos(x) returns the trigonometric cosine of angle x (in radians) as a float between -1.0 and 1.0.

Because math.pi / 2 is a float approximation, math.cos(math.pi / 2) evaluates to 6.123233995736766e-17 rather than exact 0.0. Compare near zero using math.isclose() with abs_tol.

math.cos() is a function in Python's standard math module: import it with import math and call math.cos(...), or import the name directly with from math import cos.

Syntax

Import math and pass radians.

python
import math

x_proj = math.cos(angle_radians)
CallWhat it does
math.cos(0)Returns 1.0.
math.cos(math.pi)Returns -1.0.
math.cos(math.pi / 2)Returns 6.123233995736766e-17 (practically zero).
math.cos(float("-inf"))Raises ValueError: domain error.

For degree angles, write math.cos(math.radians(deg)). Even symmetry guarantees math.cos(-x) == math.cos(x).

Function signatures
python
math.cos(x, /)
# returns: float cosine of x radians

Parameters

ParameterDescription
xRequired positional finite angle in radians (int or float).

Return Value

Return ValueDescription
floatCosine of x in [-1.0, 1.0]; returns nan for nan.

Python Examples

Example 1 — Compute Horizontal Solar Panel Footprint by Tilt

Find the horizontal ground span length * math.cos(radians(tilt)) across panel tilt angles.

python
import math

panel_len = 2.0
for tilt in [0, 30, 60, 90]:
    ground = panel_len * math.cos(math.radians(tilt))
    print(f"tilt={tilt:>2} deg -> footprint={ground:.4f} m")

How It Works

  1. Flat at zero tilt. At 0 degrees, cosine equals one, giving the full 2.0000 m length.
  2. Half span at sixty. At 60 degrees, cosine is one half, shrinking the footprint to 1.0000 m.
  3. Vertical panel. At 90 degrees, four-decimal formatting drops the tiny 6.12e-17 residual cleanly to zero.

Example 2 — Find the Third Triangle Side With the Law of Cosines

Combine math.cos() and math.sqrt() on sides 5 and 8 separated by 60 degrees.

python
import math

a, b = 5.0, 8.0
angle = math.radians(60)
c = math.sqrt(a * a + b * b - 2.0 * a * b * math.cos(angle))
print(f"third side: {c:.4f}")
print(math.cos(-angle) == math.cos(angle))

How It Works

  1. 5-7-8 triangle identity. Since cos(60 deg) = 0.5, 25 + 64 - 40 = 49 gives an exact third side of 7.0000.
  2. Radians conversion. Converting 60 degrees first ensures cosine evaluates at pi / 3.
  3. Even function check. Negating the angle returns the identical cosine value.

Example 3 — Check cos(pi / 2) Residuals and Infinity Errors

Inspect math.cos(math.pi / 2) and catch ValueError on negative infinity.

python
import math

c90 = math.cos(math.pi / 2.0)
print(c90)
print(math.isclose(c90, 0.0, abs_tol=1e-15))

try:
    math.cos(float("-inf"))
except ValueError:
    print("cos(-inf) raises ValueError")

How It Works

  1. Near-zero output. Floating-point rounding in math.pi / 2 leaves 6.12e-17 instead of zero.
  2. Absolute tolerance check. Because relative tolerance fails against zero, pass abs_tol=1e-15 to math.isclose().
  3. Infinity rejected. Cosine has no limit at negative infinity and raises ValueError.

Frequently Asked Questions

math.pi is a finite float approximation of pi, so math.pi / 2 is not exact pi/2. Its cosine is ~6.12e-17, practically zero.
No, it expects radians. Call math.cos(math.radians(deg)) for degree inputs.
Cosine oscillates between -1 and 1 forever without converging, so infinite inputs have no defined value.
Use math.acos(c), which returns an angle in radians between 0 and math.pi.
Yes, cosine is an even function, so negating the input gives the exact same value.

Next: math.cosh()

Return the hyperbolic cosine of x as a float.

cosh() →

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