0Pricing
Lua Academy · Lesson

The math Library

floor, ceil, random, and more.

The math Library is a free Lua Academy lesson on CoddyKit — lesson 3 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.

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) is 3
  • math.ceil(3.2) is 4
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.

Frequently asked questions

Is the “The math Library” lesson free?

Yes — the full text of “The math Library” 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 “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; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Integers and Floats
  2. Arithmetic Operators
  3. The math Library
  4. Number Formatting
← Back to Lua Academy