Lua Math Library
The math table holds Lua's mathematical functions and constants. Everything is accessed as math.name.
print(math.pi)
print(math.sqrt(144), math.abs(-8))
print(math.max(4, 19, 7), math.min(4, 19, 7))3.1415926535898
12.0 8
19 4
Function overview
| Function | Purpose | Example → result |
|---|---|---|
math.abs(x) |
absolute value | math.abs(-3) → 3 |
math.floor(x) / math.ceil(x) |
round down / up (returns integer) | math.floor(2.7) → 2 |
math.max(...) / math.min(...) |
largest / smallest argument | math.max(1, 5) → 5 |
math.sqrt(x) |
square root | math.sqrt(2) → 1.4142135623731 |
math.exp(x), math.log(x [, base]) |
eˣ and logarithm | math.log(8, 2) → 3.0 |
math.sin, math.cos, math.tan |
trigonometry (radians) | math.cos(0) → 1.0 |
math.asin, math.acos, math.atan(y [, x]) |
inverse trig | |
math.rad(deg) / math.deg(rad) |
degree ↔ radian | math.rad(180) → 3.1415926535898 |
math.fmod(a, b) |
remainder rounding towards zero | math.fmod(-7, 2) → -1 |
math.modf(x) |
integral and fractional parts | math.modf(3.25) → 3 0.25 |
math.random, math.randomseed |
pseudo-random numbers | see below |
math.type, math.tointeger, math.ult |
integer helpers | |
math.pi, math.huge |
π, infinity | |
math.maxinteger, math.mininteger |
integer limits |
Rounding
Lua has no math.round. Round to the nearest integer with math.floor(x + 0.5), or to n decimals with string.format:
local function round(x)
return math.floor(x + 0.5)
end
print(round(2.4), round(2.5), round(-2.5))
print(string.format("%.2f", 3.14159))
print(math.floor(-2.5), math.ceil(-2.5))
print(math.modf(-3.75))2 3 -2
3.14
-3 -2
-3 -0.75
Trigonometry and logarithms
Angles are in radians. Convert with math.rad:
local angle = math.rad(30)
print(string.format("sin(30°) = %.3f", math.sin(angle)))
print(string.format("cos(60°) = %.3f", math.cos(math.rad(60))))
-- distance between two points
local x1, y1, x2, y2 = 1, 2, 4, 6
print(math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2))
print(math.log(100, 10), math.log(1024, 2), math.exp(0))sin(30°) = 0.500
cos(60°) = 0.500
5.0
2.0 10.0 1.0
math.atan(y, x) (with two arguments) gives the angle of the point (x, y) in all four quadrants, which game code uses to point a character towards a target.
Random numbers
math.random produces pseudo-random numbers. Lua 5.4 seeds the generator randomly at start-up, so every run is different. For reproducible results (tests, replays, procedurally generated levels from a seed), call math.randomseed with a fixed number first:
| Call | Result |
|---|---|
math.random() |
float in [0, 1) |
math.random(n) |
integer in [1, n] |
math.random(m, n) |
integer in [m, n] |
math.randomseed(42) -- a fixed seed (42) makes the sequence repeatable
local rolls = {}
for i = 1, 5 do
rolls[i] = math.random(6)
end
print("dice:", table.concat(rolls, " "))
print(string.format("%.4f", math.random()))dice: 6 2 4 6 6
0.0096
Run it again: with the same seed you get the same sequence every time. Remove the randomseed line on your own machine and the numbers change on each run.
Clamping and other helpers
A clamp (keep a value inside a range) is a one-liner with min and max:
local function clamp(x, lo, hi)
return math.max(lo, math.min(hi, x))
end
print(clamp(150, 0, 100), clamp(-5, 0, 100), clamp(42, 0, 100))
print(math.fmod(7, 3), math.fmod(-7, 3), -7 % 3)100 0 42
1 -1 2
Note the difference between math.fmod (sign of the dividend, like C) and % (sign of the divisor).
Test yourself
How do you get a random integer from 1 to 10?
math.random()math.random(10)math.rand(1, 10)random(10)
Which function makes math.random repeat the same sequence?
math.seedmath.randomseedmath.resetos.seed
Lua has no math.round. Which expression rounds a positive x to the nearest integer?
math.floor(x + 0.5)math.ceil(x - 1)x // 0.5math.int(x)