Lua Academy · Lesson

math Library Functions

Use math.floor, ceil, sqrt, sin, cos, random, huge, and pi.

Lesson 1 of 413 steps

math Library Functions is a free Lua Academy lesson on CoddyKit. This is lesson 1 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.

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))    -- 4

Trigonometry

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.0

math.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))   -- 42

math.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.0

math.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})))  -- 2

math.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.75

math.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)  -- 1

Integer 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) -- 9223372036854775807

math.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.

Free to start

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 “math Library Functions” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “math Library Functions”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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

  1. math Library Functions
  2. table Library Functions
  3. os and io Libraries
  4. The debug Library
← Back to Lua Academy