Module extensions.math
Extensions to the Lua standard math library.
Functions
clamp (n, minValue, maxValue) | Returns num clamped to [minValue, maxValue]. |
round (n[, precision]) | Returns num rounded normally to precision decimals of precision. |
sign (n) | Returns -1 for a negative number, 1 for a positive number and 0 for 0 or nan. |
cbrt (n) | Returns the cube root of n . |
root (n, base) | Returns the real baseth root of n . |
proot (n, base) | Returns the principal baseth root of n . |
isnan (n) | Returns true if n is nan. |
approxeqabs (a, b[, tolerance]) | Performs an approximate comparison of two numbers. |
approxeqrel (a, b[, tolerance]) | Performs an approximate comparison of two numbers. |
Fields
nan | A value that is never equal to itself. |
epsilon | The machine epsilon, an upper bound on the relative error due to rounding. |
max_normal | The largest representable number |
min_normal | The smallest normal representable number |
min_subnormal | The smallest subnormal representable number |
e | Euler's number. |
Functions
- clamp (n, minValue, maxValue) line 46
-
Returns
num
clamped to [minValue, maxValue].Parameters:
- n number
- minValue number
- maxValue number
Returns:
-
number
- round (n[, precision]) line 54
-
Returns
num
rounded normally toprecision
decimals of precision.Parameters:
- n number
- precision number (optional)
Returns:
-
number
- sign (n) line 62
-
Returns -1 for a negative number, 1 for a positive number and 0 for 0 or nan.
Parameters:
- n number
Returns:
-
number
- cbrt (n) line 75
-
Returns the cube root of
n
.Parameters:
- n number
Returns:
-
number
- root (n, base) line 86
-
Returns the real baseth root of
n
. This extends the root function ton < 0
for odd bases.Parameters:
- n number
- base number
Returns:
-
number
Usage:
math.root(-8, 3) -- -2
math.root(-4, 2) -- nan
- proot (n, base) line 108
-
Returns the principal baseth root of
n
.Parameters:
- n number
- base number
Returns:
-
number
Usage:
math.root(-8, 3) -- nan
math.root(8, 3) -- 2
- isnan (n) line 119
-
Returns true if
n
is nan.Parameters:
- n number
Returns:
-
boolean
- approxeqabs (a, b[, tolerance]) line 134
-
Performs an approximate comparison of two numbers.
Tolerance may be left nil to use the default value of math.epsilon, a very small value. The tolerance should never be larger than small multiple of math.epsilon
This function is recommended for comparing small values near zero; using approxeqrel is suggested otherwise.
Parameters:
- a number
- b number
- tolerance number (optional)
- approxeqrel (a, b[, tolerance]) line 155
-
Performs an approximate comparison of two numbers.
Tolerance may be left nil to use the default value of
sqrt(math.epsilon)
, which means half of the digits are equal.Note that for comparisons of small numbers around zero this function won't give meaningful results, use approxeqabs instead.
Parameters:
- a number
- b number
- tolerance number (optional)
Fields
- nan line 13
-
A value that is never equal to itself.
See also:
- epsilon line 16
- The machine epsilon, an upper bound on the relative error due to rounding.
- max_normal line 19
- The largest representable number
- min_normal line 22
- The smallest normal representable number
- min_subnormal line 25
- The smallest subnormal representable number
- e line 39
- Euler's number. This is the base of the natural logarithm.