Python Glossary Русский

all Python 3mathterm

math.atan2

trigonometry

The arc tangent of y/x, taking the quadrant into account — it handles every combination of signs correctly. The result is in [-π, π].

Syntax

math.atan2(y, x)

The argument order is the reverse of how the fraction reads: y comes first, then x. Swapping them raises nothing — you just get a mirrored angle, which is a quiet and easily missed bug. Points below the x axis come back negative; if you need [0, 2π), normalise yourself with math.atan2(y, x) % (2 * math.pi).

Examples

import math
print(math.atan2(1, 1))    # → 0.7853... (π/4, I квадрант)
print(math.atan2(1, -1))   # → 2.3561... (3π/4, II квадрант)
print(math.atan2(-1, -1))  # → -2.3561... (III)
print(math.atan2(0, 1))    # → 0.0
print(math.degrees(math.atan2(1, 0)))  # → 90.0
print(math.degrees(math.atan2(-1, 0)))  # → -90.0

See also

Open in the full glossarydocs.python.orgReport an inaccuracy