Reference · Math

math.atan2

signature
math.atan2(num, num) -> float

What it does

The angle in radians, between -pi and pi, from the positive x axis to the point (x, y), as Python's math.atan2. The order is y first, then x. Unlike math.atan(y / x) it keeps the quadrant, and x may be zero.

Examples

print(math.atan2(1, 1), math.degrees(math.atan2(1, 1)))print(math.degrees(math.atan2(1, -1)), math.degrees(math.atan2(-1, -1)), math.degrees(math.atan2(-1, 1)))
output
0.7853981633974483 45.0135.0 -135.0 -45.0

Edge cases

print(math.atan2(1, 0), math.atan2(-1, 0))print(math.atan2(0, 0))print(math.atan2(0, -1), math.atan2(-0.0, -1))print(math.atan2(math.inf, math.inf), math.atan2(1, math.nan))
output
1.5707963267948966 -1.57079632679489660.03.141592653589793 -3.1415926535897930.7853981633974483 NaN

Errors

There is no domain error. A missing second argument, or a value that is not a number, is a runtime error, not an error value: it stops the request.

this one fails
print(math.atan2(1))
output
line 1: `math.atan2` expects a number as argument 2, got nothing
this one fails
print(math.atan2("1", 2))
output
line 1: `math.atan2` expects a number as argument 1, got str

Chaining

In a pipeline y comes first, so y | math.atan2(x) works. A point as a list is taken apart with let [x, y] = p:

let [x, y] = [3, 4]print(y | math.atan2(x) | math.degrees)print([[1, 0], [0, 1], [-1, 0]] | map((p) => math.degrees(math.atan2(p[1], p[0]))))
output
53.13010235415598[0.0, 90.0, 180.0]

See also

math.atan, math.hypot, math.degrees, math.sin, math.cos

Thread
8Useful? Like it, or add to its thread below.

Thread 0 posts

On math.atan2(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo