Ref Math Atan
## Python math.atan() Method
The `math.atan()` method is a built-in function in Python's standard `math` module. It returns the arc tangent (inverse tangent) of a given number $x$. The returned value is in radians and falls within the range of $[-\pi/2, \pi/2]$.
---
### Introduction to Arc Tangent
In trigonometry, the arc tangent is the inverse function of the tangent function. If $y = \tan(x)$, then $x = \arctan(y)$.
The `math.atan(x)` method calculates this relationship. It takes any real number (positive, negative, or zero) as an input and returns the corresponding angle in radians.
* **Python Version Added:** 1.6.1
---
### Syntax
To use the `math.atan()` method, you must first import the `math` module:
```python
import math
math.atan(x)
```
#### Parameter Values
| Parameter | Type | Description |
| :--- | :--- | :--- |
| **x** | `float` or `int` | **Required.** A numeric value (positive, negative, or zero) representing the tangent ratio. |
*Note: If `x` is not a number (e.g., a string or list), the method raises a `TypeError`.*
#### Return Value
* **Type:** `float`
* **Description:** Returns a float value representing the arc tangent of the number in radians. The returned value is always between $-\pi/2$ and $\pi/2$ (approximately `-1.57079` to `1.57079` radians).
---
### Code Examples
The following example demonstrates how to use `math.atan()` with different types of numeric inputs:
```python
import math
# Calculate the arc tangent of a decimal
print("atan(0.39):", math.atan(0.39))
# Calculate the arc tangent of a large positive integer
print("atan(67): ", math.atan(67))
# Calculate the arc tangent of a negative integer
print("atan(-21): ", math.atan(-21))
# Calculate the arc tangent of 0
print("atan(0): ", math.atan(0))
```
#### Output:
```text
atan(0.39): 0.37185607384858127
atan(67): 1.5558720618048116
atan(-21): -1.5232132235179132
atan(0): 0.0
```
---
### Advanced Considerations
#### 1. Converting Radians to Degrees
By default, `math.atan()` returns the angle in **radians**. If you need the result in **degrees**, you can convert it using `math.degrees()`:
```python
import math
radians_val = math.atan(1)
degrees_val = math.degrees(radians_val)
print(f"Radians: {radians_val}") # Output: ~0.785398 (pi/4)
print(f"Degrees: {degrees_val}") # Output: 45.0
```
#### 2. `math.atan(x)` vs `math.atan2(y, x)`
* **`math.atan(x)`** takes a single ratio as an argument. It cannot distinguish between quadrants (e.g., whether a negative ratio comes from a negative $y$ or a negative $x$).
* **`math.atan2(y, x)`** takes two arguments representing the $y$ and $x$ coordinates. It returns the correct angle across all four quadrants (from $-\pi$ to $\pi$). For coordinate geometry and vector calculations, `math.atan2(y, x)` is generally preferred.
YouTip