Inverse Trigonometric Functions
The trig functions turn an angle into a ratio. Their inverses go the other way: given a ratio, which angle produced it? Because sine and cosine repeat forever, the answer is only unique once we restrict the angles, and those restrictions matter a lot in code.
The problem: many angles, one value
. Infinitely many angles share each sine value, so sine has no inverse on its whole domain. A function needs to be one-to-one to have an inverse (see Composition and Inverse Functions). The fix is to keep only one stretch of the graph where the function takes every value exactly once, and invert that piece.
| Function | Restricted to | Inverse | Domain of inverse | Range of inverse (principal value) |
|---|---|---|---|---|
| all reals |
Note: means , not . The reciprocal is . In code the names are
asin,acos,atan.
The restricted intervals are chosen so that each covers all output values once, includes the first quadrant and is a single unbroken piece.
Using them
import math
print("asin(0.5) =", round(math.degrees(math.asin(0.5)), 6), "°")
print("acos(-0.5) =", round(math.degrees(math.acos(-0.5)), 6), "°")
print("atan(-1) =", round(math.degrees(math.atan(-1)), 6), "°")
print("atan(1e6) =", round(math.degrees(math.atan(1e6)), 6), "° (approaches 90° but never reaches it)")
try:
math.asin(1.0000001)
except ValueError as e:
print("asin(1.0000001):", e)asin(0.5) = 30.0 °
acos(-0.5) = 120.0 °
atan(-1) = -45.0 °
atan(1e6) = 89.999943 ° (approaches 90° but never reaches it)
asin(1.0000001): math domain error
The last line is a real-world bug: computing acos(dot(u, v) / (|u||v|)) for two nearly parallel unit vectors can produce 1.0000000000000002 because of rounding, and the call crashes. Always clamp the argument to first.
Composing a function with its inverse
Outside the restricted range the second one fails: , not 150°.
A classic exercise is to simplify expressions like . Let , so with in , where . Then
A right triangle with opposite and hypotenuse 1 gives the same result. SymPy agrees:
import sympy as sp
x = sp.Symbol("x", real=True)
print("cos(asin x) =", sp.cos(sp.asin(x)))
print("tan(acos x) =", sp.tan(sp.acos(x)))
print("asin(sin(5π/6)) =", sp.asin(sp.sin(5 * sp.pi / 6)))
print("acos(cos(-π/3)) =", sp.acos(sp.cos(-sp.pi / 3)))cos(asin x) = sqrt(1 - x**2)
tan(acos x) = sqrt(1 - x**2)/x
asin(sin(5π/6)) = pi/6
acos(cos(-π/3)) = pi/3
atan2: the angle of a vector
To get the direction of a vector , the naive formula atan(y / x) has two problems. It fails when , and it cannot tell from , since both give . The two-argument function atan2(y, x) looks at the signs of and separately and returns the true angle in :
Note the argument order: first, then , in Python, C, JavaScript, NumPy and most other languages (Excel's ATAN2 is the exception, with first).
import math
points = [(1, 1), (-1, 1), (-1, -1), (1, -1), (0, 2), (-3, 0)]
print(" point atan(y/x) atan2(y, x)")
for x, y in points:
naive = "undefined" if x == 0 else f"{math.degrees(math.atan(y / x)):7.1f}°"
print(f"{str((x, y)):9} {naive:>10} {math.degrees(math.atan2(y, x)):7.1f}°") point atan(y/x) atan2(y, x)
(1, 1) 45.0° 45.0°
(-1, 1) -45.0° 135.0°
(-1, -1) 45.0° -135.0°
(1, -1) -45.0° -45.0°
(0, 2) undefined 90.0°
(-3, 0) -0.0° 180.0°
The last row shows another trap: is −0.0, so the naive formula says the vector points along the positive x-axis, the exact opposite of the truth. atan2 gets every row right.
In software
- Aiming. To make a turret, an enemy or a camera face a target, compute
angle = atan2(target.y − me.y, target.x − me.x). That one line is in nearly every 2D game (atan2 in game math, Trig in Games). - Angle between vectors with
acos(clamp(cos_value, −1, 1)), or more robustlyatan2(|u × v|, u · v), which is accurate even for tiny angles. - Inverse kinematics, robotics and geolocation (latitude from 3D coordinates uses
asinoratan2). - Angles in . atan2 returns ; add
% (2*math.pi)if you need a compass-style angle.
import math
def aim(me, target):
return math.degrees(math.atan2(target[1] - me[1], target[0] - me[0]))
def clamp(v, lo, hi):
return max(lo, min(hi, v))
print("aim from (2, 3) to (5, 7):", round(aim((2, 3), (5, 7)), 3), "°")
print("aim from (2, 3) to (-4, 3):", round(aim((2, 3), (-4, 3)), 3), "°")
a = 8 / 37
u = v = (math.cos(a), math.sin(a)) # the same unit vector twice
c = u[0]*v[0] + u[1]*v[1] # should be exactly 1, rounds above it
print("dot product:", c)
try:
math.acos(c)
except ValueError as e:
print("acos(dot):", e)
print("safe angle:", math.acos(clamp(c, -1.0, 1.0)))aim from (2, 3) to (5, 7): 53.13 °
aim from (2, 3) to (-4, 3): 180.0 °
dot product: 1.0000000000000002
acos(dot): math domain error
safe angle: 0.0
Exercises
Exercise 1: Exact principal values
Find , and in radians.
Answer
, , .
Solution
import sympy as sp
print(sp.asin(-sp.sqrt(3)/2), sp.acos(-sp.sqrt(2)/2), sp.atan(sp.sqrt(3)))-pi/3 3*pi/4 pi/3
Exercise 2: Simplify a composition
Simplify .
Answer
.
Solution
Let , so with in . Draw a right triangle with opposite and adjacent 1; the hypotenuse is .
The sign is right for negative too, since and have the same sign in .
Exercise 3: When the inverse does not undo
Evaluate and exactly.
Answer
and .
Solution
import math
print(round(math.acos(math.cos(5 * math.pi / 3)), 6), round(math.pi / 3, 6))
print(round(math.asin(math.sin(2)), 6), round(math.pi - 2, 6))1.047198 1.047198
1.141593 1.141593
Exercise 4: Turn toward the target
A ship at (10, 10) faces 30° (math angle). A buoy is at (4, 18). Use atan2 to find the bearing to the buoy, then the signed turn in the ship must make.
Answer
, (counter-clockwise).
Solution
import math
bearing = math.degrees(math.atan2(18 - 10, 4 - 10))
turn = (bearing - 30 + 180) % 360 - 180
print(round(bearing, 2), round(turn, 2))126.87 96.87
Test yourself
What is the range of arccos?
What does math.atan2(-1, -1) return in degrees?
- 45°
- −45°
- 135°
- −135°
Why should you clamp before calling acos on a computed cosine?
- To convert to degrees
- Rounding can push the value slightly outside , which raises a domain error or returns NaN
- acos only accepts positive numbers
- Clamping makes it faster