The math Library
floor, ceil, random, and more.
The math Library is a free Lua Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.
Meet the math Library
Lua ships with a built-in math library full of useful functions and constants for working with numbers.
You call its functions with the math. prefix, like math.sqrt(9). No setup or import needed.
print(math.sqrt(9))Absolute Value
math.abs() returns the absolute value of a number, removing any negative sign.
So math.abs(-5) gives 5, and math.abs(5) also gives 5.
print(math.abs(-5))
print(math.abs(3))Rounding Down and Up
Use math.floor() to round down to the nearest whole number, and math.ceil() to round up.
math.floor(3.7)is3math.ceil(3.2)is4
print(math.floor(3.7))
print(math.ceil(3.2))Maximum and Minimum
math.max() returns the largest of its arguments and math.min() returns the smallest.
You can pass as many numbers as you like.
print(math.max(3, 9, 1))
print(math.min(3, 9, 1))Square Root
math.sqrt() computes the square root of a number. The result is a float.
For other roots, use the ^ operator, such as 27 ^ (1/3) for a cube root.
print(math.sqrt(16))
print(math.sqrt(2))Powers
You can raise numbers to a power with the ^ operator, which is the usual way in modern Lua.
For example 2 ^ 10 gives 1024.0. The result is always a float.
print(2 ^ 10)
print(5 ^ 2)The Constant pi
The library provides the constant math.pi, the value of pi (about 3.14159).
It is useful for circle and trigonometry math, like finding a circle's area.
local r = 2
print(math.pi * r ^ 2)Infinity and huge
math.huge represents positive infinity, the largest possible float value.
It is often used as a starting value when searching for a minimum, since any real number is smaller than it.
print(math.huge)
print(-math.huge)Random Numbers
math.random() returns a random number. With no arguments it gives a float between 0 and 1.
With one integer argument n, it returns an integer from 1 to n. With two, it returns a value in that range.
print(math.random(1, 6))Seeding Randomness
Use math.randomseed() to set the starting point for the random generator. Seeding with the current time gives different sequences each run.
In Lua 5.4, the generator is auto-seeded, but explicit seeding makes results reproducible when you want them.
math.randomseed(42)
print(math.random(1, 100))Trigonometry
The library includes trig functions like math.sin(), math.cos(), and math.tan(). They expect angles in radians, not degrees.
Multiply degrees by math.pi / 180 to convert.
print(math.sin(math.pi / 2))
print(math.cos(0))Quick Check
Check your knowledge of the math library.
Recap
You explored the math library: abs, floor, ceil, max, min, sqrt, and trig functions.
You also met the constants math.pi and math.huge, plus math.random and math.randomseed for randomness.
Next, you will learn how to format numbers for clean output.
Learn Lua with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 40
- Lessons
- 159
Frequently Asked Questions
Is the “The math Library” lesson free?
Yes — any 3 lessons of the Lua Academy learning path, including “The math Library”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.
What will I learn in “The math Library”?
floor, ceil, random, and more. 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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “The math Library” 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
- Integers and Floats
- Arithmetic Operators
- The math Library
- Number Formatting