mathInherited from Lua 5.1's standard library.
LuaAdded by Luau: math.round, math.sign, math.clamp, math.lerp, math.map, math.noise, math.isnan, math.isinf, math.isfinite, math.nan, math.e, math.phi, math.sqrt2, math.tau
Changed by Luau: math.log, math.noise
Luau
math.round, math.sign, math.clamp, math.lerp, math.map, math.noise, math.isnan, math.isinf, math.isfinite, math.nan, math.e, math.phi, math.sqrt2, math.taumath.log, math.noiseThe math library rounds numbers and computes powers, logarithms, trigonometry, interpolation, random numbers and noise. It also holds constants like math.pi.
Its functions take and return number values (64-bit floats). Most follow the C standard library and return NaN or an infinity for input outside their domain instead of raising an error.
Summary
Rounding and parts
function math.floor(n: number): numbermath.floorInherited from Lua 5.1's standard library.
Lua- Rounds a number down to the nearest integer
function math.ceil(n: number): numbermath.ceilInherited from Lua 5.1's standard library.
Lua- Rounds a number up to the nearest integer
function math.round(n: number): numbermath.roundAdded by Luau 0.501 or earlier.
Luau- Rounds a number to the nearest integer
function math.modf(n: number): (number, number)math.modfInherited from Lua 5.1's standard library.
Lua- Splits a number into its integer and fractional parts
function math.fmod(x: number, y: number): numbermath.fmodInherited from Lua 5.1's standard library.
Lua- Returns the remainder of x / y with the quotient rounded toward zero
function math.frexp(n: number): (number, number)math.frexpInherited from Lua 5.1's standard library.
Lua- Splits a number into a significand and a binary exponent
function math.ldexp(s: number, e: number): numbermath.ldexpInherited from Lua 5.1's standard library.
Lua- Builds a number from a significand and a binary exponent
Signs and limits
function math.abs(n: number): numbermath.absInherited from Lua 5.1's standard library.
Lua- Returns the absolute value of a number
function math.sign(n: number): numbermath.signAdded by Luau 0.501 or earlier.
Luau- Returns -1, 0 or 1 for the sign of a number
function math.min(first: number, ...: number): numbermath.minInherited from Lua 5.1's standard library.
Lua- Returns the smallest of its arguments
function math.max(first: number, ...: number): numbermath.maxInherited from Lua 5.1's standard library.
Lua- Returns the largest of its arguments
function math.clamp(n: number, min: number, max: number): numbermath.clampAdded by Luau 0.501 or earlier.
Luau- Limits a number to a range
Powers and logarithms
function math.pow(x: number, y: number): numbermath.powInherited from Lua 5.1's standard library.
Lua- Raises a number to a power
function math.sqrt(n: number): numbermath.sqrtInherited from Lua 5.1's standard library.
Lua- Returns the square root of a number
function math.exp(n: number): numbermath.expInherited from Lua 5.1's standard library.
Lua- Returns e raised to the power of a number
function math.log(n: number, base: number?): numbermath.logInherited from Lua 5.1's standard library.
LuaChanged by Luau 0.501 or earlier: Takes an optional base, as Lua 5.2 does.
Luau- Returns the logarithm of a number in a given base
function math.log10(n: number): numbermath.log10Inherited from Lua 5.1's standard library.
Lua- Returns the base-10 logarithm of a number
Trigonometry
function math.sin(n: number): numbermath.sinInherited from Lua 5.1's standard library.
Lua- Returns the sine of an angle in radians
function math.cos(n: number): numbermath.cosInherited from Lua 5.1's standard library.
Lua- Returns the cosine of an angle in radians
function math.tan(n: number): numbermath.tanInherited from Lua 5.1's standard library.
Lua- Returns the tangent of an angle in radians
function math.asin(n: number): numbermath.asinInherited from Lua 5.1's standard library.
Lua- Returns the arc sine of a number in radians
function math.acos(n: number): numbermath.acosInherited from Lua 5.1's standard library.
Lua- Returns the arc cosine of a number in radians
function math.atan(n: number): numbermath.atanInherited from Lua 5.1's standard library.
Lua- Returns the arc tangent of a number in radians
function math.atan2(y: number, x: number): numbermath.atan2Inherited from Lua 5.1's standard library.
Lua- Returns the arc tangent of y / x using both signs to pick the quadrant
function math.sinh(n: number): numbermath.sinhInherited from Lua 5.1's standard library.
Lua- Returns the hyperbolic sine of a number
function math.cosh(n: number): numbermath.coshInherited from Lua 5.1's standard library.
Lua- Returns the hyperbolic cosine of a number
function math.tanh(n: number): numbermath.tanhInherited from Lua 5.1's standard library.
Lua- Returns the hyperbolic tangent of a number
function math.deg(n: number): numbermath.degInherited from Lua 5.1's standard library.
Lua- Converts an angle from radians to degrees
function math.rad(n: number): numbermath.radInherited from Lua 5.1's standard library.
Lua- Converts an angle from degrees to radians
Interpolation
function math.lerp(a: number, b: number, t: number): numbermath.lerpAdded by Luau 0.656.
Luau- Interpolates linearly between two numbers
function math.map(x: number, inmin: number, inmax: number, outmin: number, outmax: number): numbermath.mapAdded by Luau 0.648.
Luau- Maps a number linearly from one range onto another
Random numbers
function math.random(): number function math.random(n: number): number function math.random(min: number, max: number): numbermath.randomInherited from Lua 5.1's standard library.
Lua- Returns a random float or integer from the global generator
function math.randomseed(seed: number)math.randomseedInherited from Lua 5.1's standard library.
Lua- Reseeds the global random number generator
Noise
function math.noise(x: number, y: number?, z: number?): numbermath.noiseAdded by Luau 0.501 or earlier.
Changed by Luau 0.716: Wraps inputs every 256 units so large inputs don't lose precision.
Luau- Returns the 3D Perlin noise value at a point
Number checks
function math.isnan(x: number): booleanmath.isnanAdded by Luau 0.701.
Luau- Checks whether a number is NaN
function math.isinf(x: number): booleanmath.isinfAdded by Luau 0.701.
Luau- Checks whether a number is an infinity
function math.isfinite(x: number): booleanmath.isfiniteAdded by Luau 0.701.
Luau- Checks whether a number is neither NaN nor an infinity
Constants
math.pi: numbermath.piInherited from Lua 5.1's standard library.
Lua- Holds the ratio of a circle's circumference to its diameter
math.huge: numbermath.hugeInherited from Lua 5.1's standard library.
Lua- Holds positive infinity
math.nan: numbermath.nanAdded by Luau 0.711.
Luau- Holds a NaN (not a number) value
math.e: numbermath.eAdded by Luau 0.711.
Luau- Holds Euler's number, the base of the natural logarithm
math.phi: numbermath.phiAdded by Luau 0.711.
Luau- Holds the golden ratio
math.sqrt2: numbermath.sqrt2Added by Luau 0.711.
Luau- Holds the square root of 2
math.tau: numbermath.tauAdded by Luau 0.711.
Luau- Holds the ratio of a circle's circumference to its radius
Rounding and parts
math.floorInherited from Lua 5.1's standard library.
Lua
function math.floor(n: number): numberRounds n down to the nearest integer.
print(math.floor(2.9))
print(math.floor(-2.1))2 -3
math.ceilInherited from Lua 5.1's standard library.
Lua
function math.ceil(n: number): numberRounds n up to the nearest integer.
print(math.ceil(2.1))
print(math.ceil(-2.9))3 -2
math.roundAdded by Luau 0.501 or earlier.
Luau
function math.round(n: number): numberRounds n to the nearest integer. A value exactly halfway between two integers rounds away from zero.
print(math.round(2.5))
print(math.round(-2.5))
print(math.round(2.4))3 -3 2
math.modfInherited from Lua 5.1's standard library.
Lua
function math.modf(n: number): (number, number)Returns the integer part and the fractional part of n. Both have the sign of n.
const whole, fraction = math.modf(-1.5)
print(whole)
print(fraction)-1 -0.5
math.fmodInherited from Lua 5.1's standard library.
Lua
function math.fmod(x: number, y: number): numberReturns the remainder of x / y with the quotient rounded toward zero. The result has the sign of x. The % operator rounds the quotient down instead and its result has the sign of y:
print(math.fmod(-7, 3))
print(-7 % 3)-1 2
Returns NaN when y is 0.
math.frexpInherited from Lua 5.1's standard library.
Lua
function math.frexp(n: number): (number, number)Returns a significand s and a binary exponent e such that n == s * 2 ^ e. The absolute value of s is at least 0.5 and less than 1, unless n is 0.
math.ldexpInherited from Lua 5.1's standard library.
Lua
function math.ldexp(s: number, e: number): numberReturns s * 2 ^ e. It's the inverse of math.frexp. e is truncated to an integer.
Signs and limits
math.absInherited from Lua 5.1's standard library.
Lua
function math.abs(n: number): numberReturns the absolute value of n. NaN stays NaN.
math.signAdded by Luau 0.501 or earlier.
Luau
function math.sign(n: number): numberReturns -1 when n is negative, 1 when it's positive and 0 when it's 0 or NaN.
math.minInherited from Lua 5.1's standard library.
Lua
function math.min(first: number, ...: number): numberReturns the smallest of its arguments. Calling it without arguments is an error.
print(math.min(3, 9, 4))3
math.maxInherited from Lua 5.1's standard library.
Lua
function math.max(first: number, ...: number): numberReturns the largest of its arguments. Calling it without arguments is an error.
print(math.max(3, 9, 4))9
math.clampAdded by Luau 0.501 or earlier.
Luau
function math.clamp(n: number, min: number, max: number): numberReturns n limited to the range min to max. It returns min when n is below it, max when n is above it and n otherwise.
print(math.clamp(150, 0, 100))
print(math.clamp(-5, 0, 100))
print(math.clamp(42, 0, 100))100 0 42
A min greater than max is an error.
Powers and logarithms
math.powInherited from Lua 5.1's standard library.
Lua
function math.pow(x: number, y: number): numberReturns x raised to the power of y like x ^ y.
math.sqrtInherited from Lua 5.1's standard library.
Lua
function math.sqrt(n: number): numberReturns the square root of n. Returns NaN when n is negative.
math.expInherited from Lua 5.1's standard library.
Lua
function math.exp(n: number): numberReturns math.e raised to the power of n.
math.logInherited from Lua 5.1's standard library.
LuaChanged by Luau 0.501 or earlier: Takes an optional base, as Lua 5.2 does.
Luau
function math.log(n: number, base: number?): numberReturns the logarithm of n in base. Without base it returns the natural logarithm (base math.e). Returns NaN when n is negative and -math.huge when it's 0.
print(math.log(8, 2))
print(math.log(1000, 10))3 3
Bases 2 and 10 use dedicated functions and give exact results for exact powers. Other bases compute math.log(n) / math.log(base).
math.log10Inherited from Lua 5.1's standard library.
Lua
function math.log10(n: number): numberReturns the base-10 logarithm of n. Returns NaN when n is negative and -math.huge when it's 0. It's the same as math.log(n, 10).
Trigonometry
math.sinInherited from Lua 5.1's standard library.
Lua
function math.sin(n: number): numberReturns the sine of n, an angle in radians. The result is in the range -1 to 1.
math.cosInherited from Lua 5.1's standard library.
Lua
function math.cos(n: number): numberReturns the cosine of n, an angle in radians. The result is in the range -1 to 1.
math.tanInherited from Lua 5.1's standard library.
Lua
function math.tan(n: number): numberReturns the tangent of n, an angle in radians.
math.asinInherited from Lua 5.1's standard library.
Lua
function math.asin(n: number): numberReturns the arc sine of n in radians, in the range -math.pi / 2 to math.pi / 2. Returns NaN when n is outside -1 to 1.
math.acosInherited from Lua 5.1's standard library.
Lua
function math.acos(n: number): numberReturns the arc cosine of n in radians, in the range 0 to math.pi. Returns NaN when n is outside -1 to 1.
math.atanInherited from Lua 5.1's standard library.
Lua
function math.atan(n: number): numberReturns the arc tangent of n in radians, in the range -math.pi / 2 to math.pi / 2.
math.atan2Inherited from Lua 5.1's standard library.
Lua
function math.atan2(y: number, x: number): numberReturns the arc tangent of y / x in radians, in the range -math.pi to math.pi. It uses the signs of both arguments to pick the quadrant and handles an x of 0. That makes it the function for the angle of a direction:
print(math.deg(math.atan2(1, 1)))
print(math.deg(math.atan2(1, -1)))45 135
math.sinhInherited from Lua 5.1's standard library.
Lua
function math.sinh(n: number): numberReturns the hyperbolic sine of n.
math.coshInherited from Lua 5.1's standard library.
Lua
function math.cosh(n: number): numberReturns the hyperbolic cosine of n.
math.tanhInherited from Lua 5.1's standard library.
Lua
function math.tanh(n: number): numberReturns the hyperbolic tangent of n.
math.degInherited from Lua 5.1's standard library.
Lua
function math.deg(n: number): numberConverts n from radians to degrees.
math.radInherited from Lua 5.1's standard library.
Lua
function math.rad(n: number): numberConverts n from degrees to radians.
Interpolation
math.lerpAdded by Luau 0.656.
Luau
function math.lerp(a: number, b: number, t: number): numberInterpolates linearly from a to b by the factor t and returns a + (b - a) * t. A t of 1 returns exactly b. t isn't clamped: values outside 0 to 1 extrapolate past a or b.
print(math.lerp(10, 20, 0.25))
print(math.lerp(10, 20, 1.5))12.5 25
math.mapAdded by Luau 0.648.
Luau
function math.map(x: number, inmin: number, inmax: number, outmin: number, outmax: number): numberMaps x linearly from the range inmin to inmax onto the range outmin to outmax. The result isn't clamped when x is outside the input range.
-- a 0 to 255 color channel as a fraction from 0 to 1
print(math.map(51, 0, 255, 0, 1))0.2
Random numbers
math.randomInherited from Lua 5.1's standard library.
Lua
function math.random(): number
function math.random(n: number): number
function math.random(min: number, max: number): numberReturns a random number from the global random number generator. Its result depends on the arguments:
- With none, a float from 0 up to but not including 1.
- With
n, an integer from 1 toninclusive. - With
minandmax, an integer frommintomaxinclusive.
The arguments are truncated to integers. An empty range, like math.random(0) or a min greater than max, is an error. The generator is PCG32. Each Luwu VM seeds it differently when it starts.
math.randomseedInherited from Lua 5.1's standard library.
Lua
function math.randomseed(seed: number)Reseeds the global random number generator. Every call to math.random after it returns a sequence that depends only on seed.
math.randomseed(42)
const first = { math.random(100), math.random(100), math.random(100) }
math.randomseed(42)
const second = { math.random(100), math.random(100), math.random(100) }
print(table.concat(first, ", ") == table.concat(second, ", "))true
Noise
math.noiseAdded by Luau 0.501 or earlier.
Changed by Luau 0.716: Wraps inputs every 256 units so large inputs don't lose precision.
Luau
function math.noise(x: number, y: number?, z: number?): numberReturns the 3D Perlin noise value at the point (x, y, z). y and z default to 0. The result is in the range -1 to 1 and changes smoothly as the point moves. That makes noise useful for terrain, textures and other natural-looking variation. The same point always gives the same value.
print(math.noise(1.5, 2.25, 0.75))
print(math.noise(1, 2, 3))-0.21080541610717773 0
- Points where every coordinate is an integer always return 0.
- The noise repeats every 256 units along each axis.
- The coordinates are converted to 32-bit floats. Large coordinates lose precision in that conversion and above 2^24 (about 16.7 million) the fractional part is gone. With the precision fix on, each coordinate first wraps into one 256-unit period at full 64-bit precision. A large coordinate then returns the same value as the matching small one.
Number checks
math.isnanAdded by Luau 0.701.
Luau
function math.isnan(x: number): booleanReturns true when x is NaN. NaN is the only value not equal to itself and x ~= x gives the same answer.
math.isinfAdded by Luau 0.701.
Luau
function math.isinf(x: number): booleanReturns true when x is math.huge or -math.huge.
math.isfiniteAdded by Luau 0.701.
Luau
function math.isfinite(x: number): booleanReturns true when x is neither NaN nor an infinity.
print(math.isfinite(1 / 3))
print(math.isfinite(1 / 0))
print(math.isfinite(0 / 0))true false false
Constants
math.piInherited from Lua 5.1's standard library.
Lua
math.pi: numberThe ratio of a circle's circumference to its diameter, about 3.14159.
math.hugeInherited from Lua 5.1's standard library.
Lua
math.huge: numberPositive infinity. It's greater than every other number and -math.huge is less than every other number.
math.nanAdded by Luau 0.711.
Luau
math.nan: numberA NaN (not a number) value. It isn't equal to anything, itself included. Use math.isnan to test for it.
math.eAdded by Luau 0.711.
Luau
math.e: numberEuler's number, the base of the natural logarithm, about 2.71828.
math.phiAdded by Luau 0.711.
Luau
math.phi: numberThe golden ratio, about 1.61803.
math.sqrt2Added by Luau 0.711.
Luau
math.sqrt2: numberThe square root of 2, about 1.41421.
math.tauAdded by Luau 0.711.
Luau
math.tau: numberThe ratio of a circle's circumference to its radius, 2 * math.pi.