<commit_before>---
title: Operators
sort_rank: 2
---

# Operators

## Binary operators

Prometheus's query language supports basic logical and arithmetic operators.
For operations between two instant vectors, the [matching behavior](#vector-matching)
can be modified.

### Arithmetic binary operators

The following binary arithmetic operators exist in Prometheus:

* `+` (addition)
* `-` (subtraction)
* `*` (multiplication)
* `/` (division)
* `%` (modulo)
* `^` (power/exponentiation)

Binary arithmetic operators are defined between scalar/scalar, vector/scalar,
and vector/vector value pairs.

**Between two scalars**, the behavior is obvious: they evaluate to another
scalar that is the result of the operator applied to both scalar operands.

**Between an instant vector and a scalar**, the operator is applied to the
value of every data sample in the vector. E.g. if a time series instant vector
is multiplied by 2, the result is another vector in which every sample value of
the original vector is multiplied by 2. The metric name is dropped.

**Between two instant vectors**, a binary arithmetic operator is applied to
each entry in the left-hand side vector and its [matching element](#vector-matching)
in the right-hand vector. The result is propagated into the result vector with the
grouping labels becoming the output label set. The metric name is dropped. Entries
for which no matching entry in the right-hand vector can be found are not part of
the result.

### Trigonometric binary operators

The following trigonometric binary operators, which work in radians, exist in Prometheus:

* `atan2` (based on https://pkg.go.dev/math#Atan2, _This is experimental_)
    
    A usecase of `atan2` is converting directional data between the [polar](https://en.wikipedia.org/wiki/Polar_coordinate_system) (r, theta)
    and [cartesian](https://en.wikipedia.org/wiki/Cartesian_coordinate_system) (x, y) coordinate forms. 
    
    Example:

    Take some wind speed and direction data in the polar form. To get a trend over a
    longer period of time, it's not correct to just average out the direction data.
    The polar data needs to be converted into the cartesian form:

    ```
    wind:northwards = windspeed * cos(rad(winddirection))
    wind:westwards = windspeed * sin(rad(winddirection))
    ```

    Having done that, the longerterm averages over northwards and westwards now make sense:

    ```
    wind:northwards:avg1h = avg_over_time(wind:northwards[1h])
    wind:westwards:avg1h = avg_over_time(wind:westwards[1h])
    ```

    The "average" direction can be extracted out of these again by using `atan2`:

    ```
    wind:direction:avg1h = deg(wind:northwards:avg1h atan2 wind:westwards:avg1h)
    ```

    The Wikipedia article on the circular mean has more about this calculation:
    https://en.wikipedia.org/wiki/Circular_mean

Trigonometric operators allow trigonometric functions to be executed on two vectors using
vector matching, which isn't available with normal functions. They act in the same manner
as arithmetic operators.

### Comparison binary operators

The following binary comparison operators exist in Prometheus:
<commit_msg>Remove example and experimental<commit_after>---
title: Operators
sort_rank: 2
---

# Operators

## Binary operators

Prometheus's query language supports basic logical and arithmetic operators.
For operations between two instant vectors, the [matching behavior](#vector-matching)
can be modified.

### Arithmetic binary operators

The following binary arithmetic operators exist in Prometheus:

* `+` (addition)
* `-` (subtraction)
* `*` (multiplication)
* `/` (division)
* `%` (modulo)
* `^` (power/exponentiation)

Binary arithmetic operators are defined between scalar/scalar, vector/scalar,
and vector/vector value pairs.

**Between two scalars**, the behavior is obvious: they evaluate to another
scalar that is the result of the operator applied to both scalar operands.

**Between an instant vector and a scalar**, the operator is applied to the
value of every data sample in the vector. E.g. if a time series instant vector
is multiplied by 2, the result is another vector in which every sample value of
the original vector is multiplied by 2. The metric name is dropped.

**Between two instant vectors**, a binary arithmetic operator is applied to
each entry in the left-hand side vector and its [matching element](#vector-matching)
in the right-hand vector. The result is propagated into the result vector with the
grouping labels becoming the output label set. The metric name is dropped. Entries
for which no matching entry in the right-hand vector can be found are not part of
the result.

### Trigonometric binary operators

The following trigonometric binary operators, which work in radians, exist in Prometheus:

* `atan2` (based on https://pkg.go.dev/math#Atan2)

Trigonometric operators allow trigonometric functions to be executed on two vectors using
vector matching, which isn't available with normal functions. They act in the same manner
as arithmetic operators.

### Comparison binary operators

The following binary comparison operators exist in Prometheus:

{'fraction_non_alphanumeric': 0.07289647150058162, 'fraction_numerical': 0.0038774718883288098, 'mean_word_length': 4.5592672413793105, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7429946', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1306, 'n_words': 647}