The math.acos(x) function computes the arc cosine (inverse cosine) of a numeric ratio x. For input values between -1.0 and 1.0, it returns the principal angle in radians in the closed range [0, π] (or 0° to 180°).
Exact Inverse: math.acos() reverses math.cos() (math.cos(math.acos(x)) == x for any -1.0 ≤ x ≤ 1.0).
The math.acos() function is part of the standard math module:
import math
angle_radians = math.acos(x)
| Parameter | Type | Required? | Description |
|---|---|---|---|
x |
float or int |
Yes | A numeric ratio representing adjacent / hypotenuse. Must satisfy -1.0 ≤ x ≤ 1.0. |
Return Value: Returns a floating-point number (float) representing the angle in radians in the interval [0, π].
Evaluating arc cosine for key boundary and intermediate ratios:
import math
print("acos(1.0): ", math.acos(1.0)) # 0.0 radians (0 degrees)
print("acos(0.5): ", math.acos(0.5)) # 1.0471975511965979 (~pi / 3, 60 degrees)
print("acos(0.0): ", math.acos(0.0)) # 1.5707963267948966 (pi / 2, 90 degrees)
print("acos(-1.0):", math.acos(-1.0)) # 3.141592653589793 (pi, 180 degrees)
acos(1.0): 0.0
acos(0.5): 1.0471975511965979
acos(0.0): 1.5707963267948966
acos(-1.0): 3.141592653589793
To interpret angles in conventional compass or geometric degrees (0° to 180°), convert using math.degrees():
import math
ratios = [1.0, 0.8660254, 0.70710678, 0.5, 0.0, -0.5, -1.0]
for r in ratios:
rad = math.acos(r)
deg = math.degrees(rad)
print(f"acos({r:5.2f}) -> {rad:6.4f} rad == {deg:5.1f}°")
acos( 1.00) -> 0.0000 rad == 0.0°
acos( 0.87) -> 0.5236 rad == 30.0°
acos( 0.71) -> 0.7854 rad == 45.0°
acos( 0.50) -> 1.0472 rad == 60.0°
acos( 0.00) -> 1.5708 rad == 90.0°
acos(-0.50) -> 2.0944 rad == 120.0°
acos(-1.00) -> 3.1416 rad == 180.0°
Because the cosine ratio of any real angle can never exceed 1.0 or drop below -1.0, passing values outside this domain raises a ValueError: math domain error:
import math
try:
math.acos(1.01)
except ValueError as err:
print("Domain error caught:", err)
# Output: Domain error caught: math domain error
Defensive Clamping: In numerical programming (such as computing vector dot products), tiny floating-point rounding errors can occasionally yield 1.0000000000000002. Guard against crashes using max(-1.0, min(1.0, ratio)) before calling math.acos().
Given the three side lengths a, b, and c of an arbitrary triangle, the Law of Cosines allows you to solve for any internal angle:
c2 = a2 + b2 - 2ab · cos(C) ⇒ C = arccos((a2 + b2 - c2) / (2ab))
import math
a, b, c = 3, 4, 5 # Classic 3-4-5 right triangle
cos_C = (a**2 + b**2 - c**2) / (2 * a * b)
angle_C_deg = math.degrees(math.acos(cos_C))
print("Angle C:", angle_C_deg, "degrees") # 90.0 degrees
In 3D game engines, robotics, and machine learning (cosine similarity), the angle θ between two vectors u and v is calculated from their dot product:
u · v = |u| · |v| · cos(θ) ⇒ θ = arccos((u · v) / (|u| · |v|))
Visualizing the domain [-1, 1] and range [0, π] using Matplotlib:
import math
import matplotlib.pyplot as plt
x_vals = [i * 0.02 - 1.0 for i in range(0, 101)] # -1.0 to +1.0
y_vals = [math.acos(x) for x in x_vals]
plt.plot(x_vals, y_vals, label="acos(x)", color="#1a73e8")
plt.title("Inverse Cosine (Arc Cosine) Curve")
plt.xlabel("Input Ratio x [-1, 1]")
plt.ylabel("Angle in Radians [0, pi]")
plt.grid(True)
plt.show()
Let's construct a vector angle calculator that handles both 2D and 3D coordinate vectors, protects against numerical overflow/underflow, and verifies angles with unit assertions:
import math
def calculate_vector_angle(v1, v2):
dot_product = sum(a * b for a, b in zip(v1, v2))
mag1 = math.sqrt(sum(a**2 for a in v1))
mag2 = math.sqrt(sum(b**2 for b in v2))
if mag1 == 0 or mag2 == 0:
raise ValueError("Zero-length vectors do not possess an angle.")
# Clamp ratio strictly within [-1.0, 1.0] to eliminate floating-point drift
cos_theta = max(-1.0, min(1.0, dot_product / (mag1 * mag2)))
angle_rad = math.acos(cos_theta)
return round(math.degrees(angle_rad), 2)
# Unit Assertions
assert calculate_vector_angle([1, 0], [5, 0]) == 0.0 # Parallel (0 deg)
assert calculate_vector_angle([1, 0], [0, 1]) == 90.0 # Perpendicular (90 deg)
assert calculate_vector_angle([1, 0], [-1, 0]) == 180.0 # Opposite direction (180 deg)
assert calculate_vector_angle([1, 1, 0], [1, 0, 0]) == 45.0 # 3D vector 45 deg diagonal
print("Angle between [1, 0] and [0, 1]: ", calculate_vector_angle([1, 0], [0, 1]), "degrees")
print("Angle between [1, 1, 0] and [1, 0, 0]:", calculate_vector_angle([1, 1, 0], [1, 0, 0]), "degrees")
print("All vector angle calculations passed successfully!")
Open in Google Colab View on GitHub
Run all math.acos() examples interactively, test the Law of Cosines, solve dot product angles between 3D vectors, and verify assertions directly in Google Colab.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.