mathLuaLuau

The 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

math.floorLua
Rounds a number down to the nearest integer
math.ceilLua
Rounds a number up to the nearest integer
math.roundLuau
Rounds a number to the nearest integer
math.modfLua
Splits a number into its integer and fractional parts
math.fmodLua
Returns the remainder of x / y with the quotient rounded toward zero
math.frexpLua
Splits a number into a significand and a binary exponent
math.ldexpLua
Builds a number from a significand and a binary exponent

Signs and limits

math.absLua
Returns the absolute value of a number
math.signLuau
Returns -1, 0 or 1 for the sign of a number
math.minLua
Returns the smallest of its arguments
math.maxLua
Returns the largest of its arguments
math.clampLuau
Limits a number to a range

Powers and logarithms

math.powLua
Raises a number to a power
math.sqrtLua
Returns the square root of a number
math.expLua
Returns e raised to the power of a number
math.logLuaLuau
Returns the logarithm of a number in a given base
math.log10Lua
Returns the base-10 logarithm of a number

Trigonometry

math.sinLua
Returns the sine of an angle in radians
math.cosLua
Returns the cosine of an angle in radians
math.tanLua
Returns the tangent of an angle in radians
math.asinLua
Returns the arc sine of a number in radians
math.acosLua
Returns the arc cosine of a number in radians
math.atanLua
Returns the arc tangent of a number in radians
math.atan2Lua
Returns the arc tangent of y / x using both signs to pick the quadrant
math.sinhLua
Returns the hyperbolic sine of a number
math.coshLua
Returns the hyperbolic cosine of a number
math.tanhLua
Returns the hyperbolic tangent of a number
math.degLua
Converts an angle from radians to degrees
math.radLua
Converts an angle from degrees to radians

Interpolation

math.lerpLuau
Interpolates linearly between two numbers
math.mapLuau
Maps a number linearly from one range onto another

Random numbers

math.randomLua
Returns a random float or integer from the global generator
math.randomseedLua
Reseeds the global random number generator

Noise

math.noiseLuau
Returns the 3D Perlin noise value at a point

Number checks

math.isnanLuau
Checks whether a number is NaN
math.isinfLuau
Checks whether a number is an infinity
math.isfiniteLuau
Checks whether a number is neither NaN nor an infinity

Constants

math.piLua
Holds the ratio of a circle's circumference to its diameter
math.hugeLua
Holds positive infinity
math.nanLuau
Holds a NaN (not a number) value
math.eLuau
Holds Euler's number, the base of the natural logarithm
math.phiLuau
Holds the golden ratio
math.sqrt2Luau
Holds the square root of 2
math.tauLuau
Holds the ratio of a circle's circumference to its radius

Rounding and parts

math.floorLua

function math.floor(n: number): number

Rounds n down to the nearest integer.

Input
print(math.floor(2.9))
print(math.floor(-2.1))
Output
2
-3

math.ceilLua

function math.ceil(n: number): number

Rounds n up to the nearest integer.

Input
print(math.ceil(2.1))
print(math.ceil(-2.9))
Output
3
-2

math.roundLuau

function math.round(n: number): number

Rounds n to the nearest integer. A value exactly halfway between two integers rounds away from zero.

Input
print(math.round(2.5))
print(math.round(-2.5))
print(math.round(2.4))
Output
3
-3
2

math.modfLua

function math.modf(n: number): (number, number)

Returns the integer part and the fractional part of n. Both have the sign of n.

Input
const whole, fraction = math.modf(-1.5)
print(whole)
print(fraction)
Output
-1
-0.5

math.fmodLua

function math.fmod(x: number, y: number): number

Returns 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:

Input
print(math.fmod(-7, 3))
print(-7 % 3)
Output
-1
2

Returns NaN when y is 0.

math.frexpLua

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.ldexpLua

function math.ldexp(s: number, e: number): number

Returns s * 2 ^ e. It's the inverse of math.frexp. e is truncated to an integer.

Signs and limits

math.absLua

function math.abs(n: number): number

Returns the absolute value of n. NaN stays NaN.

math.signLuau

function math.sign(n: number): number

Returns -1 when n is negative, 1 when it's positive and 0 when it's 0 or NaN.

math.minLua

function math.min(first: number, ...: number): number

Returns the smallest of its arguments. Calling it without arguments is an error.

Input
print(math.min(3, 9, 4))
Output
3

math.maxLua

function math.max(first: number, ...: number): number

Returns the largest of its arguments. Calling it without arguments is an error.

Input
print(math.max(3, 9, 4))
Output
9

math.clampLuau

function math.clamp(n: number, min: number, max: number): number

Returns 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.

Input
print(math.clamp(150, 0, 100))
print(math.clamp(-5, 0, 100))
print(math.clamp(42, 0, 100))
Output
100
0
42

A min greater than max is an error.

Powers and logarithms

math.powLua

function math.pow(x: number, y: number): number

Returns x raised to the power of y like x ^ y.

math.sqrtLua

function math.sqrt(n: number): number

Returns the square root of n. Returns NaN when n is negative.

math.expLua

function math.exp(n: number): number

Returns math.e raised to the power of n.

math.logLuaLuau

function math.log(n: number, base: number?): number

Returns 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.

Input
print(math.log(8, 2))
print(math.log(1000, 10))
Output
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.log10Lua

function math.log10(n: number): number

Returns 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.sinLua

function math.sin(n: number): number

Returns the sine of n, an angle in radians. The result is in the range -1 to 1.

math.cosLua

function math.cos(n: number): number

Returns the cosine of n, an angle in radians. The result is in the range -1 to 1.

math.tanLua

function math.tan(n: number): number

Returns the tangent of n, an angle in radians.

math.asinLua

function math.asin(n: number): number

Returns 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.acosLua

function math.acos(n: number): number

Returns the arc cosine of n in radians, in the range 0 to math.pi. Returns NaN when n is outside -1 to 1.

math.atanLua

function math.atan(n: number): number

Returns the arc tangent of n in radians, in the range -math.pi / 2 to math.pi / 2.

math.atan2Lua

function math.atan2(y: number, x: number): number

Returns 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:

Input
print(math.deg(math.atan2(1, 1)))
print(math.deg(math.atan2(1, -1)))
Output
45
135

math.sinhLua

function math.sinh(n: number): number

Returns the hyperbolic sine of n.

math.coshLua

function math.cosh(n: number): number

Returns the hyperbolic cosine of n.

math.tanhLua

function math.tanh(n: number): number

Returns the hyperbolic tangent of n.

math.degLua

function math.deg(n: number): number

Converts n from radians to degrees.

math.radLua

function math.rad(n: number): number

Converts n from degrees to radians.

Interpolation

math.lerpLuau

function math.lerp(a: number, b: number, t: number): number

Interpolates 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.

Input
print(math.lerp(10, 20, 0.25))
print(math.lerp(10, 20, 1.5))
Output
12.5
25

math.mapLuau

function math.map(x: number, inmin: number, inmax: number, outmin: number, outmax: number): number

Maps 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.

Input
-- a 0 to 255 color channel as a fraction from 0 to 1
print(math.map(51, 0, 255, 0, 1))
Output
0.2

Random numbers

math.randomLua

function math.random(): number
function math.random(n: number): number
function math.random(min: number, max: number): number

Returns a random number from the global random number generator. Its result depends on the arguments:

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.randomseedLua

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.

Input
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, ", "))
Output
true

Noise

math.noiseLuau

function math.noise(x: number, y: number?, z: number?): number

Returns 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.

Input
print(math.noise(1.5, 2.25, 0.75))
print(math.noise(1, 2, 3))
Output
-0.21080541610717773
0

Number checks

math.isnanLuau

function math.isnan(x: number): boolean

Returns true when x is NaN. NaN is the only value not equal to itself and x ~= x gives the same answer.

math.isinfLuau

function math.isinf(x: number): boolean

Returns true when x is math.huge or -math.huge.

math.isfiniteLuau

function math.isfinite(x: number): boolean

Returns true when x is neither NaN nor an infinity.

Input
print(math.isfinite(1 / 3))
print(math.isfinite(1 / 0))
print(math.isfinite(0 / 0))
Output
true
false
false

Constants

math.piLua

math.pi: number

The ratio of a circle's circumference to its diameter, about 3.14159.

math.hugeLua

math.huge: number

Positive infinity. It's greater than every other number and -math.huge is less than every other number.

math.nanLuau

math.nan: number

A NaN (not a number) value. It isn't equal to anything, itself included. Use math.isnan to test for it.

math.eLuau

math.e: number

Euler's number, the base of the natural logarithm, about 2.71828.

math.phiLuau

math.phi: number

The golden ratio, about 1.61803.

math.sqrt2Luau

math.sqrt2: number

The square root of 2, about 1.41421.

math.tauLuau

math.tau: number

The ratio of a circle's circumference to its radius, 2 * math.pi.