math Library Functions
Use math.floor, ceil, sqrt, sin, cos, random, huge, and pi.
math Library Functions is a free Lua Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The math Module
The math module provides mathematical constants and functions: math.pi, math.huge, math.maxinteger, and dozens of functions.
Rounding Functions
math.floor rounds down, math.ceil rounds up, math.tointeger converts a float to integer or returns nil.
print(math.floor(3.7)) -- 3
print(math.ceil(3.2)) -- 4
print(math.tointeger(4.0)) -- 4Trigonometry
Functions take radians. Use math.pi for degree-to-radian conversion.
local deg = 45
local rad = deg * math.pi / 180
print(math.sin(rad)) -- ~0.707
print(math.cos(rad)) -- ~0.707
print(math.tan(rad)) -- ~1.0math.sqrt and math.abs
math.sqrt(x) returns the square root; math.abs(x) returns the absolute value.
print(math.sqrt(144)) -- 12.0
print(math.abs(-42)) -- 42math.exp and math.log
Exponential and logarithm functions. math.log accepts an optional base argument.
print(math.exp(1)) -- 2.718...
print(math.log(math.exp(1))) -- 1.0
print(math.log(100, 10)) -- 2.0math.min and math.max
Return the minimum or maximum of a list of numbers. Accept multiple arguments.
print(math.min(3, 1, 4, 1, 5)) -- 1
print(math.max(3, 1, 4, 1, 5)) -- 5
print(math.min(table.unpack({7,2,9}))) -- 2math.random and math.randomseed
Generate pseudorandom numbers. Seed with os.time() for non-repeating sequences.
math.randomseed(os.time())
print(math.random()) -- float in [0,1)
print(math.random(6)) -- integer in [1,6]
print(math.random(10, 20)) -- integer in [10,20]math.modf
math.modf(x) returns the integral and fractional parts of x as two separate values.
local int, frac = math.modf(3.75)
print(int, frac) -- 3.0 0.75math.huge and Infinity
math.huge is positive infinity. Useful as an initial value for minimum-finding loops.
local min = math.huge
for _, v in ipairs({5, 2, 8, 1}) do
if v < min then min = v end
end
print(min) -- 1Integer Arithmetic (Lua 5.3+)
Lua 5.3 introduced true integers. Use math.type(x) to distinguish integer from float.
print(math.type(1)) -- integer
print(math.type(1.0)) -- float
print(math.maxinteger) -- 9223372036854775807math.fmod
math.fmod(x, y) returns the remainder of x/y with the same sign as x, unlike the % operator.
print(math.fmod(-7, 3)) -- -1
print(-7 % 3) -- 2 (always non-negative in Lua)math Library Question
Which function returns both the integer and fractional parts of a number?
Recap: math Library
The math library covers rounding, trigonometry, exponentials, random numbers, min/max, and integer arithmetic. Always seed math.random with os.time() for realistic randomness.
Frequently asked questions
Is the “math Library Functions” lesson free?
Yes — the full text of “math Library Functions” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “math Library Functions”?
Use math.floor, ceil, sqrt, sin, cos, random, huge, and pi. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “math Library Functions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- math Library Functions
- table Library Functions
- os and io Libraries
- The debug Library