TinyMath functions
Use TinyMath functions to do math in the Canvas expression language, from rounding and averages to trigonometry and comparisons. The EvalMath lambda in alert action templates accepts the same functions.
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 and mod. |
| 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, - matches subtract, * matches multiply, / matches divide, < matches lt, <= matches lte, > matches gt, >= matches gte, and == matches eq.
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.
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.
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. 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'.
| Task | Functions |
|---|---|
| Arithmetic | add, subtract, multiply, divide, mod, pow, abs, exp, log, log10 (call it as log(a, 10)), sqrt, cbrt, square, cube |
| Rounding | ceil, floor, fix, round |
| Summarize numbers or arrays | sum, mean, median, mode, min, max, range, count, size, unique, first, last |
| Limits and random numbers | clamp, random |
| Trigonometry and constants | sin, cos, tan, degtorad, radtodeg, pi |
| Comparisons and conditions | eq, gt, gte, lt, lte, ifelse, defaults |
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
abs(-1)
abs(2)
abs([-1, -2, 3, -4])
- returns 1
- returns 2
- returns [1, 2, 3, 4]
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
add(1, 2, 3)
add([10, 20, 30, 40], 10, 20, 30)
add([1, 2], 3, [4, 5], 6)
add([1, 2, 3])
- returns 6
- returns [70, 80, 90, 100]
- returns [(1 + 3 + 4 + 6), (2 + 3 + 5 + 6)] = [14, 16]
- returns 6
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
cbrt(-27)
cbrt(94)
cbrt([27, 64, 125])
- returns -3
- returns 4.546835943776344
- returns [3, 4, 5]
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
ceil(1.2)
ceil(-1.8)
ceil([1.1, 2.2, 3.3])
- returns 2
- returns -1
- returns [2, 3, 4]
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 function for a lower limit or the min 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'ifa,min, ormaxare arrays of different lengths'Min must be less than max'ifmaxis less thanmin
Example
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])
- returns 2
- returns [15, 20, 25, 25]
- returns [15, 10, 10, 20]
- returns [20, 30, 35, 35]
- returns [clamp(1, 3, 4), clamp(9, 3, 5)] = [3, 5]
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
cos(0)
cos(1.5707963267948966)
cos([0, 1.5707963267948966])
- returns 1
- returns 6.123233995736766e-17
- returns [1, 6.123233995736766e-17]
Returns the length of an array. Alias for size.
| 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
count([])
count([-1, -2, -3, -4])
- returns 0
- returns 4
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
cube(-3)
cube([3, 4, 5])
- returns -27
- returns [27, 64, 125]
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
defaults(5, 1)
defaults(value, 1)
defaults(prices, 0)
- returns 5
- returns 1 if value is null
- returns [3, 0, 5] if prices is [3, null, 5]
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
degtorad(0)
degtorad(90)
degtorad([0, 90, 180, 360])
- returns 0
- returns 1.5707963267948966
- returns [0, 1.5707963267948966, 3.141592653589793, 6.283185307179586]
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'ifaandbare arrays with different lengths'Cannot divide by 0'ifbequals 0 or contains 0
Example
divide(6, 3)
divide([10, 20, 30, 40], 10)
divide(10, [1, 2, 5, 10])
divide([14, 42, 65, 108], [2, 7, 5, 12])
- returns 2
- returns [1, 2, 3, 4]
- returns [10, 5, 2, 1]
- returns [7, 6, 13, 9]
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'ifbis not provided'Array length mismatch'ifaandbare arrays of different lengths
Example
eq(1, 1)
eq(1, 2)
eq([1, 1], 1)
eq([1, 2], 1)
eq([1, 2], [1, 2])
- returns true
- returns false
- returns true
- returns false
- returns true
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
exp(2)
exp([1, 2, 3])
- returns e^2 = 7.38905609893065
- returns [e1, e2, e^3] = [2.718281828459045, 7.38905609893065, 20.085536923187668]
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
first(2)
first([1, 2, 3])
- returns 2
- returns 1
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
fix(1.2)
fix(-1.8)
fix([1.8, 2.9, -3.7, -4.6])
- returns 1
- returns -1
- returns [1, 2, -3, -4]
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
floor(1.8)
floor(-1.2)
floor([1.7, 2.8, 3.9])
- returns 1
- returns -2
- returns [1, 2, 3]
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'ifbis not provided'Array length mismatch'ifaandbare arrays of different lengths
Example
gt(1, 1)
gt(2, 1)
gt([2, 3], 1)
gt([1, 2], 1)
gt([1, 2], [0, 1])
- returns false
- returns true
- returns true
- returns false
- returns true
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:
-
If aorbis an array, returnstrueonly if every element satisfies the comparison. -
If ais an array, returnstrueonly if every element is equal to the other value, or every element is greater.
Throws:
'Missing b value'ifbis not provided'Array length mismatch'ifaandbare arrays of different lengths
Example
gte(1, 1)
gte(1, 2)
gte([1, 2], 2)
- returns true
- returns false
- returns false
gte([1, 2], [1, 1])
gte(2, [1, 2])
- returns true
- returns true
gte([1, 2], [1, 1])
gte(2, [1, 2])
- returns false
- returns false
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'ifcondis not a boolean'Missing a value'ifais not provided or is null'Missing b value'ifbis not provided or is null
Example
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)
- returns 0
- returns 7
- returns [1, 2, 3]
- returns 20
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
last(2)
last([1, 2, 3])
- returns 2
- returns 3
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'ifbis less than or equal to 0'Must be greater than 0'ifa< 0
Example
log(1)
log(64, 8)
log(42, 5)
log([2, 4, 8, 16, 32], 2)
- returns 0
- returns 2
- returns 2.322344707681546
- returns [1, 2, 3, 4, 5]
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
log(10, 10)
log(100, 10)
log(80, 10)
log([10, 100, 10000, 100000], 10)
- returns 1
- returns 2
- returns 1.9030899869919433
- returns [1, 2, 4, 5]
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'ifbis not provided'Array length mismatch'ifaandbare arrays of different lengths
Example
lt(1, 1)
lt(1, 2)
lt([1, 2], 3)
lt([1, 2], 2)
lt([1, 2], [2, 3])
- returns false
- returns true
- returns true
- returns false
- returns true
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:
-
If aorbis an array, returnstrueonly if every element satisfies the comparison. -
If ais an array, returnstrueonly if every element is equal to the other value, or every element is less.
Throws:
'Missing b value'ifbis not provided'Array length mismatch'ifaandbare arrays of different lengths
Example
lte(1, 1)
lte(1, 2)
lte([1, 2], [1, 1])
- returns true
- returns true
- returns false
lte([1, 2], 2)
lte(1, [1, 2])
- returns true
- returns true
lte([1, 2], 2)
lte(1, [1, 2])
- returns false
- returns false
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
max(1, 2, 3)
max([10, 20, 30, 40], 15)
max([1, 9], 4, [3, 5])
max([3, 1, 4])
- returns 3
- returns [15, 20, 30, 40]
- returns [max([1, 4, 3]), max([9, 4, 5])] = [4, 9]
- returns 4
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
mean(1, 2, 3)
mean([10, 20, 30, 40], 20)
mean([1, 9], 5, [3, 4])
mean([1, 2, 6])
- returns 2
- returns [15, 20, 25, 30]
- returns [mean([1, 5, 3]), mean([9, 5, 4])] = [3, 6]
- returns 3
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
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])
- returns 1.5
- returns 2
- returns [15, 20, 25, 25]
- returns [median([1, 2, 4, 3]), median([9, 2, 4, 5])] = [2.5, 4.5]
- returns 3
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
min(1, 2, 3)
min([10, 20, 30, 40], 25)
min([1, 9], 4, [3, 5])
min([3, 1, 4])
- returns 1
- returns [10, 20, 25, 25]
- returns [min([1, 4, 3]), min([9, 4, 5])] = [1, 4]
- returns 1
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'ifaandbare arrays with different lengths'Cannot divide by 0'ifbis 0, or ifaandbare both arrays andbcontains 0
Example
mod(10, 7)
mod([11, 22, 33, 44], 10)
mod(100, [3, 7, 11, 23])
mod([14, 42, 65, 108], [5, 4, 14, 2])
- returns 3
- returns [1, 2, 3, 4]
- returns [1, 2, 1, 8]
- returns [4, 2, 9, 0]
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
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])
- returns [1]
- returns [1,2]
- returns [[10], [20], [30], [10, 20, 30, 40]]
- returns [mode([1, 1, 4, 3]), mode([9, 1, 4, 5])] = [[1], [1, 4, 5, 9]]
- returns [1]
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
multiply(6, 3)
multiply([10, 20, 30, 40], 10)
multiply(10, [1, 2, 5, 10])
multiply([1, 2, 3, 4], [2, 7, 5, 12])
- returns 18
- returns [100, 200, 300, 400]
- returns [10, 20, 50, 100]
- returns [2, 14, 15, 48]
Returns the mathematical constant pi.
Returns: number. The mathematical constant pi, 3.141592653589793.
Example
pi()
- returns 3.141592653589793
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
pow(2, 3)
pow([1, 2, 3], 4)
- returns 8
- returns [1, 16, 81]
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
radtodeg(0)
radtodeg(1.5707963267948966)
radtodeg([0, 1.5707963267948966, 3.141592653589793, 6.283185307179586])
- returns 0
- returns 90
- returns [0, 90, 180, 360]
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
random()
random(10)
random(-10, 10)
- returns a random number between 0 (inclusive) and 1 (exclusive)
- returns a random number between 0 (inclusive) and 10 (exclusive)
- returns a random number between -10 (inclusive) and 10 (exclusive)
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
range(1, 2, 3)
range([10, 20, 30, 40], 15)
range([1, 9], 4, [3, 5])
range([1, 9, 4])
- returns 2
- returns [5, 5, 15, 25]
- returns [range([1, 4, 3]), range([9, 4, 5])] = [3, 5]
- returns 8
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
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)
- returns 1
- returns -11
- returns -10.1
- returns 10.9375
- returns 3
- returns -2
- returns 1
- returns [2.92, 5.12, 3.52, 4.49]
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
sin(0)
sin(1.5707963267948966)
sin([0, 1.5707963267948966])
- returns 0
- returns 1
- returns [0, 1]
Returns the length of an array. Alias for count.
| 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
size([])
size([-1, -2, -3, -4])
- returns 0
- returns 4
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
sqrt(9)
sqrt(30)
sqrt([9, 16, 25])
- returns 3
- returns 5.477225575051661
- returns [3, 4, 5]
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
square(-3)
square([3, 4, 5])
- returns 9
- returns [9, 16, 25]
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
subtract(6, 3)
subtract([10, 20, 30, 40], 10)
subtract(10, [1, 2, 5, 10])
subtract([14, 42, 65, 108], [2, 7, 5, 12])
- returns 3
- returns [0, 10, 20, 30]
- returns [9, 8, 5, 0]
- returns [12, 35, 60, 96]
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
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)
- returns 6
- returns 160
- returns sum(1, 2, 3, 4, 5, 6) = 21
- returns sum(10, 20, 30, 40, 10, 1, 2, 3, 22) = 138
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
tan(0)
tan(1)
tan([0, 1, -1])
- returns 0
- returns 1.5574077246549023
- returns [0, 1.5574077246549023, -1.5574077246549023]
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
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])
- returns 1
- returns 0
- returns 4
- returns 5