Skip to content

math

The complete math library. Functions marked (Luau) are Luau extensions beyond standard Lua 5.1.

The circle constant π.

print(math.pi) --> 3.1415926535898

Positive infinity; -math.huge is negative infinity.

print(math.huge) --> inf
print(math.huge > 1e300) --> true

Rounds toward negative infinity.

print(math.floor(2.7)) --> 2
print(math.floor(-2.7)) --> -3

Rounds toward positive infinity.

print(math.ceil(2.1)) --> 3
print(math.ceil(-2.1)) --> -2

Rounds half away from zero (unlike floor/ceil symmetry).

print(math.round(2.5)) --> 3
print(math.round(-2.5)) --> -3
print(math.round(2.4)) --> 2

Constrains x to the inclusive range.

print(math.clamp(5, 0, 3)) --> 3
print(math.clamp(-1, 0, 3)) --> 0
print(math.clamp(2, 0, 3)) --> 2

Linear interpolation, not clampedt outside [0,1] extrapolates.

print(math.lerp(0, 10, 0.5)) --> 5
print(math.lerp(0, 10, 1.5)) --> 15 (extrapolates)
print(math.lerp(0, 10, 2.0)) --> 20

math.map(x, inMin, inMax, outMin, outMax) (Luau)

Section titled “math.map(x, inMin, inMax, outMin, outMax) (Luau)”

Remaps x from one range onto another (unclamped).

print(math.map(5, 0, 10, 100, 200)) --> 150
print(math.map(0.5, 0, 1, -1, 1)) --> 0

Returns -1, 0, or 1.

print(math.sign(-7)) --> -1
print(math.sign(0)) --> 0
print(math.sign(7)) --> 1
print(math.abs(-5)) --> 5
print(math.abs(5.5)) --> 5.5
print(math.sqrt(144)) --> 12

Same as x ^ y.

print(math.pow(2, 10)) --> 1024

e^x.

print(math.exp(0)) --> 1
print(math.exp(1)) --> 2.718281828459

Natural log; accepts an optional base in Luau (math.log(x, base)).

print(math.log(1)) --> 0
print(math.log(8, 2)) --> 3

Base-10 logarithm.

print(math.log10(1000)) --> 3

Angles in radians throughout.

print(math.sin(0)) --> 0
print(math.cos(0)) --> 1
print(math.tan(math.pi / 4)) --> 1 (approximately 0.999...)

math.asin(x), math.acos(x), math.atan(y, x?)

Section titled “math.asin(x), math.acos(x), math.atan(y, x?)”

Inverse functions. atan accepts the y, x pair like atan2.

print(math.asin(1)) --> 1.5707963267949 (pi/2)
print(math.acos(1)) --> 0
print(math.atan(1, 1)) --> 0.78539816339745 (pi/4)

Angle of the point (x, y) in radians, full quadrant awareness.

print(math.atan2(1, 0)) --> 1.5707963267949
print(math.atan2(-1, 0)) --> -1.5707963267949

Radian ↔ degree conversion.

print(math.deg(math.pi)) --> 180
print(math.rad(180)) --> 3.1415926535898
print(math.sinh(0)) --> 0
print(math.cosh(0)) --> 1
print(math.tanh(0)) --> 0

C-style remainder (result keeps the sign of x).

print(math.fmod(7, 3)) --> 1
print(math.fmod(-7, 3)) --> -1 (differs from Lua's % operator)

Splits into integral and fractional parts.

print(math.modf(3.7)) --> 3 0.7
print(math.modf(-3.7)) --> -3 -0.7

Returns mantissa m and exponent e such that x = m * 2^e.

local m, e = math.frexp(8)
print(m, e) --> 0.5 4

Recombines m * 2^e — the inverse of frexp.

print(math.ldexp(0.5, 4)) --> 8

Any number of arguments.

print(math.max(1, 7, 3)) --> 7
print(math.min(1, 7, 3)) --> 1
print(math.isnan(0/0)) --> true
print(math.isnan(1)) --> false
print(math.isinf(math.huge)) --> true
print(math.isinf(-math.huge)) --> true
print(math.isinf(1)) --> false
print(math.isfinite(1)) --> true
print(math.isfinite(math.huge)) --> false
print(math.isfinite(0/0)) --> false

No args: uniform float in [0, 1). One arg: integer in [1, m]. Two args: integer in [m, n].

print(math.random()) --> e.g. 0.41702228463094
print(math.random(6)) --> e.g. 3 (a die roll)
print(math.random(10, 20)) --> e.g. 15

Seeds the generator.

math.randomseed(42)
print(math.random(100)) --> deterministic on every run with the same seed

Deterministic Perlin-style value noise; output roughly in [-0.5, 0.5].

print(math.noise(1.25, 5.0)) --> 0.22412109375 (same every run)
-- animate smoothly along one axis:
usems.hook("frame", function(dt)
t = (t or 0) + dt
local n = math.noise(t * 0.1)
-- n drifts smoothly between roughly -0.5 and 0.5
end)