HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to LuaVariablesNumbers & MathStringsTablesConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Table
🔁 Condition & Loop
🧩 Function
Lua›Numbers & Math

Numbers & Math

Arithmetic (+ - * / % ^ //) and math library.

Simple enough for a 5-year-old to understand

Arithmetic

Example code
1
print(7 % 2)
Remainder: 1
CodeRemainder: 1
2
print(2 ^ 10)
Exponent: 1024.0 (float!)
CodeExponent: 1024.0 (float!)
3
print(7 // 2)
Integer division: 3
CodeInteger division: 3
Code
💡 Hint: ^ is the exponent operator but its result is a float (1024.0). Use // for integer.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the remainder of 23 divided by 4.

🎯 Expected output: 3

Loading editor…

About this topic

In Lua, numbers are represented by a single type. In older versions, all numbers were floating-point; Lua 5.3 introduced a distinction between integers and decimals, but both fall under the same number type and can be used together.

Arithmetic operators are familiar. The thing to watch out for is division: a single slash always gives a decimal result, while a double slash is used for integer division. There is a percent sign for the remainder and a caret for exponentiation.

An interesting behavior of Lua is that it automatically converts text that looks like numbers. When you try to do arithmetic with a string, Lua tries to convert it to a number; if successful, the operation works. This convenience also leads to hidden typos.

What you will learn

  • Arithmetic operators
  • The difference between integer and decimal division
  • Automatic type conversion
  • The math library
  • Conversion between numbers and strings

Who is this for?

  • Script writers who will perform calculations
  • Those who get unexpected values as a result of division
  • Those calculating in-game scores and coordinates

When to use?

Position, damage, and duration calculations in game scripts constantly require numerical operations; knowing the division behavior prevents incorrect results.

Values from the user come as strings; it is safe to explicitly convert them to numbers before factoring them into calculations.

Common mistakes

Wrong

print(7 / 2)
print(math.type(7 / 2))

In Lua 5.3 and later, a single slash always gives a decimal result: it writes 3.5 and the type becomes float. Using this value where an integer is expected causes problems. Integer division requires a double slash.

Correct

print(7 // 2)
print(math.type(7 // 2))

Wrong

local girdi = "abc"
print(girdi + 1)

Lua tries to convert text to a number, but "abc" cannot be converted to a number and the error "attempt to perform arithmetic on a string value" is thrown. The interesting thing is this: the same code works fine with the text "10". Therefore, the error only appears with certain inputs and is easily missed in tests.

Correct

local girdi = "abc"
local sayi = tonumber(girdi)
if sayi then print(sayi + 1) else print("not a number") end

Wrong

local a = 0.1
local b = 0.2
if a + b == 0.3 then print("equal") else print("not equal") end

Decimals cannot be stored exactly in binary; the sum is very close to 0.3 but not equal to it, and "not equal" is printed to the screen. This is not specific to Lua, but is the general behavior of floating-point arithmetic. Instead of comparing decimals directly, a difference threshold is used.

Correct

local a = 0.1
local b = 0.2
if math.abs((a + b) - 0.3) < 1e-9 then print("equal") else print("not equal") end

Frequently asked questions

How do I convert text to a number?

The tonumber function is used; if it cannot be converted, it returns nil, so the result must be checked. In the reverse direction, the tostring function converts a number to text, but the concatenation operator already does this automatically.

How is rounding done?

There is no built-in rounding function in Lua. math.floor is used to round down, and math.ceil is used to round up. To round to the nearest, adding a half to the number and rounding down is a common method.

How to generate a random number?

The math.random function is used; without arguments, it gives an integer between zero and one, and with two arguments, within the range you specify. In older Lua versions, if the initial seed was not set, it would produce the same sequence on every run; in newer versions, this happens automatically.

Summary

A single slash gives a decimal division, and a double slash gives an integer division.

Lua automatically converts text that looks like a number.

Text that cannot be converted produces a runtime error.

Decimal numbers should not be compared using direct equality.

Mathematical operations are in the math library.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code