math_utils
Source: functions.
Methods
abs(n) → number
Calculates the absolute value of n
Parameter
Name | Type | Optional | Description |
---|---|---|---|
n |
number |
|
- Returns
-
number
absdiff(a, b) → number
Returns the absolute difference between a
and b
Parameters
Name | Type | Optional | Description |
---|---|---|---|
a |
number |
|
|
b |
number |
|
- Returns
-
number
add(a, b) → number
Casts a
and b
using Number
constructor, then adds them using +
operator
Parameters
Name | Type | Optional | Description |
---|---|---|---|
a |
number |
|
|
b |
number |
|
- Returns
-
number
ceil(n) → number
Rounds n
up to the nearest integer
Parameter
Name | Type | Optional | Description |
---|---|---|---|
n |
number |
|
- Returns
-
number
clamp(n, min, max) → number
Clamps n
between min
and max
Parameters
Name | Type | Optional | Description |
---|---|---|---|
n |
number |
|
|
min |
number |
|
|
max |
number |
|
- Returns
-
number
div(a, b) → number
Casts a
and b
using Number
constructor, then divides them using /
operator
Parameters
Name | Type | Optional | Description |
---|---|---|---|
a |
number |
|
|
b |
number |
|
- Returns
-
number
even(n) → boolean
Checks if n
is even
Parameter
Name | Type | Optional | Description |
---|---|---|---|
n |
number |
|
- Returns
-
boolean
exp(b, e[, m]) → number
Calculates b ** m
(optionally modulo m
)
Parameters
Name | Type | Optional | Description |
---|---|---|---|
b |
number |
|
|
e |
number |
|
|
m |
number |
Yes |
- Returns
-
number
floor(n) → number
Rounds n
down to the nearest integer
Parameter
Name | Type | Optional | Description |
---|---|---|---|
n |
number |
|
- Returns
-
number
gt(a, b) → boolean
Checks if a
is greater than b
Parameters
Name | Type | Optional | Description |
---|---|---|---|
a |
number |
|
|
b |
number |
|
- Returns
-
boolean
gte(a, b) → boolean
Checks if a
is greater than or equal to b
Parameters
Name | Type | Optional | Description |
---|---|---|---|
a |
number |
|
|
b |
number |
|
- Returns
-
boolean
inrange(n, min, max) → boolean
Checks if n
is in the range [min
, max
] (inclusive)
Parameters
Name | Type | Optional | Description |
---|---|---|---|
n |
number |
|
|
min |
number |
|
|
max |
number |
|
- Returns
-
boolean
lt(a, b) → boolean
Checks if a
is less than b
Parameters
Name | Type | Optional | Description |
---|---|---|---|
a |
number |
|
|
b |
number |
|
- Returns
-
boolean
lte(a, b) → boolean
Checks if a
is less than or equal to b
Parameters
Name | Type | Optional | Description |
---|---|---|---|
a |
number |
|
|
b |
number |
|
- Returns
-
boolean
max(iter) → number
Calculates the maximum of an iterable using the builtin Math.max
Example
max([2, 1, 3]) // 3
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing any type |
|
The iterable to find the maximum of |
- Returns
-
number
min(iter) → number
Calculates the minimum of an iterable using the builtin Math.min
Example
min([2, 1, 3]) // 1
Parameter
Name | Type | Optional | Description |
---|---|---|---|
iter |
Iterable containing any type |
|
The iterable to find the minimum of |
- Returns
-
number
mod(a, b) → number
Casts a
and b
using Number
constructor, then calculates a % b
(note: this is not true modulo, just remainder after division)
Parameters
Name | Type | Optional | Description |
---|---|---|---|
a |
number |
|
|
b |
number |
|
- Returns
-
number
mul(a, b) → number
Casts a
and b
using Number
constructor, then multiplies them using *
operator
Parameters
Name | Type | Optional | Description |
---|---|---|---|
a |
number |
|
|
b |
number |
|
- Returns
-
number
odd(n) → boolean
Checks if n
is odd
Parameter
Name | Type | Optional | Description |
---|---|---|---|
n |
number |
|
- Returns
-
boolean
round(n) → number
Rounds n
to the nearest integer
Parameter
Name | Type | Optional | Description |
---|---|---|---|
n |
number |
|
- Returns
-
number
sign(n) → number
Calculates the sign of n
Parameter
Name | Type | Optional | Description |
---|---|---|---|
n |
number |
|
- Returns
-
number
sqrt(n) → number
Calculates the square root of n
Parameter
Name | Type | Optional | Description |
---|---|---|---|
n |
number |
|
- Returns
-
number
sub(a, b) → number
Casts a
and b
using Number
constructor, then subtracts them using -
operator
Parameters
Name | Type | Optional | Description |
---|---|---|---|
a |
number |
|
|
b |
number |
|
- Returns
-
number
wrapindex(index, length) → number
Wraps index
around length
(for indexing into to arrays, strings, etc.)
Parameters
Name | Type | Optional | Description |
---|---|---|---|
index |
number |
|
|
length |
number |
|
- Returns
-
number