> For the complete documentation index, see [llms.txt](https://cortex-docs.paloaltonetworks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/atan2.md).

# atan2

Use the `atan2()` function to calculate the angle in radians between the positive x-axis and the ray from the origin to the point (x, y), using the signs of both arguments to determine the quadrant of the result.

## Syntax

```sql
atan2(<y>, <x>)
```

## Parameters

| Name | Type           | Required | Description                                           |
| ---- | -------------- | -------- | ----------------------------------------------------- |
| `y`  | integer, float | Yes      | The y-coordinate (vertical component) of the point.   |
| `x`  | integer, float | Yes      | The x-coordinate (horizontal component) of the point. |

## Returns

**Type**: float

**Description**: The `atan2()` function returns the angle in radians between the positive x-axis and the point (x, y). The resulting value is within the range (-π, π].

## Usage notes

* **Input type**: XQL doesn't support NaN or infinite values as input and these value types also can not be returned.
* **Valid Input Range**: Both `x` and `y` accept any real number.
* **Null Handling**: If either input expression is `null`, the function returns `null`.
* **Quadrant Awareness**: Unlike `atan()`, the `atan2()` function uses the signs of both arguments to determine the correct quadrant of the angle, making it suitable for full 360-degree angle calculations.
* **Special Cases**: `atan2(0, 0)` returns `0.0`. `atan2(1, 0)` returns `π/2`. `atan2(-1, 0)` returns `-π/2`.
* **Common Use Cases**: This function is typically used within the `alter` stage for computing angles from Cartesian coordinates, navigation calculations, and directional analysis.

## Examples

### Example 1: Calculate angle from literal coordinates

**Goal**: Calculate the angle for specific coordinate pairs to verify known mathematical results.

**XQL code**:

```sql
dataset = xdr_data
| limit 1
| alter angle1 = atan2(1, 1), angle2 = atan2(1, 0), angle3 = atan2(-1, -1)
| fields angle1, angle2, angle3
```

**Explanation**: You use `atan2()` on three coordinate pairs. `atan2(1, 1)` returns approximately `0.7854` (π/4, or 45°), `atan2(1, 0)` returns approximately `1.5708` (π/2, or 90°), and `atan2(-1, -1)` returns approximately `-2.3562` (-3π/4, or -135°).

**Output**:

| ANGLE1 | ANGLE2 | ANGLE3  |
| ------ | ------ | ------- |
| 0.7854 | 1.5708 | -2.3562 |

### Example 2: Calculate angle from field values

**Goal**: Compute the angle between two numeric fields representing coordinates.

**XQL code**:

```sql
config timeframe = 1d
| dataset = sample_xql_raw
| alter angle_rad = atan2(numeric_value, duration_seconds)
| fields event_id, numeric_value, duration_seconds, angle_rad
| limit 3
```

**Explanation**: This query computes the angle in radians from the `numeric_value` (y-coordinate) and `duration_seconds` (x-coordinate) fields using `atan2()`. The function correctly handles all four quadrants based on the signs of the inputs.

**Output**:

| EVENT\_ID | NUMERIC\_VALUE | DURATION\_SECONDS | ANGLE\_RAD |
| --------- | -------------- | ----------------- | ---------- |
| 101       | 3.0            | 4.0               | 0.6435     |
| 102       | -2.0           | 1.5               | -0.9273    |
| 103       | 5.0            | -3.0              | 2.1112     |

### Example 3: Convert atan2 result to degrees

**Goal**: Calculate the angle using `atan2()` and convert the result from radians to degrees.

**XQL code**:

```sql
config timeframe = 1d
| dataset = sample_xql_raw
| alter angle_rad = atan2(numeric_value, duration_seconds)
| alter angle_deg = multiply(angle_rad, divide(180, pi()))
| fields event_id, numeric_value, duration_seconds, angle_rad, angle_deg
| limit 3
```

**Explanation**: This query calculates the angle in radians using `atan2()`, then converts it to degrees by multiplying by `180 / π`. This is useful for presenting angles in a more human-readable format.

**Output**:

| EVENT\_ID | NUMERIC\_VALUE | DURATION\_SECONDS | ANGLE\_RAD | ANGLE\_DEG |
| --------- | -------------- | ----------------- | ---------- | ---------- |
| 101       | 3.0            | 4.0               | 0.6435     | 36.87      |
| 102       | -2.0           | 1.5               | -0.9273    | -53.13     |
| 103       | 5.0            | -3.0              | 2.1112     | 120.96     |

## Related articles

* **Stages**: [`alter`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/stages/alter.md), [`fields`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/stages/fields.md), [`limit`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/stages/limit.md)
* **Functions**: [`atan`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/atan.md), [`sin`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/sin.md), [`cos`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/cos.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/atan2.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
