Func Number Atan2
## Python atan2() Function
The `math.atan2()` function in Python returns the arc tangent (inverse tangent) of the given $y$ and $x$ coordinates.
Unlike the standard arc tangent function `math.atan(y / x)`, which only covers a range of $-\pi/2$ to $\pi/2$ radians (Quadrants I and IV), `math.atan2(y, x)` takes both the $y$ and $x$ coordinates as separate arguments. This allows it to correctly determine the quadrant of the angle and return a value in the range $[-\pi, \pi]$ radians.
---
## Syntax
To use the `atan2()` function, you must first import Python's built-in `math` module.
```python
import math
math.atan2(y, x)
```
> **Note:** The `atan2()` function cannot be accessed directly. It must be called using the `math` module object.
---
## Parameters
* **`y`**: A numeric value representing the y-coordinate (vertical axis).
* **`x`**: A numeric value representing the x-coordinate (horizontal axis).
### Parameter Order Warning
Pay close attention to the order of the arguments: **`y` comes before `x`** (`math.atan2(y, x)`). This matches the mathematical definition $\theta = \operatorname{atan2}(y, x)$, which corresponds to the angle of the vector $(x, y)$ relative to the positive x-axis.
---
## Return Value
The function returns a float value representing the arc tangent of $y/x$ in **radians**.
* The returned value is in the range **$[-\pi, \pi]$** (approximately $-3.14159$ to $3.14159$).
* If you need to convert the result from radians to degrees, you can use `math.degrees()`.
---
## Code Examples
### Basic Usage
The following example demonstrates how to use `math.atan2()` with various positive and negative coordinate pairs:
```python
import math
# Print the arc tangent for different (y, x) coordinates
print("atan2(-0.50, -0.50) : ", math.atan2(-0.50, -0.50))
print("atan2(0.50, 0.50) : ", math.atan2(0.50, 0.50))
print("atan2(5, 5) : ", math.atan2(5, 5))
print("atan2(-10, 10) : ", math.atan2(-10, 10))
print("atan2(10, 20) : ", math.atan2(10, 20))
```
**Output:**
```text
atan2(-0.50, -0.50) : -2.356194490192345
atan2(0.50, 0.50) : 0.7853981633974483
atan2(5, 5) : 0.7853981633974483
atan2(-10, 10) : -0.7853981633974483
atan2(10, 20) : 0.4636476090008061
```
---
## Key Considerations
### 1. Quadrant Awareness
The primary advantage of `math.atan2(y, x)` over `math.atan(y/x)` is its ability to distinguish between opposite quadrants.
For example:
* Point $(5, 5)$ is in Quadrant I. Both $x$ and $y$ are positive.
* Point $(-5, -5)$ is in Quadrant III. Both $x$ and $y$ are negative.
If you use simple division $y/x$, both yield $1.0$ ($5/5 = 1$ and $-5/-5 = 1$). A standard `math.atan(1)` cannot distinguish between these two points and will always return $0.785$ ($\pi/4$ radians).
`math.atan2()` resolves this ambiguity:
```python
import math
# Quadrant I (positive x, positive y)
print("Quadrant I (5, 5): ", math.atan2(5, 5)) # Output: 0.7853981633974483 ( 45 degrees)
# Quadrant III (negative x, negative y)
print("Quadrant III (-5, -5):", math.atan2(-5, -5)) # Output: -2.356194490192345 (-135 degrees)
```
### 2. Handling Division by Zero
If you attempt to calculate `math.atan(y / x)` when $x = 0$, Python will throw a `ZeroDivisionError`.
`math.atan2(y, x)` safely handles $x = 0$ without throwing an error:
* If $y > 0$ and $x = 0$, it returns $\pi/2$ ($90^\circ$).
* If $y < 0$ and $x = 0$, it returns $-\pi/2$ ($-90^\circ$).
* If both $y = 0$ and $x = 0$, it returns $0.0$.
```python
import math
print(math.atan2(5, 0)) # Output: 1.5707963267948966 (pi / 2)
print(math.atan2(-5, 0)) # Output: -1.5707963267948966 (-pi / 2)
print(math.atan2(0, 0)) # Output: 0.0
```
YouTip