Calculate Absolute Value

Calculate the absolute value using the math:abs() function

Query

flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 1["Expression"] 2["Expression"] result{{Result Set}} repo --> 1 1 --> 2 2 --> result
logscale
x := -3.5
        | math:abs(x, as=result)

Introduction

The math:abs() function can be used to calculate the absolute value of a number. It converts negative numbers to positive by removing the negative sign, while leaving positive numbers unchanged.

In this example, the math:abs() function is used to calculate the absolute value of a negative number, demonstrating how it returns the positive version of that number.

Step-by-Step

  1. Starting with the source repository events.

  2. flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 1["Expression"] 2["Expression"] result{{Result Set}} repo --> 1 1 --> 2 2 --> result style 1 fill:#ff0000,stroke-width:4px,stroke:#000;
    logscale
    x := -3.5

    Assigns the negative value -3.5 to field x. This value will demonstrate how the absolute value function removes the negative sign.

  3. flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 1["Expression"] 2["Expression"] result{{Result Set}} repo --> 1 1 --> 2 2 --> result style 2 fill:#ff0000,stroke-width:4px,stroke:#000;
    logscale
    | math:abs(x, as=result)

    Calculates the absolute value of the number in field x and returns the result in a field named result. If the as parameter is not specified, the result is returned in a field named _abs as default.

  4. Event Result set.

Summary and Results

The query is used to find the absolute value of a number, which is useful when you need to know the size of a value without considering whether it is positive or negative.

This query is useful, for example, to calculate distances, determine the size of differences, or measure deviations from a reference point.

Sample output from the incoming example data:

result
3.5

The result shows that the absolute value of -3.5 is 3.5, demonstrating how the function removes the negative sign.

Note that the math:abs() function works with both integer and decimal values. For example, math:abs(-3.5) = 3.5, math:abs(3.5) = 3.5, and math:abs(0) = 0.

The math:abs() function is particularly useful in dashboards where you need to analyze values regardless of their sign, such as measuring deviations from a baseline or calculating differences between values.