﻿---
title: TinyMath functions
description: Look up the TinyMath functions you can use in Canvas expressions, with their parameters, return values, errors, and examples.
url: https://docs-v3-preview.elastic.dev/elastic/docs-content/pull/9283/explore-analyze/visualize/canvas/canvas-tinymath-functions
products:
  - Elastic Documentation
  - Kibana
applies_to:
  - Elastic Cloud Serverless: Generally available
  - Elastic Stack: Generally available
---

# TinyMath functions
Use TinyMath functions to do math in the [Canvas expression language](https://docs-v3-preview.elastic.dev/elastic/docs-content/pull/9283/explore-analyze/visualize/canvas/canvas-function-reference), from rounding and averages to trigonometry and comparisons. The [`EvalMath` lambda](/elastic/docs-content/pull/9283/explore-analyze/alerting/alerts/rule-action-variables#_evalmath) in alert action templates accepts the same functions.

## Expression syntax

An expression combines numbers, variables, operators, and function calls, such as `round(price * 1.2, 2)`. A variable is a value from the data that the expression runs on, such as a `datatable` column in Canvas or a field in an alert action template.

| To do this                                 | Write this                                               | Notes                                                                                                                                                                                                                                                                 |
|--------------------------------------------|----------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Calculate                                  | `+`, `-`, `*`, `/`, and parentheses, as in `(2 + 3) * 4` | `*` and `/` run before `+` and `-`. Operators of the same rank run from left to right. `^` and `%` aren't supported, so use [`pow`](#_pow_a_b) and [`mod`](#_mod_a_b).                                                                                                |
| Write a number                             | `10`, `-10`, `1.5`, or `1e3`                             | Write `0.5`, not `.5`. Leading zeros and a leading `+` aren't accepted.                                                                                                                                                                                               |
| Subtract or negate a variable              | `a - b` or `0 - x`                                       | Put spaces around `-`. TinyMath reads `a-b` as one variable named `a-b`, and `-x` as one variable named `-x`.                                                                                                                                                         |
| Use a value from the data                  | `price` or `context.value`                               | Names are case-sensitive, and a name that isn't in the data is an error, not null. A dot reads a nested value, so `context.value` reads `value` inside `context`. A name can contain letters, digits, `_`, `.`, `@`, `-`, `[`, and `]`, but can't start with a digit. |
| Use a name with spaces or other characters | `"sales total"` or `'sales total'`                       | Quote a name that contains a space, starts with a digit, or contains a character such as `+`.                                                                                                                                                                         |
| Call a function                            | `round(x, 2)`                                            | Put `(` right after the function name, with no space. Separate arguments with commas. An argument can be an expression, as in `add(1 + 2, 3 * 4)`. Function names are case-sensitive and can't contain digits.                                                        |
| Compare values                             | `<`, `<=`, `>`, `>=`, `==`                               | `!=`, `=`, and `===` aren't supported. A comparison of two numbers gives `true` or `false`. Use it on its own, in parentheses, or as a function argument, as in `ifelse(x > 1, 10, 20)`. You can't chain comparisons or use the result in arithmetic.                 |

Each operator gives the same result as the function with the same meaning, including for arrays and when an error occurs. `+` matches [`add`](#_add_args), `-` matches [`subtract`](#_subtract_a_b), `*` matches [`multiply`](#_multiply_a_b), `/` matches [`divide`](#_divide_a_b), `<` matches [`lt`](#_lt_a_b), `<=` matches [`lte`](#_lte_a_b), `>` matches [`gt`](#_gt_a_b), `>=` matches [`gte`](#_gte_a_b), and `==` matches [`eq`](#_eq_a_b).
You can't write `null`, `true`, or `false`. TinyMath reads those words as variable names.
An expression can't contain an array directly. Arrays come from the data that the expression runs on, such as the columns of a `datatable`.
You can wrap any function in another function as long as the return type of the inner function matches the parameter type of the outer function.
<note applies-to="Elastic Cloud Serverless: Generally available, Elastic Stack: Generally available since 9.4">
  A TinyMath expression can contain up to 20 levels of nested parentheses, including the parentheses of function calls. The expression can be up to 8,192 characters, not counting quoted text, such as a quoted column name or a Kibana Query Language (KQL) or Lucene filter. TinyMath rejects expressions that exceed these limits before it parses them. Typical expressions stay well within these limits.
</note>


## How functions treat arrays and non-numbers

Most of the functions accept arrays and apply to each element of the array. For the functions that accept multiple arrays as parameters, the function generally does the calculation index by index. The examples with arrays show what each function returns for the values in a `datatable` column.
If you pass a single array to `add`, `max`, `mean`, `median`, `min`, `mode`, or `range`, the function summarizes the whole array instead of working by index. For example, `mean` of a column returns the average of that column. An empty array makes `add`, `max`, `mean`, `min`, and `range` throw an error.
If a value isn't a number, such as null, most functions return `NaN`. The exceptions, which don't check that values are numbers, are `count`, `defaults`, `first`, `ifelse`, `last`, `size`, and `unique`. To replace null values before you calculate, use [`defaults`](#_defaults_a_b). For example, if `prices` is `[1, null]`, `abs(prices)` returns `NaN`, while `abs(defaults(prices, 0))` returns `[1, 0]`. A comparison with null returns `NaN`, not `true` or `false`, and `ifelse` then throws `'Condition clause is of the wrong type'`.

## Functions by task


| Task                        | Functions                                                                                                                                                                                                                                                                                                                                     |
|-----------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Arithmetic                  | [`add`](#_add_args), [`subtract`](#_subtract_a_b), [`multiply`](#_multiply_a_b), [`divide`](#_divide_a_b), [`mod`](#_mod_a_b), [`pow`](#_pow_a_b), [`abs`](#_abs_a), [`exp`](#_exp_a), [`log`](#_log_a_b), [`log10`](#_log10_a) (call it as `log(a, 10)`), [`sqrt`](#_sqrt_a), [`cbrt`](#_cbrt_a), [`square`](#_square_a), [`cube`](#_cube_a) |
| Rounding                    | [`ceil`](#_ceil_a), [`floor`](#_floor_a), [`fix`](#_fix_a), [`round`](#_round_a_b)                                                                                                                                                                                                                                                            |
| Summarize numbers or arrays | [`sum`](#_sum_args), [`mean`](#_mean_args), [`median`](#_median_args), [`mode`](#_mode_args), [`min`](#_min_args), [`max`](#_max_args), [`range`](#_range_args), [`count`](#_count_a), [`size`](#_size_a), [`unique`](#_unique_a), [`first`](#_first_a), [`last`](#_last_a)                                                                   |
| Limits and random numbers   | [`clamp`](#_clamp_a_min_max), [`random`](#_random_a_b)                                                                                                                                                                                                                                                                                        |
| Trigonometry and constants  | [`sin`](#_sin_a), [`cos`](#_cos_a), [`tan`](#_tan_a), [`degtorad`](#_degtorad_a), [`radtodeg`](#_radtodeg_a), [`pi`](#_pi)                                                                                                                                                                                                                    |
| Comparisons and conditions  | [`eq`](#_eq_a_b), [`gt`](#_gt_a_b), [`gte`](#_gte_a_b), [`lt`](#_lt_a_b), [`lte`](#_lte_a_b), [`ifelse`](#_ifelse_cond_a_b), [`defaults`](#_defaults_a_b)                                                                                                                                                                                     |


## abs( a )

Calculates the absolute value of a number. For arrays, the function applies to each element.

| Param | Type                   | Description                     |
|-------|------------------------|---------------------------------|
| a     | number  Array.<number> | a number or an array of numbers |

**Returns**: `number` | `Array.<number>`. The absolute value of `a`. Returns an array with the absolute values of each element if `a` is an array.
**Example**
```js
abs(-1)
abs(2)
abs([-1, -2, 3, -4])
```


## add( …args )

Calculates the sum of one or more numbers or arrays. If you pass several arguments and at least one is an array, the function calculates the sum by index.

| Param | Type                   | Description                              |
|-------|------------------------|------------------------------------------|
| …args | number  Array.<number> | one or more numbers or arrays of numbers |

**Returns**: `number` | `Array.<number>`. The sum of all numbers in `args` if `args` contains only numbers. Returns an array of sums of the elements at each index, including all scalar numbers in `args` in the calculation at each index if you pass several arguments and at least one is an array. If you pass a single array, the result covers every element.
**Throws**: `'Array length mismatch'` if `args` contains arrays of different lengths
**Example**
```js
add(1, 2, 3)
add([10, 20, 30, 40], 10, 20, 30)
add([1, 2], 3, [4, 5], 6)
add([1, 2, 3])
```


## cbrt( a )

Calculates the cube root of a number. For arrays, the function applies to each element.

| Param | Type                   | Description                     |
|-------|------------------------|---------------------------------|
| a     | number  Array.<number> | a number or an array of numbers |

**Returns**: `number` | `Array.<number>`. The cube root of `a`. Returns an array with the cube roots of each element if `a` is an array.
**Example**
```js
cbrt(-27)
cbrt(94)
cbrt([27, 64, 125])
```


## ceil( a )

Calculates the ceiling of a number, that is, rounds a number toward positive infinity. For arrays, the function applies to each element.

| Param | Type                   | Description                     |
|-------|------------------------|---------------------------------|
| a     | number  Array.<number> | a number or an array of numbers |

**Returns**: `number` | `Array.<number>`. The ceiling of `a`. Returns an array with the ceilings of each element if `a` is an array.
**Example**
```js
ceil(1.2)
ceil(-1.8)
ceil([1.1, 2.2, 3.3])
```


## clamp( a, min, max )

Restricts a value to a given range. Returns `min` if the value is below it, `max` if the value is above it, and the value itself otherwise. To limit a value on one side only, use the [`max`](#_max_args) function for a lower limit or the [`min`](#_min_args) function for an upper limit. For example, `max(value, 5)` returns at least 5, and `min(value, 5)` returns at most 5.

| Param | Type                   | Description                             |
|-------|------------------------|-----------------------------------------|
| a     | number  Array.<number> | a number or an array of numbers         |
| min   | number  Array.<number> | the minimum value this function returns |
| max   | number  Array.<number> | the maximum value this function returns |

**Returns**: `number` | `Array.<number>`. The closest value between `min` (inclusive) and `max` (inclusive). Returns an array with values greater than or equal to `min` and less than or equal to `max` at each index if `a`, `min`, or `max` is an array. If you leave out `min` or `max`, the result is `NaN`, or an array of `NaN` values if `a` or `min` is an array. If `a` and `min` are both arrays and `max` is a number, each element of the result is `NaN`.
**Throws**:
- `'Array length mismatch'` if `a`, `min`, or `max` are arrays of different lengths
- `'Min must be less than max'` if `max` is less than `min`

**Example**
```js
clamp(1, 2, 3)
clamp([10, 20, 30, 40], 15, 25)
clamp(10, [15, 2, 4, 20], 25)
clamp(35, 10, [20, 30, 40, 50])
clamp([1, 9], 3, [4, 5])
```


## cos( a )

Calculates the cosine of a number. For arrays, the function applies to each element.

| Param | Type                   | Description                                |
|-------|------------------------|--------------------------------------------|
| a     | number  Array.<number> | a number or an array of numbers in radians |

**Returns**: `number` | `Array.<number>`. The cosine of `a`. Returns an array with the cosine of each element if `a` is an array.
**Example**
```js
cos(0)
cos(1.5707963267948966)
cos([0, 1.5707963267948966])
```


## count( a )

Returns the length of an array. Alias for [`size`](#_size_a).

| Param | Type        | Description         |
|-------|-------------|---------------------|
| a     | Array.<any> | array of any values |

**Returns**: `number`. The length of the array.
**Throws**: `'Must pass an array'` if `a` is not an array.
**Example**
```js
count([])
count([-1, -2, -3, -4])
```


## cube( a )

Calculates the cube of a number. For arrays, the function applies to each element.

| Param | Type                   | Description                     |
|-------|------------------------|---------------------------------|
| a     | number  Array.<number> | a number or an array of numbers |

**Returns**: `number` | `Array.<number>`. The cube of `a`. Returns an array with the cubes of each element if `a` is an array.
**Example**
```js
cube(-3)
cube([3, 4, 5])
```


## defaults( a, b )

Replaces null values with a fallback value. For arrays, the function applies to each element and also replaces undefined elements, such as the value of a row that doesn't have the column.

| Param | Type             | Description                                         |
|-------|------------------|-----------------------------------------------------|
| a     | any  Array.<any> | a value or an array of values that can include null |
| b     | any              | the value to use in place of null                   |

**Returns**: `any` | `Array.<any>`. `a` if it isn't null, otherwise `b`. Returns an array with `b` in place of each null or undefined element if `a` is an array.
**Example**
```js
defaults(5, 1)
defaults(value, 1)
defaults(prices, 0)
```


## degtorad( a )

Converts degrees to radians. For arrays, the function applies to each element.

| Param | Type                   | Description                                |
|-------|------------------------|--------------------------------------------|
| a     | number  Array.<number> | a number or an array of numbers in degrees |

**Returns**: `number` | `Array.<number>`. `a` converted to radians. Returns an array with each element converted to radians if `a` is an array.
**Example**
```js
degtorad(0)
degtorad(90)
degtorad([0, 90, 180, 360])
```


## divide( a, b )

Divides two numbers. If either argument is an array, the function applies to each element.

| Param | Type                   | Description                                              |
|-------|------------------------|----------------------------------------------------------|
| a     | number  Array.<number> | dividend, a number or an array of numbers                |
| b     | number  Array.<number> | divisor, a number or an array of numbers, `b` can't be 0 |

**Returns**: `number` | `Array.<number>`. Returns the quotient of `a` and `b` if both are numbers. Returns an array with the quotients applied index-wise to each element if `a` or `b` is an array.
**Throws**:
- `'Array length mismatch'` if `a` and `b` are arrays with different lengths
- `'Cannot divide by 0'` if `b` equals 0 or contains 0

**Example**
```js
divide(6, 3)
divide([10, 20, 30, 40], 10)
divide(10, [1, 2, 5, 10])
divide([14, 42, 65, 108], [2, 7, 5, 12])
```


## eq( a, b )

Checks whether two values are equal. For arrays, the function returns `true` only if every element is equal. You can also write this comparison as `a == b`.

| Param | Type                   | Description                                                                            |
|-------|------------------------|----------------------------------------------------------------------------------------|
| a     | number  Array.<number> | a number or an array of numbers                                                        |
| b     | number  Array.<number> | a number, or, if `a` is also an array, an array of numbers with the same length as `a` |

**Returns**: `boolean`. `true` if `a` and `b` are equal, `false` otherwise. If `a` is an array, returns `true` only if every element of `a` equals `b`, or equals the element at the same index of `b` if `b` is an array.
**Throws**:
- `'Missing b value'` if `b` is not provided
- `'Array length mismatch'` if `a` and `b` are arrays of different lengths

**Example**
```js
eq(1, 1)
eq(1, 2)
eq([1, 1], 1)
eq([1, 2], 1)
eq([1, 2], [1, 2])
```


## exp( a )

Calculates *e^x* where *e* is Euler's number. For arrays, the function applies to each element.

| Param | Type                   | Description                     |
|-------|------------------------|---------------------------------|
| a     | number  Array.<number> | a number or an array of numbers |

**Returns**: `number` | `Array.<number>`. `e^a`. Returns an array with the values of `e^x` evaluated where `x` is each element of `a` if `a` is an array.
**Example**
```js
exp(2)
exp([1, 2, 3])
```


## first( a )

Returns the first element of an array. If `a` isn't an array, the function returns `a`.

| Param | Type             | Description                   |
|-------|------------------|-------------------------------|
| a     | any  Array.<any> | a value or an array of values |

**Returns**: `*`. The first element of `a`. Returns `a` if `a` is not an array.
**Example**
```js
first(2)
first([1, 2, 3])
```


## fix( a )

Calculates the fix of a number, that is, rounds a number toward 0. For arrays, the function applies to each element.

| Param | Type                   | Description                     |
|-------|------------------------|---------------------------------|
| a     | number  Array.<number> | a number or an array of numbers |

**Returns**: `number` | `Array.<number>`. The fix of `a`. Returns an array with the fixes for each element if `a` is an array.
**Example**
```js
fix(1.2)
fix(-1.8)
fix([1.8, 2.9, -3.7, -4.6])
```


## floor( a )

Calculates the floor of a number, that is, rounds a number toward negative infinity. For arrays, the function applies to each element.

| Param | Type                   | Description                     |
|-------|------------------------|---------------------------------|
| a     | number  Array.<number> | a number or an array of numbers |

**Returns**: `number` | `Array.<number>`. The floor of `a`. Returns an array with the floor of each element if `a` is an array.
**Example**
```js
floor(1.8)
floor(-1.2)
floor([1.7, 2.8, 3.9])
```


## gt( a, b )

Checks whether one value is greater than another. For arrays, the function returns `true` only if every element is greater. You can also write this comparison as `a > b`.

| Param | Type                   | Description                                                                            |
|-------|------------------------|----------------------------------------------------------------------------------------|
| a     | number  Array.<number> | a number or an array of numbers                                                        |
| b     | number  Array.<number> | a number, or, if `a` is also an array, an array of numbers with the same length as `a` |

**Returns**: `boolean`. `true` if `a` is greater than `b`, `false` otherwise. If `a` is an array, returns `true` only if every element of `a` is greater than `b`, or greater than the element at the same index of `b` if `b` is an array.
**Throws**:
- `'Missing b value'` if `b` is not provided
- `'Array length mismatch'` if `a` and `b` are arrays of different lengths

**Example**
```js
gt(1, 1)
gt(2, 1)
gt([2, 3], 1)
gt([1, 2], 1)
gt([1, 2], [0, 1])
```


## gte( a, b )

Checks whether one value is greater than or equal to another. You can also write this comparison as `a >= b`.

| Param | Type                   | Description                     |
|-------|------------------------|---------------------------------|
| a     | number  Array.<number> | a number or an array of numbers |
| b     | number  Array.<number> | a number or an array of numbers |

**Returns**: `boolean`. `true` if `a` is greater than or equal to `b`, `false` otherwise.
The result for arrays depends on the version:
- <applies-to>Elastic Cloud Serverless: Generally available</applies-to> <applies-to>Elastic Stack: Generally available since 9.5</applies-to> If `a` or `b` is an array, returns `true` only if every element satisfies the comparison.
- <applies-to>Elastic Stack: Generally available from 9.0 to 9.4</applies-to> If `a` is an array, returns `true` only if every element is equal to the other value, or every element is greater.

**Throws**:
- `'Missing b value'` if `b` is not provided
- `'Array length mismatch'` if `a` and `b` are arrays of different lengths

**Example**
```js
gte(1, 1)
gte(1, 2)
gte([1, 2], 2)
```

<applies-to>Elastic Cloud Serverless: Generally available</applies-to> <applies-to>Elastic Stack: Generally available since 9.5</applies-to> More array comparisons:
```js
gte([1, 2], [1, 1])
gte(2, [1, 2])
```

<applies-to>Elastic Stack: Generally available from 9.0 to 9.4</applies-to> More array comparisons:
```js
gte([1, 2], [1, 1])
gte(2, [1, 2])
```


## ifelse( cond, a, b )

Returns one of two values, depending on a condition.

| Param | Type             | Description                                                         |
|-------|------------------|---------------------------------------------------------------------|
| cond  | boolean          | a condition, such as `5 > 6` or the result of a comparison function |
| a     | any  Array.<any> | the value to return if `cond` is true                               |
| b     | any  Array.<any> | the value to return if `cond` is false                              |

**Returns**: `any` | `Array.<any>`. `a` if `cond` is true, otherwise `b`.
TinyMath evaluates both `a` and `b` first, so an error in either one stops the expression. For example, `ifelse(5 < 6, 1, 1 / 0)` throws `'Cannot divide by 0'`.
Comparing arrays gives one boolean. `ifelse` therefore picks one value for the whole array, not one per element.
**Throws**:
- `'Condition clause is of the wrong type'` if `cond` is not a boolean
- `'Missing a value'` if `a` is not provided or is null
- `'Missing b value'` if `b` is not provided or is null

**Example**
```js
ifelse(5 > 6, 1, 0)
ifelse(1 == 1, 7, 5)
ifelse(1 < 2, [1, 2, 3], [2, 3, 4])
ifelse(gt([0, 3], 1), 10, 20)
```


## last( a )

Returns the last element of an array. If `a` isn't an array, the function returns `a`.

| Param | Type             | Description                   |
|-------|------------------|-------------------------------|
| a     | any  Array.<any> | a value or an array of values |

**Returns**: `*`. The last element of `a`. Returns `a` if `a` is not an array.
**Example**
```js
last(2)
last([1, 2, 3])
```


## log( a, b )

Calculates the logarithm of a number. For arrays, the function applies to each element.

| Param | Type                   | Description                                                                                                                  |
|-------|------------------------|------------------------------------------------------------------------------------------------------------------------------|
| a     | number  Array.<number> | a number or an array of numbers that can't be negative                                                                       |
| b     | number                 | (optional) the base of the logarithm. If you don't provide a base, the function uses e and calculates the natural logarithm. |

**Returns**: `number` | `Array.<number>`. The logarithm of `a`. Returns an array with the logarithms of each element if `a` is an array.
**Throws**:
- `'Base out of range'` if `b` is less than or equal to 0
- `'Must be greater than 0'` if `a` < 0

**Example**
```js
log(1)
log(64, 8)
log(42, 5)
log([2, 4, 8, 16, 32], 2)
```


## log10( a )

Calculates the base 10 logarithm of a number. For arrays, the function applies to each element.
Expressions don't accept the name `log10`, because function names can't contain digits. To calculate a base 10 logarithm, use `log` with a base of 10, as in the examples.

| Param | Type                   | Description                                            |
|-------|------------------------|--------------------------------------------------------|
| a     | number  Array.<number> | a number or an array of numbers that can't be negative |

**Returns**: `number` | `Array.<number>`. The base 10 logarithm of `a`. Returns an array with the base 10 logarithms of each element if `a` is an array. The result can differ from the exact value in the last digits, as in `log(1000, 10)`, which returns `2.9999999999999996`.
**Throws**: `'Must be greater than 0'` if `a` < 0
**Example**
```js
log(10, 10)
log(100, 10)
log(80, 10)
log([10, 100, 10000, 100000], 10)
```


## lt( a, b )

Checks whether one value is less than another. For arrays, the function returns `true` only if every element is less. You can also write this comparison as `a < b`.

| Param | Type                   | Description                                                                            |
|-------|------------------------|----------------------------------------------------------------------------------------|
| a     | number  Array.<number> | a number or an array of numbers                                                        |
| b     | number  Array.<number> | a number, or, if `a` is also an array, an array of numbers with the same length as `a` |

**Returns**: `boolean`. `true` if `a` is less than `b`, `false` otherwise. If `a` is an array, returns `true` only if every element of `a` is less than `b`, or less than the element at the same index of `b` if `b` is an array.
**Throws**:
- `'Missing b value'` if `b` is not provided
- `'Array length mismatch'` if `a` and `b` are arrays of different lengths

**Example**
```js
lt(1, 1)
lt(1, 2)
lt([1, 2], 3)
lt([1, 2], 2)
lt([1, 2], [2, 3])
```


## lte( a, b )

Checks whether one value is less than or equal to another. You can also write this comparison as `a <= b`.

| Param | Type                   | Description                     |
|-------|------------------------|---------------------------------|
| a     | number  Array.<number> | a number or an array of numbers |
| b     | number  Array.<number> | a number or an array of numbers |

**Returns**: `boolean`. `true` if `a` is less than or equal to `b`, `false` otherwise.
The result for arrays depends on the version:
- <applies-to>Elastic Cloud Serverless: Generally available</applies-to> <applies-to>Elastic Stack: Generally available since 9.5</applies-to> If `a` or `b` is an array, returns `true` only if every element satisfies the comparison.
- <applies-to>Elastic Stack: Generally available from 9.0 to 9.4</applies-to> If `a` is an array, returns `true` only if every element is equal to the other value, or every element is less.

**Throws**:
- `'Missing b value'` if `b` is not provided
- `'Array length mismatch'` if `a` and `b` are arrays of different lengths

**Example**
```js
lte(1, 1)
lte(1, 2)
lte([1, 2], [1, 1])
```

<applies-to>Elastic Cloud Serverless: Generally available</applies-to> <applies-to>Elastic Stack: Generally available since 9.5</applies-to> More array comparisons:
```js
lte([1, 2], 2)
lte(1, [1, 2])
```

<applies-to>Elastic Stack: Generally available from 9.0 to 9.4</applies-to> More array comparisons:
```js
lte([1, 2], 2)
lte(1, [1, 2])
```


## max( …args )

Finds the maximum value of one or more numbers or arrays of numbers. If you pass several arguments and at least one is an array, the function finds the maximum by index.

| Param | Type                   | Description                              |
|-------|------------------------|------------------------------------------|
| …args | number  Array.<number> | one or more numbers or arrays of numbers |

**Returns**: `number` | `Array.<number>`. The maximum value of all numbers if `args` contains only numbers. Returns an array with the maximum values at each index, including all scalar numbers in `args` in the calculation at each index if you pass several arguments and at least one is an array. If you pass a single array, the result covers every element.
**Throws**: `'Array length mismatch'` if `args` contains arrays of different lengths
**Example**
```js
max(1, 2, 3)
max([10, 20, 30, 40], 15)
max([1, 9], 4, [3, 5])
max([3, 1, 4])
```


## mean( …args )

Finds the mean value of one or more numbers or arrays of numbers. If you pass several arguments and at least one is an array, the function finds the mean by index.

| Param | Type                   | Description                              |
|-------|------------------------|------------------------------------------|
| …args | number  Array.<number> | one or more numbers or arrays of numbers |

**Returns**: `number` | `Array.<number>`. The mean value of all numbers if `args` contains only numbers. Returns an array with the mean values of each index, including all scalar numbers in `args` in the calculation at each index if you pass several arguments and at least one is an array. If you pass a single array, the result covers every element.
**Throws**: `'Array length mismatch'` if `args` contains arrays of different lengths
**Example**
```js
mean(1, 2, 3)
mean([10, 20, 30, 40], 20)
mean([1, 9], 5, [3, 4])
mean([1, 2, 6])
```


## median( …args )

Finds the median value(s) of one or more numbers or arrays of numbers. If you pass several arguments and at least one is an array, the function finds the median by index.

| Param | Type                   | Description                              |
|-------|------------------------|------------------------------------------|
| …args | number  Array.<number> | one or more numbers or arrays of numbers |

**Returns**: `number` | `Array.<number>`. The median value of all numbers if `args` contains only numbers. Returns an array with the median values of each index, including all scalar numbers in `args` in the calculation at each index if you pass several arguments and at least one is an array. If you pass a single array, the result covers every element.
If you pass a single array, `median` sorts that array in place, in descending order. A later use of the same array in the expression sees the sorted values. For example, with `x` as `[3, 1, 4, 1, 5]`, `median(x) + first(x)` returns 8, because `first(x)` reads the sorted array.
**Throws**: `'Array length mismatch'` if `args` contains arrays of different lengths, unless the first array is empty, in which case the result is an empty array
**Example**
```js
median(1, 1, 2, 3)
median(1, 1, 2, 2, 3)
median([10, 20, 30, 40], 10, 20, 30)
median([1, 9], 2, 4, [3, 5])
median([3, 1, 4, 1, 5])
```


## min( …args )

Finds the minimum value of one or more numbers or arrays of numbers. If you pass several arguments and at least one is an array, the function finds the minimum by index.

| Param | Type                   | Description                              |
|-------|------------------------|------------------------------------------|
| …args | number  Array.<number> | one or more numbers or arrays of numbers |

**Returns**: `number` | `Array.<number>`. The minimum value of all numbers if `args` contains only numbers. Returns an array with the minimum values of each index, including all scalar numbers in `args` in the calculation at each index if you pass several arguments and at least one is an array. If you pass a single array, the result covers every element.
**Throws**: `'Array length mismatch'` if `args` contains arrays of different lengths
**Example**
```js
min(1, 2, 3)
min([10, 20, 30, 40], 25)
min([1, 9], 4, [3, 5])
min([3, 1, 4])
```


## mod( a, b )

Calculates the remainder after dividing two numbers. If either argument is an array, the function applies to each element.

| Param | Type                   | Description                                              |
|-------|------------------------|----------------------------------------------------------|
| a     | number  Array.<number> | dividend, a number or an array of numbers                |
| b     | number  Array.<number> | divisor, a number or an array of numbers, `b` can't be 0 |

**Returns**: `number` | `Array.<number>`. The remainder of `a` divided by `b` if both are numbers. Returns an array with the remainders applied index-wise to each element if `a` or `b` is an array. If `a` is a number and `b` is an array that contains 0, the result for that element is `NaN`.
**Throws**:
- `'Array length mismatch'` if `a` and `b` are arrays with different lengths
- `'Cannot divide by 0'` if `b` is 0, or if `a` and `b` are both arrays and `b` contains 0

**Example**
```js
mod(10, 7)
mod([11, 22, 33, 44], 10)
mod(100, [3, 7, 11, 23])
mod([14, 42, 65, 108], [5, 4, 14, 2])
```


## mode( …args )

Finds the mode value(s) of one or more numbers or arrays of numbers. If you pass several arguments and at least one is an array, the function finds the mode by index.

| Param | Type                   | Description                              |
|-------|------------------------|------------------------------------------|
| …args | number  Array.<number> | one or more numbers or arrays of numbers |

**Returns**: `number` | `Array.<number>` | `Array.<Array.<number>>`. If you pass a single number, returns that number. If you pass only numbers, or a single array, returns an array of the mode value(s) of every value. If you pass several arguments and at least one is an array, returns an array of arrays with the mode value(s) at each index, including all scalar numbers in `args` in the calculation at each index.
**Throws**: `'Array length mismatch'` if `args` contains arrays of different lengths, unless the first array is empty, in which case the result is an empty array
**Example**
```js
mode(1, 1, 2, 3)
mode(1, 1, 2, 2, 3)
mode([10, 20, 30, 40], 10, 20, 30)
mode([1, 9], 1, 4, [3, 5])
mode([1, 1, 2])
```


## multiply( a, b )

Multiplies two numbers. If either argument is an array, the function applies to each element.

| Param | Type                   | Description                     |
|-------|------------------------|---------------------------------|
| a     | number  Array.<number> | a number or an array of numbers |
| b     | number  Array.<number> | a number or an array of numbers |

**Returns**: `number` | `Array.<number>`. The product of `a` and `b` if both are numbers. Returns an array with the products applied index-wise to each element if `a` or `b` is an array.
**Throws**: `'Array length mismatch'` if `a` and `b` are arrays with different lengths
**Example**
```js
multiply(6, 3)
multiply([10, 20, 30, 40], 10)
multiply(10, [1, 2, 5, 10])
multiply([1, 2, 3, 4], [2, 7, 5, 12])
```


## pi()

Returns the mathematical constant pi.
**Returns**: `number`. The mathematical constant pi, `3.141592653589793`.
**Example**
```js
pi()
```


## pow( a, b )

Raises a number to a given exponent. For arrays, the function applies to each element.

| Param | Type                   | Description                     |
|-------|------------------------|---------------------------------|
| a     | number  Array.<number> | a number or an array of numbers |
| b     | number                 | the exponent                    |

**Returns**: `number` | `Array.<number>`. `a` raised to the power of `b`. Returns an array with each element raised to the power of `b` if `a` is an array.
**Throws**: `'Missing exponent'` if `b` is not provided
**Example**
```js
pow(2, 3)
pow([1, 2, 3], 4)
```


## radtodeg( a )

Converts radians to degrees. For arrays, the function applies to each element.

| Param | Type                   | Description                                |
|-------|------------------------|--------------------------------------------|
| a     | number  Array.<number> | a number or an array of numbers in radians |

**Returns**: `number` | `Array.<number>`. `a` converted to degrees. Returns an array with each element converted to degrees if `a` is an array.
**Example**
```js
radtodeg(0)
radtodeg(1.5707963267948966)
radtodeg([0, 1.5707963267948966, 3.141592653589793, 6.283185307179586])
```


## random( a, b )

Generates a random number within the given range where the lower bound is inclusive and the upper bound is exclusive. If you pass no numbers, it returns a number between 0 and 1. If you pass one number, it returns a number between 0 and that number. If both bounds are the same finite number, as in `random(0)` or `random(5, 5)`, it returns that number.

| Param | Type   | Description                                            |
|-------|--------|--------------------------------------------------------|
| a     | number | (optional) must be 0 or greater if `b` is not provided |
| b     | number | (optional) must be greater than or equal to `a`        |

**Returns**: `number`. A random number between 0 and 1 if you pass no numbers. Returns a random number between 0 and `a` if you pass one number. Returns a random number between `a` and `b` if you pass two numbers.
**Throws**: `'Min is greater than max'` if `a` < 0 when you pass only `a`, or if `a` > `b` when you pass both `a` and `b`
**Example**
```js
random()
random(10)
random(-10, 10)
```


## range( …args )

Finds the range of one or more numbers or arrays of numbers. If you pass several arguments and at least one is an array, the function finds the range by index.

| Param | Type                   | Description                              |
|-------|------------------------|------------------------------------------|
| …args | number  Array.<number> | one or more numbers or arrays of numbers |

**Returns**: `number` | `Array.<number>`. The range value of all numbers if `args` contains only numbers. Returns an array with the range values at each index, including all scalar numbers in `args` in the calculation at each index if you pass several arguments and at least one is an array. If you pass a single array, the result covers every element.
**Throws**: `'Array length mismatch'` if `args` contains arrays of different lengths
**Example**
```js
range(1, 2, 3)
range([10, 20, 30, 40], 15)
range([1, 9], 4, [3, 5])
range([1, 9, 4])
```


## round( a, b )

Rounds a number to the nearest integer by default, or to a number of decimal places if you pass `b`. A value exactly halfway rounds toward positive infinity. Decimals such as 1.005 aren't stored exactly in binary, so `round(1.005, 2)` returns 1, not 1.01. For arrays, the function applies to each element.

| Param | Type                   | Description                                           |
|-------|------------------------|-------------------------------------------------------|
| a     | number  Array.<number> | a number or an array of numbers                       |
| b     | number                 | (optional) number of decimal places, default value: 0 |

**Returns**: `number` | `Array.<number>`. The rounded value of `a`. Returns an array with the rounded values of each element if `a` is an array.
**Example**
```js
round(1.2)
round(-10.51)
round(-10.1, 2)
round(10.93745987, 4)
round(2.5)
round(-2.5)
round(1.005, 2)
round([2.9234, 5.1234, 3.5234, 4.49234324], 2)
```


## sin( a )

Calculates the sine of a number. For arrays, the function applies to each element.

| Param | Type                   | Description                                |
|-------|------------------------|--------------------------------------------|
| a     | number  Array.<number> | a number or an array of numbers in radians |

**Returns**: `number` | `Array.<number>`. The sine of `a`. Returns an array with the sine of each element if `a` is an array.
**Example**
```js
sin(0)
sin(1.5707963267948966)
sin([0, 1.5707963267948966])
```


## size( a )

Returns the length of an array. Alias for [`count`](#_count_a).

| Param | Type        | Description         |
|-------|-------------|---------------------|
| a     | Array.<any> | array of any values |

**Returns**: `number`. The length of the array.
**Throws**: `'Must pass an array'` if `a` is not an array
**Example**
```js
size([])
size([-1, -2, -3, -4])
```


## sqrt( a )

Calculates the square root of a number. For arrays, the function applies to each element.

| Param | Type                   | Description                     |
|-------|------------------------|---------------------------------|
| a     | number  Array.<number> | a number or an array of numbers |

**Returns**: `number` | `Array.<number>`. The square root of `a`. Returns an array with the square roots of each element if `a` is an array.
**Throws**: `'Unable find the square root of a negative number'` if `a` < 0
**Example**
```js
sqrt(9)
sqrt(30)
sqrt([9, 16, 25])
```


## square( a )

Calculates the square of a number. For arrays, the function applies to each element.

| Param | Type                   | Description                     |
|-------|------------------------|---------------------------------|
| a     | number  Array.<number> | a number or an array of numbers |

**Returns**: `number` | `Array.<number>`. The square of `a`. Returns an array with the squares of each element if `a` is an array.
**Example**
```js
square(-3)
square([3, 4, 5])
```


## subtract( a, b )

Subtracts two numbers. If either argument is an array, the function applies to each element.

| Param | Type                   | Description                     |
|-------|------------------------|---------------------------------|
| a     | number  Array.<number> | a number or an array of numbers |
| b     | number  Array.<number> | a number or an array of numbers |

**Returns**: `number` | `Array.<number>`. The difference of `a` and `b` if both are numbers, or an array of differences applied index-wise to each element.
**Throws**: `'Array length mismatch'` if `a` and `b` are arrays with different lengths
**Example**
```js
subtract(6, 3)
subtract([10, 20, 30, 40], 10)
subtract(10, [1, 2, 5, 10])
subtract([14, 42, 65, 108], [2, 7, 5, 12])
```


## sum( …args )

Calculates the sum of one or more numbers or arrays. If an argument is an array, the function adds its elements. The arrays can have different lengths.

| Param | Type                   | Description                              |
|-------|------------------------|------------------------------------------|
| …args | number  Array.<number> | one or more numbers or arrays of numbers |

**Returns**: `number`. The sum of all numbers and all elements of any arrays in `args`.
**Example**
```js
sum(1, 2, 3)
sum([10, 20, 30, 40], 10, 20, 30)
sum([1, 2], 3, [4, 5], 6)
sum([10, 20, 30, 40], 10, [1, 2, 3], 22)
```


## tan( a )

Calculates the tangent of a number. For arrays, the function applies to each element.

| Param | Type                   | Description                                |
|-------|------------------------|--------------------------------------------|
| a     | number  Array.<number> | a number or an array of numbers in radians |

**Returns**: `number` | `Array.<number>`. The tangent of `a`. Returns an array with the tangent of each element if `a` is an array.
**Example**
```js
tan(0)
tan(1)
tan([0, 1, -1])
```


## unique( a )

Counts the number of unique values in an array.

| Param | Type        | Description         |
|-------|-------------|---------------------|
| a     | Array.<any> | array of any values |

**Returns**: `number`. The number of unique values in the array. Returns 1 if `a` is not an array.
**Example**
```js
unique(100)
unique([])
unique([1, 2, 3, 4])
unique([1, 2, 3, 4, 2, 2, 2, 3, 4, 2, 4, 5, 2, 1, 4, 2])
```