# `math`

> Look up math.avg, math.min, math.max, and math.derivative, the running statistics of the Arc math module.

The `math` module gives running statistics and rates of change of a numeric value. Import it with `import math`. All `math` functions are flow functions: a function body cannot call them.

`avg`, `min`, and `max` share these parameters:

* [`duration`](https://docs.synnaxlabs.com/reference/control/arc/standard-library/math#duration)`i64 ns = 0`

  Start again when this much time has passed, measured on input timestamps.

* [`count`](https://docs.synnaxlabs.com/reference/control/arc/standard-library/math#count)`i64 = 0`

  Start again on the next input after this many values.

* [`reset`](https://docs.synnaxlabs.com/reference/control/arc/standard-library/math#reset)`bool = false`

  Has no effect in text mode, which cannot wire an input to it.

With `duration` and `count` at 0, the value covers every input since the stage started. A window starts again with the input that ends the old window. Synnax reads `duration` and `count` once, when the automation starts.

## `avg`

Gives the average of all input values since the window started.

* Input -> a numeric value.
* Output -> one value for each input batch, of the same type as the input. It has the timestamp of the last value in the batch. It is not a moving average.
* Runs -> on each new input. Its window starts again when its stage starts.

```arc
import math

tank_pt -> math.avg{duration=5s} -> tank_pt_avg
```

An integer input gives an integer average, rounded toward zero. On the Core, the sum uses the input type, so it can overflow and loses precision. Use a float input. A Driver sums in `f64`.

## `derivative`

Gives the rate of change of the input, in units per second.

* Input -> a numeric value.
* Output -> one `f64` value for each input value, with the same timestamp. The rate is the change from the previous value, divided by the time between their timestamps.
* Runs -> on each new input. The first value, and a value with a timestamp that is not later than the one before it, give 0. It starts again when its stage starts.

`derivative` has no parameters.

```arc
import math

tank_pt -> math.derivative{} -> tank_pt_rate
```

## `max`

Gives the largest input value since the window started.

* Input -> a numeric value.
* Output -> one value for each input batch, of the same type as the input.
* Runs -> on each new input. Its window starts again when its stage starts.

```arc
import math

tank_pt -> math.max{} -> tank_pt_max
```

## `min`

Gives the smallest input value since the window started.

* Input -> a numeric value.
* Output -> one value for each input batch, of the same type as the input.
* Runs -> on each new input. Its window starts again when its stage starts.

```arc
import math

tank_pt -> math.min{count=1000} -> tank_pt_min
```
