Ir para o conteúdo principal
Version: latest - a1.156.0 ⚖️

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​

ReturnsNameDescription
numberceilReturns the smallest integer greater than or equal to the given value
numbertanReturn the tangent value for a given value in radians.
numberminReturn the minimum value from a variable length list of arguments
numberfmodReturns the remainder of the division of base by modulator that rounds the quotient towards zero
numberexpReturns e (the base of natural logarithms) raised to the power exponent
numberrandomGenerates pseudo-random numbers uniformly distributed
numberradConvert from degrees to radians
numbersinReturn the sine value for a given value in radians.
numbercosReturn the cosine value for a given value in radians.
numberasinReturn the inverse sine in radians of the given value.
randomseedSets a seed for the pseudo-random generator: Equal seeds produce equal sequences of numbers
numberfloorReturns the largest integer less than or equal to the given value
numbermaxReturn the maximum value from a variable length list of arguments
numberatanReturns 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
numberacosReturn the inverse cosine in radians of the given value.
numberabsReturn the absolute, or non-negative value, of a given value
numberlogReturns the logarithm of x in the given base. The default base is e (natural logarithm)
number, numbermodfReturn the integral and fractional parts of the given number
numberdegConvert from radians to degrees
numbersqrtReturn 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

TypeParameterDefaultDescription
numbernumber Required parameter The 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

TypeParameterDefaultDescription
numbervalue Required parameter Angle in radians

Returns

TypeDescription
numberThe tangent

min​

Return the minimum value from a variable length list of arguments
local ret = math.min(numbers...)

Parameters

TypeParameterDefaultDescription
varargs of numbernumbers... Required parameter Numbers 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

TypeParameterDefaultDescription
numberbase Required parameter The base value.
numbermodulator Required parameter The modulator.

Returns

TypeDescription
numberThe remainder

exp​

Returns e (the base of natural logarithms) raised to the power exponent
local ret = math.exp(exponent)

Parameters

TypeParameterDefaultDescription
numberexponent Required parameter The 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

TypeParameterDefaultDescription
numberm Required parameter If m is the only parameter: upper limit. If n is also provided: lower limit. If provided, this must be an integer.
numbern Required parameter Upper 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

TypeParameterDefaultDescription
numberdegrees Required parameter The 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

TypeParameterDefaultDescription
numbernumber Required parameter Angle in radians

Returns

TypeDescription
numberThe sine

cos​

Return the cosine value for a given value in radians.
local ret = math.cos(number)

Parameters

TypeParameterDefaultDescription
numbernumber Required parameter Angle in radians

Returns

TypeDescription
numberThe cosine

asin​

Return the inverse sine in radians of the given value.
local ret = math.asin(normal)

Parameters

TypeParameterDefaultDescription
numbernormal Required parameter Sine 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

TypeParameterDefaultDescription
numberseed Required parameter The new seed

floor​

Returns the largest integer less than or equal to the given value
local ret = math.floor(number)

Parameters

TypeParameterDefaultDescription
numbernumber Required parameter The 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

TypeParameterDefaultDescription
varargs of numbernumbers... Required parameter Numbers 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

TypeParameterDefaultDescription
numbernormal Required parameter Tangent value.

Returns

TypeDescription
numberThe angle in radians

acos​

Return the inverse cosine in radians of the given value.
local ret = math.acos(cos)

Parameters

TypeParameterDefaultDescription
numbercos Required parameter Cosine 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

TypeParameterDefaultDescription
numberx Required parameter The 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

TypeParameterDefaultDescription
numberx Required parameter The value to get the base from exponent from.
numberbase Required parameter The logarithmic base.

Returns

TypeDescription
numberThe logarithm

modf​

Return the integral and fractional parts of the given number
local ret_1, ret_2 = math.modf(base)

Parameters

TypeParameterDefaultDescription
numberbase Required parameter The base value.

Returns

TypeDescription
numberThe integral part
numberThe fractional part

deg​

Convert from radians to degrees
local ret = math.deg(radians)

Parameters

TypeParameterDefaultDescription
numberradians Required parameter Value 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

TypeParameterDefaultDescription
numbervalue Required parameter Value to get the square root of.

Returns

TypeDescription
numberThe square root