math
The complete math library. Functions marked (Luau) are Luau extensions
beyond standard Lua 5.1.
Constants
Section titled “Constants”math.pi
Section titled “math.pi”The circle constant π.
print(math.pi) --> 3.1415926535898math.huge
Section titled “math.huge”Positive infinity; -math.huge is negative infinity.
print(math.huge) --> infprint(math.huge > 1e300) --> trueRounding
Section titled “Rounding”math.floor(x)
Section titled “math.floor(x)”Rounds toward negative infinity.
print(math.floor(2.7)) --> 2print(math.floor(-2.7)) --> -3math.ceil(x)
Section titled “math.ceil(x)”Rounds toward positive infinity.
print(math.ceil(2.1)) --> 3print(math.ceil(-2.1)) --> -2math.round(x) (Luau)
Section titled “math.round(x) (Luau)”Rounds half away from zero (unlike floor/ceil symmetry).
print(math.round(2.5)) --> 3print(math.round(-2.5)) --> -3print(math.round(2.4)) --> 2Clamping, mapping, interpolation
Section titled “Clamping, mapping, interpolation”math.clamp(x, min, max) (Luau)
Section titled “math.clamp(x, min, max) (Luau)”Constrains x to the inclusive range.
print(math.clamp(5, 0, 3)) --> 3print(math.clamp(-1, 0, 3)) --> 0print(math.clamp(2, 0, 3)) --> 2math.lerp(a, b, t) (Luau)
Section titled “math.lerp(a, b, t) (Luau)”Linear interpolation, not clamped — t outside [0,1] extrapolates.
print(math.lerp(0, 10, 0.5)) --> 5print(math.lerp(0, 10, 1.5)) --> 15 (extrapolates)print(math.lerp(0, 10, 2.0)) --> 20math.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)) --> 150print(math.map(0.5, 0, 1, -1, 1)) --> 0math.sign(x) (Luau)
Section titled “math.sign(x) (Luau)”Returns -1, 0, or 1.
print(math.sign(-7)) --> -1print(math.sign(0)) --> 0print(math.sign(7)) --> 1Absolute value, powers, roots
Section titled “Absolute value, powers, roots”math.abs(x)
Section titled “math.abs(x)”print(math.abs(-5)) --> 5print(math.abs(5.5)) --> 5.5math.sqrt(x)
Section titled “math.sqrt(x)”print(math.sqrt(144)) --> 12math.pow(x, y)
Section titled “math.pow(x, y)”Same as x ^ y.
print(math.pow(2, 10)) --> 1024math.exp(x)
Section titled “math.exp(x)”e^x.
print(math.exp(0)) --> 1print(math.exp(1)) --> 2.718281828459Logarithms
Section titled “Logarithms”math.log(x)
Section titled “math.log(x)”Natural log; accepts an optional base in Luau (math.log(x, base)).
print(math.log(1)) --> 0print(math.log(8, 2)) --> 3math.log10(x)
Section titled “math.log10(x)”Base-10 logarithm.
print(math.log10(1000)) --> 3Trigonometry
Section titled “Trigonometry”Angles in radians throughout.
math.sin(x), math.cos(x), math.tan(x)
Section titled “math.sin(x), math.cos(x), math.tan(x)”print(math.sin(0)) --> 0print(math.cos(0)) --> 1print(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)) --> 0print(math.atan(1, 1)) --> 0.78539816339745 (pi/4)math.atan2(y, x)
Section titled “math.atan2(y, x)”Angle of the point (x, y) in radians, full quadrant awareness.
print(math.atan2(1, 0)) --> 1.5707963267949print(math.atan2(-1, 0)) --> -1.5707963267949math.deg(r), math.rad(d)
Section titled “math.deg(r), math.rad(d)”Radian ↔ degree conversion.
print(math.deg(math.pi)) --> 180print(math.rad(180)) --> 3.1415926535898Hyperbolic
Section titled “Hyperbolic”math.sinh(x), math.cosh(x), math.tanh(x)
Section titled “math.sinh(x), math.cosh(x), math.tanh(x)”print(math.sinh(0)) --> 0print(math.cosh(0)) --> 1print(math.tanh(0)) --> 0Modulo and decomposition
Section titled “Modulo and decomposition”math.fmod(x, y)
Section titled “math.fmod(x, y)”C-style remainder (result keeps the sign of x).
print(math.fmod(7, 3)) --> 1print(math.fmod(-7, 3)) --> -1 (differs from Lua's % operator)math.modf(x)
Section titled “math.modf(x)”Splits into integral and fractional parts.
print(math.modf(3.7)) --> 3 0.7print(math.modf(-3.7)) --> -3 -0.7math.frexp(x)
Section titled “math.frexp(x)”Returns mantissa m and exponent e such that x = m * 2^e.
local m, e = math.frexp(8)print(m, e) --> 0.5 4math.ldexp(m, e)
Section titled “math.ldexp(m, e)”Recombines m * 2^e — the inverse of frexp.
print(math.ldexp(0.5, 4)) --> 8Min/max
Section titled “Min/max”math.max(...), math.min(...)
Section titled “math.max(...), math.min(...)”Any number of arguments.
print(math.max(1, 7, 3)) --> 7print(math.min(1, 7, 3)) --> 1Classification (Luau)
Section titled “Classification (Luau)”math.isnan(x)
Section titled “math.isnan(x)”print(math.isnan(0/0)) --> trueprint(math.isnan(1)) --> falsemath.isinf(x)
Section titled “math.isinf(x)”print(math.isinf(math.huge)) --> trueprint(math.isinf(-math.huge)) --> trueprint(math.isinf(1)) --> falsemath.isfinite(x)
Section titled “math.isfinite(x)”print(math.isfinite(1)) --> trueprint(math.isfinite(math.huge)) --> falseprint(math.isfinite(0/0)) --> falseRandomness
Section titled “Randomness”math.random(m?, n?)
Section titled “math.random(m?, n?)”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.41702228463094print(math.random(6)) --> e.g. 3 (a die roll)print(math.random(10, 20)) --> e.g. 15math.randomseed(seed)
Section titled “math.randomseed(seed)”Seeds the generator.
math.randomseed(42)print(math.random(100)) --> deterministic on every run with the same seedNoise (Luau)
Section titled “Noise (Luau)”math.noise(x, y?, z?)
Section titled “math.noise(x, y?, z?)”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.5end)