all Python 3mathterm
math.acos
trigonometry
The arc cosine of a value. It takes a value in [-1, 1] and returns an angle in radians in [0, π].
Syntax
math.acos(x)
The usual crash is ValueError: math domain error, when floating-point rounding pushes the argument just past 1.0 or -1.0 by about 1e-16 — typical for cosine similarity or a normalised dot product. Clamp it before the call with max(-1.0, min(1.0, x)). The result is in radians; math.degrees() converts it.
Examples
import math
print(math.acos(1)) # → 0.0
print(math.acos(0)) # → 1.5707... (π/2)
print(math.acos(-1)) # → 3.1415... (π)
print(math.acos(0.5)) # → 1.0471... (π/3)
print(math.degrees(math.acos(0.5))) # → ? (значение зависит от системной libm — карточка учит наличию погрешности, а не её цифрам)
print(math.degrees(math.acos(0))) # → 90.0
See also
Open in the full glossarydocs.python.orgReport an inaccuracy