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โ
| Value | Name |
|---|---|
3.141592653589793 | math.pi |
2^1024 | math.huge |
๐ฟ Static Functionsโ
ceil: number | Returns the smallest integer greater than or equal to the given value | |
tan: number | Return the tangent value for a given value in radians | |
min: number | Return the minimum value from a variable length list of arguments | |
fmod: number | Returns the remainder of the division of base by modulator that rounds the quotient towards zero | |
exp: number | Returns e (the base of natural logarithms) raised to the power exponent | |
random: number | Generates pseudo-random numbers uniformly distributed | |
rad: number | Convert from degrees to radians | |
sin: number | Return the sine value for a given value in radians | |
cos: number | Return the cosine value for a given value in radians | |
asin: number | Return the inverse sine in radians of the given value | |
randomseed | Sets a seed for the pseudo-random generator: Equal seeds produce equal sequences of numbers | |
floor: number | Returns the largest integer less than or equal to the given value | |
max: number | Return the maximum value from a variable length list of arguments | |
atan: number | 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 | |
acos: number | Return the inverse cosine in radians of the given value | |
abs: number | Return the absolute, or non-negative value, of a given value | |
log: number | Returns the logarithm of x in the given base. The default base is e (natural logarithm) | |
modf: number, number | Return the integral and fractional parts of the given number | |
deg: number | Convert from radians to degrees | |
sqrt: number | Return 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
| Name | Type | Description |
|---|---|---|
number | number | The number to be rounded up |
Returns
| Type | Description |
|---|---|
| number | The rounded up integer |

tanโ
Return the tangent value for a given value in radians
local ret = math.tan(value)
Parameters
| Name | Type | Description |
|---|---|---|
value | number | Angle in radians |
Returns
| Type | Description |
|---|---|
| number | The tangent |

minโ
Return the minimum value from a variable length list of arguments
local ret = math.min(numbers...)
Returns
| Type | Description |
|---|---|
| number | The 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)
Returns
| Type | Description |
|---|---|
| number | The remainder |

expโ
Returns e (the base of natural logarithms) raised to the power exponent
local ret = math.exp(exponent)
Parameters
| Name | Type | Description |
|---|---|---|
exponent | number | The exponent for the function |
Returns
| Type | Description |
|---|---|
| number | e raised to the given power |

randomโ
Generates pseudo-random numbers uniformly distributed
local ret = math.random(m, n)
Parameters
| Name | Type | Description |
|---|---|---|
m | number | If m is the only parameter: upper limit. If n is also provided: lower limit. If provided, this must be an integer |
n | number | Upper limit. If provided, this must be an integer |
Returns
| Type | Description |
|---|---|
| number | A 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
| Name | Type | Description |
|---|---|---|
degrees | number | The angle measured in degrees |
Returns
| Type | Description |
|---|---|
| number | The angle in radians |

sinโ
Return the sine value for a given value in radians
local ret = math.sin(number)
Parameters
| Name | Type | Description |
|---|---|---|
number | number | Angle in radians |
Returns
| Type | Description |
|---|---|
| number | The sine |

cosโ
Return the cosine value for a given value in radians
local ret = math.cos(number)
Parameters
| Name | Type | Description |
|---|---|---|
number | number | Angle in radians |
Returns
| Type | Description |
|---|---|
| number | The cosine |

asinโ
Return the inverse sine in radians of the given value
local ret = math.asin(normal)
Parameters
| Name | Type | Description |
|---|---|---|
normal | number | Sine value in the range of -1 to 1 |
Returns
| Type | Description |
|---|---|
| number | The angle in radians |

randomseedโ
Sets a seed for the pseudo-random generator: Equal seeds produce equal sequences of numbers
math.randomseed(seed)
Parameters
| Name | Type | Description |
|---|---|---|
seed | number | The new seed |

floorโ
Returns the largest integer less than or equal to the given value
local ret = math.floor(number)
Parameters
| Name | Type | Description |
|---|---|---|
number | number | The number to be rounded down |
Returns
| Type | Description |
|---|---|
| number | The rounded down integer |

maxโ
Return the maximum value from a variable length list of arguments
local ret = math.max(numbers...)
Returns
| Type | Description |
|---|---|
| number | The 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
| Name | Type | Description |
|---|---|---|
normal | number | Tangent value |
Returns
| Type | Description |
|---|---|
| number | The angle in radians |

acosโ
Return the inverse cosine in radians of the given value
local ret = math.acos(cos)
Parameters
| Name | Type | Description |
|---|---|---|
cos | number | Cosine value in range of -1 to 1 |
Returns
| Type | Description |
|---|---|
| number | The angle in radians |

absโ
Return the absolute, or non-negative value, of a given value
local ret = math.abs(x)
Parameters
| Name | Type | Description |
|---|---|---|
x | number | The number to get the absolute value of |
Returns
| Type | Description |
|---|---|
| number | The 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
| Name | Type | Description |
|---|---|---|
x | number | The value to get the base from exponent from |
base | number | The logarithmic base |
Returns
| Type | Description |
|---|---|
| number | The logarithm |

modfโ
Return the integral and fractional parts of the given number
local integral, fractional = math.modf(base)
Parameters
| Name | Type | Description |
|---|---|---|
base | number | The base value |