Math API

Mathematical functions wrapping the C standard library. All functions accept and return numbers.

Rounding & Truncation

abs(value)

Returns the absolute value.

abs(-5)     // 5
abs(3.14)   // 3.14
floor(value)

Rounds down toward negative infinity.

floor(3.7)    // 3
floor(-1.2)   // -2
ceil(value)

Rounds up toward positive infinity.

ceil(3.2)     // 4
ceil(-1.7)    // -1
round(value)

Rounds to the nearest integer. Halfway values round away from zero.

round(3.5)    // 4
round(-1.5)   // -2
trunc(value)

Truncates toward zero (removes fractional part).

trunc(3.9)    // 3
trunc(-3.9)   // -3
sign(value)

Returns the sign: -1, 0, or 1.

sign(42)     // 1
sign(-7)     // -1
sign(0)      // 0

Powers & Roots

sqrt(value)

Returns the square root.

sqrt(16)    // 4
sqrt(2)     // 1.41421...
cbrt(value)

Returns the cube root.

cbrt(27)    // 3
cbrt(-8)    // -2
pow(base, exponent)

Returns base raised to the power of exponent.

pow(2, 10)     // 1024
pow(25, 0.5)   // 5

Exponential & Logarithmic

exp(value)

Returns e raised to the power of value.

exp2(value)

Returns 2 raised to the power of value.

expm1(value)

Returns exp(value) - 1 with better precision for small values.

log(value)

Returns the natural logarithm (base e).

log10(value)

Returns the base-10 logarithm.

log10(100)    // 2
log10(1000)   // 3
log2(value)

Returns the base-2 logarithm.

log2(8)      // 3
log2(1024)   // 10
log1p(value)

Returns log(1 + value) with better precision for small values.

Trigonometric

All trigonometric functions use radians.

sin(value)  /  cos(value)  /  tan(value)

Returns the sine, cosine, or tangent.

sin(0)                 // 0
cos(0)                 // 1
tan(3.14159265 / 4)    // ~1
asin(value)  /  acos(value)  /  atan(value)

Inverse trigonometric functions. Returns radians.

atan2(y, x)

Returns the arc tangent of y/x using both signs to determine the quadrant. Result is in (-π, π].

atan2(1, 1)     // ~0.7854 (π/4)
atan2(0, -1)    // ~3.14159 (π)

Hyperbolic

sinh(value)  /  cosh(value)  /  tanh(value)

Hyperbolic sine, cosine, and tangent.

asinh(value)  /  acosh(value)  /  atanh(value)

Inverse hyperbolic functions.

Min / Max / Clamp

min(a, b)

Returns the smaller of two numbers.

max(a, b)

Returns the larger of two numbers.

clamp(value, minVal, maxVal)

Clamps a value to the range [minVal, maxVal].

clamp(5, 0, 10)     // 5
clamp(-3, 0, 10)    // 0
clamp(15, 0, 10)    // 10

Modulo & Remainder

fmod(x, y)

Floating-point remainder of x / y (same sign as x).

fmod(5.5, 2)     // 1.5
fmod(-5.5, 2)    // -1.5
remainder(x, y)

IEEE remainder of x / y (rounds quotient to nearest integer).

Utility

hypot(x, y)

Returns sqrt(x² + y²) computed without overflow.

hypot(3, 4)    // 5
hypot(5, 12)   // 13
toRadians(degrees)

Converts degrees to radians.

toRadians(180)   // ~3.14159 (π)
toRadians(90)    // ~1.5708 (π/2)
toDegrees(radians)

Converts radians to degrees.

toDegrees(3.14159265)   // ~180

Interpolation & Mapping

lerp(start, end, t)

Linearly interpolates between start and end by factor t.

lerp(0, 100, 0.5)    // 50
lerp(0, 100, 0.25)   // 25
map(value, inMin, inMax, outMin, outMax)

Re-maps a value from one range to another.

map(5, 0, 10, 0, 100)      // 50
map(0.5, 0, 1, -1, 1)      // 0

Type Checks

isNaN(value)

Checks if a number is NaN.

isInfinite(value)

Checks if a number is positive or negative infinity.

isFinite(value)

Checks if a number is finite (not NaN and not infinite).

isFinite(42)       // true
isFinite(1 / 0)    // false

See also: Conversions APIList APIString API