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)
Call
What 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
Parameter
Description
x
Required positional finite angle in radians (int or float).
Return Value
Return Value
Description
float
Cosine 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")
Output
tilt= 0 deg -> footprint=2.0000 m
tilt=30 deg -> footprint=1.7321 m
tilt=60 deg -> footprint=1.0000 m
tilt=90 deg -> footprint=0.0000 m
How It Works
Flat at zero tilt. At 0 degrees, cosine equals one, giving the full 2.0000 m length.
Half span at sixty. At 60 degrees, cosine is one half, shrinking the footprint to 1.0000 m.
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))
Output
third side: 7.0000
True
How It Works
5-7-8 triangle identity. Since cos(60 deg) = 0.5, 25 + 64 - 40 = 49 gives an exact third side of 7.0000.
Radians conversion. Converting 60 degrees first ensures cosine evaluates at pi / 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.