Skip to main content
Version: bleeding-edge ๐Ÿฉธ

math

This library provides basic mathematical functions. It provides all its functions and constants inside the table math


๐ŸŽ’ Examplesโ€‹

-- Rounding
local a = math.floor(3.7) -- 3
local b = math.ceil(3.2) -- 4

-- Random numbers
local chance = math.random() -- float between 0 and 1
local dice = math.random(1, 6) -- integer between 1 and 6

-- Min, Max and Absolute
local lowest = math.min(10, 5, 8) -- 5
local distance = math.abs(-250) -- 250

-- Trigonometry works with radians
local sine = math.sin(math.rad(90)) -- 1.0

๐Ÿ—ผ Static Propertiesโ€‹

ValueName
3.141592653589793math.pi
2^1024math.huge

๐Ÿ—ฟ Static Functionsโ€‹

ceil: numberReturns the smallest integer greater than or equal to the given value
tan: numberReturn the tangent value for a given value in radians
min: numberReturn the minimum value from a variable length list of arguments
fmod: numberReturns the remainder of the division of base by modulator that rounds the quotient towards zero
exp: numberReturns e (the base of natural logarithms) raised to the power exponent
random: numberGenerates pseudo-random numbers uniformly distributed
rad: numberConvert from degrees to radians
sin: numberReturn the sine value for a given value in radians
cos: numberReturn the cosine value for a given value in radians
asin: numberReturn the inverse sine in radians of the given value
randomseedSets a seed for the pseudo-random generator: Equal seeds produce equal sequences of numbers
floor: numberReturns the largest integer less than or equal to the given value
max: numberReturn the maximum value from a variable length list of arguments
atan: numberReturns the arc tangent in radians. Pass y and x to get the angle of the point (x, y), using the signs of both to find the quadrant
acos: numberReturn the inverse cosine in radians of the given value
abs: numberReturn the absolute, or non-negative value, of a given value
log: numberReturns the logarithm of x in the given base. The default base is e (natural logarithm)
modf: number, numberReturn the integral and fractional parts of the given number
deg: numberConvert from radians to degrees
sqrt: numberReturn the square root of a given number. Only non-negative arguments are allowed

ceilโ€‹

Returns the smallest integer greater than or equal to the given value

local ret = math.ceil(number)
Parameters
NameTypeDescription
numbernumberThe number to be rounded up
Returns
TypeDescription
numberThe rounded up integer

tanโ€‹

Return the tangent value for a given value in radians

local ret = math.tan(value)
Parameters
NameTypeDescription
valuenumberAngle in radians
Returns
TypeDescription
numberThe tangent

minโ€‹

Return the minimum value from a variable length list of arguments

local ret = math.min(numbers...)
Parameters
NameTypeDescription
numbers...varargs of numberNumbers to get the smallest from
Returns
TypeDescription
numberThe smallest value

fmodโ€‹

Returns the remainder of the division of base by modulator that rounds the quotient towards zero

local ret = math.fmod(base, modulator)
Parameters
NameTypeDescription
basenumberThe base value
modulatornumberThe modulator
Returns
TypeDescription
numberThe remainder

expโ€‹

Returns e (the base of natural logarithms) raised to the power exponent

local ret = math.exp(exponent)
Parameters
NameTypeDescription
exponentnumberThe exponent for the function
Returns
TypeDescription
numbere raised to the given power

randomโ€‹

Generates pseudo-random numbers uniformly distributed

local ret = math.random(m, n)
Parameters
NameTypeDescription
mnumberIf m is the only parameter: upper limit. If n is also provided: lower limit. If provided, this must be an integer
nnumberUpper limit. If provided, this must be an integer
Returns
TypeDescription
numberA random float between 0 and 1 when called without parameters, otherwise a random integer in the given range

radโ€‹

Convert from degrees to radians

local ret = math.rad(degrees)
Parameters
NameTypeDescription
degreesnumberThe angle measured in degrees
Returns
TypeDescription
numberThe angle in radians

sinโ€‹

Return the sine value for a given value in radians

local ret = math.sin(number)
Parameters
NameTypeDescription
numbernumberAngle in radians
Returns
TypeDescription
numberThe sine

cosโ€‹

Return the cosine value for a given value in radians

local ret = math.cos(number)
Parameters
NameTypeDescription
numbernumberAngle in radians
Returns
TypeDescription
numberThe cosine

asinโ€‹

Return the inverse sine in radians of the given value

local ret = math.asin(normal)
Parameters
NameTypeDescription
normalnumberSine value in the range of -1 to 1
Returns
TypeDescription
numberThe angle in radians

randomseedโ€‹

Sets a seed for the pseudo-random generator: Equal seeds produce equal sequences of numbers

math.randomseed(seed)
Parameters
NameTypeDescription
seednumberThe new seed

floorโ€‹

Returns the largest integer less than or equal to the given value

local ret = math.floor(number)
Parameters
NameTypeDescription
numbernumberThe number to be rounded down
Returns
TypeDescription
numberThe rounded down integer

maxโ€‹

Return the maximum value from a variable length list of arguments

local ret = math.max(numbers...)
Parameters
NameTypeDescription
numbers...varargs of numberNumbers to get the largest from
Returns
TypeDescription
numberThe largest value

atanโ€‹

Returns the arc tangent in radians. Pass y and x to get the angle of the point (x, y), using the signs of both to find the quadrant

local ret = math.atan(normal)
Parameters
NameTypeDescription
normalnumberTangent value
Returns
TypeDescription
numberThe angle in radians

acosโ€‹

Return the inverse cosine in radians of the given value

local ret = math.acos(cos)
Parameters
NameTypeDescription
cosnumberCosine value in range of -1 to 1
Returns
TypeDescription
numberThe angle in radians

absโ€‹

Return the absolute, or non-negative value, of a given value

local ret = math.abs(x)
Parameters
NameTypeDescription
xnumberThe number to get the absolute value of
Returns
TypeDescription
numberThe absolute value

logโ€‹

Returns the logarithm of x in the given base. The default base is e (natural logarithm)

local ret = math.log(x, base)
Parameters
NameTypeDescription
xnumberThe value to get the base from exponent from
basenumberThe logarithmic base
Returns
TypeDescription
numberThe logarithm

modfโ€‹

Return the integral and fractional parts of the given number

local integral, fractional = math.modf(base)
Parameters
NameTypeDescription
basenumberThe base value
Returns
TypeDescription
numberThe integral part
numberThe fractional part

degโ€‹

Convert from radians to degrees

local ret = math.deg(radians)
Parameters
NameTypeDescription
radiansnumberValue to be converted to degrees
Returns
TypeDescription
numberThe angle in degrees

sqrtโ€‹

Return the square root of a given number. Only non-negative arguments are allowed

local ret = math.sqrt(value)
Parameters
NameTypeDescription
valuenumberValue to get the square root of
Returns
TypeDescription
numberThe square root