Python math.acos() Function

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

Definition

math.acos(x) returns the arc cosine (inverse cosine) of x in radians, within the principal range [0, pi].

Like math.asin(), it requires -1 <= x <= 1 and raises ValueError outside that range. It is the standard tool for finding the unsigned angle between two vectors.

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

Syntax

Import math and pass a cosine ratio in [-1, 1].

python
import math

angle_rad = math.acos(cos_value)
CallWhat it does
math.acos(1)Returns 0.0.
math.acos(0)Returns approximately 1.570796... (pi / 2).
math.acos(-1)Returns 3.141592653589793 (math.pi).
math.acos(2)Raises ValueError: outside [-1, 1].

Always clamp dot-product cosines into [-1.0, 1.0] before calling math.acos() so tiny float drift does not raise ValueError.

Function signatures
python
math.acos(x, /)
# returns: float arc cosine in [0, pi] for -1 <= x <= 1

Parameters

ParameterDescription
xRequired positional number (int or float) between -1 and 1 inclusive.

Return Value

Return ValueDescription
floatAngle in radians in [0.0, math.pi]; returns nan for nan.

Python Examples

Example 1 — Find the Angle Between Two 2D Robot Vectors

Compute normalized dot products and convert math.acos() radians into degrees.

python
import math


def angle_deg(u, v):
    dot = u[0] * v[0] + u[1] * v[1]
    cos_t = dot / (math.hypot(*u) * math.hypot(*v))
    cos_t = max(-1.0, min(1.0, cos_t))
    return math.degrees(math.acos(cos_t))


print(f"45 deg pair: {angle_deg((3, 0), (3, 3)):.1f}")
print(f"right angle: {angle_deg((1, 0), (0, 1)):.1f}")
print(f"opposite   : {angle_deg((2, 0), (-5, 0)):.1f}")

How It Works

  1. Dot product ratio. Dividing u . v by both vector lengths via math.hypot() gives the cosine of the angle between them.
  2. Defensive clamp. Clamping inside [-1.0, 1.0] prevents ValueError on parallel vectors where rounding nudges past 1.0.
  3. Full 0 to 180 range. Orthogonal vectors report 90.0 and opposite vectors report 180.0 degrees.

Example 2 — Compute Spherical Arc Distance Between Two Coordinates

Use the spherical law of cosines with math.acos() to measure distance along a sphere.

python
import math

earth_km = 6371.0
lat1, lon1 = math.radians(0.0), math.radians(0.0)
lat2, lon2 = math.radians(0.0), math.radians(90.0)

cos_d = (
    math.sin(lat1) * math.sin(lat2)
    + math.cos(lat1) * math.cos(lat2) * math.cos(lon2 - lon1)
)
arc = math.acos(max(-1.0, min(1.0, cos_d)))
print(f"quarter equator: {earth_km * arc:.2f} km")

How It Works

  1. Convert lat/lon to radians. Degrees are converted before evaluating spherical trig terms.
  2. Central angle via acos. Ninety degrees along the equator yields a central angle of pi / 2 radians.
  3. Multiply by radius. Scaling by Earth radius gives ~10,007.54 km, one quarter of the equator.

Example 3 — Test Exact Endpoints and Catch Out-of-Range Errors

Check endpoints 1, 0, and -1 and handle values outside [-1, 1].

python
import math

print(math.acos(1))
print(math.acos(-1) == math.pi)
print(round(math.degrees(math.acos(0)), 1))

for x in [-1.5, 2.0]:
    try:
        math.acos(x)
    except ValueError:
        print(f"acos({x}) raises ValueError")

How It Works

  1. Exact boundary endpoints. acos(1) is zero and acos(-1) equals math.pi.
  2. Ninety degrees at zero. acos(0) returns pi / 2, converting to 90.0 degrees.
  3. Strict interval check. Both -1.5 and 2.0 raise ValueError immediately.

Frequently Asked Questions

It returns the inverse cosine angle in radians, always between 0.0 and math.pi (0 to 180 degrees).
Cosine values of real angles never leave [-1, 1]. Clamp computed cosines with max(-1.0, min(1.0, x)) before calling math.acos().
Use acos() for the unsigned angle (0 to 180 deg) between two vectors; use atan2(y, x) when you need a signed direction (-180 to 180 deg) in 2D.
Exactly math.pi (3.141592653589793), representing 180 degrees.
Import cmath and call cmath.acos(x).

Next: math.acosh()

Return the inverse hyperbolic cosine of x >= 1 as a float.

acosh() →

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.

8 people found this page helpful