The arithmetic is the part of Lua with no surprises in it. Five operators, and they do what you expect.
print(7 + 2)print(7 - 2)print(7 * 2)print(7 / 2)print(7 % 2)95143.51Note that 7 / 2 gives 3.5, not 3. Division in Lua never throws away the fraction. If you want a whole number you have to ask for one, which is what math.floor is for.
This course runs on plain Lua now, and on LÖVE in Unit 2. Those are not quite the same interpreter, and here is the only difference you will actually see:
print(10 / 2)print(2 ^ 8)5.0256.0print(10 / 2)print(2 ^ 8)5256Same number, different spelling. Lua 5.4 tracks whether a number is a whole number or a decimal and prints 5.0 to tell you a division happened. Older Lua, which is what LÖVE runs, has only one kind of number and prints 5. Nothing is wrong with either. Do not be alarmed when the same code prints differently in Unit 2.
If you ever need the display to match exactly, run the value through math.floor or string.format and both will agree.
Anything beyond the five operators lives in math. These six cover almost everything you will need in Unit 2.
print(math.floor(7 / 2))print(math.ceil(3.2))print(math.max(3, 9, 5))print(math.min(3, 9, 5))print(math.abs(-4))print(math.sqrt(2))349341.4142135623731math.max and math.min take as many numbers as you want to give them. In a game they are how you stop a value escaping — health that cannot go below zero is math.max(0, health).
Lua will quietly convert a numeric string when you do arithmetic on it. This is the reverse of the JavaScript behavior you may have been burned by, and it is worth seeing side by side with ...
print("42" + 1)print("42" .. 1)print(tonumber("42") + 1)print(tonumber("hello"))4342143nilDo not rely on that first line. Convert on purpose with tonumber, which hands back nil when the text is not a number rather than crashing. That nil is your chance to notice bad input before it spreads.
math.random(1, 6) gives a whole number from 1 to 6, and both ends are included.
math.randomseed(os.time())
print(math.random(1, 6))print(math.random(1, 6))41That first line matters more than it looks. Without math.randomseed(os.time()), LÖVE hands you the exact same "random" numbers every single time you run the program. Lua 5.4 seeds itself and hides the problem; Unit 2 will not. Seed once, at the top of the file, and it is correct everywhere.
Start a fresh main.lua. You are building a jump computer: how much fuel a trip needs, and how many tanks that means loading.
-- main.lua - Skynest jump computer
local distance = 47local fuelPerUnit = 2.5
local totalFuel = distance * fuelPerUnit
print("Distance:", distance)print("Fuel needed:", totalFuel)Distance: 47Fuel needed: 117.5%.-- main.lua - Skynest jump computer
local distance = 47local fuelPerUnit = 2.5local tankSize = 40
local totalFuel = distance * fuelPerUnitlocal tanks = totalFuel / tankSize
print("Distance:", distance)print("Fuel needed:", totalFuel)print("Tanks to load:", math.ceil(tanks))print("Full tanks:", math.floor(tanks))print("Left over:", totalFuel % tankSize)Distance: 47Fuel needed: 117.5Tanks to load: 3Full tanks: 2Left over: 37.5-- main.lua - Skynest jump computer
math.randomseed(os.time())
local distance = 47local fuelPerUnit = 2.5local tankSize = 40
local totalFuel = distance * fuelPerUnitlocal tanks = totalFuel / tankSize
print("Distance:", distance)print("Fuel needed:", totalFuel)print("Tanks to load:", math.ceil(tanks))print("Full tanks:", math.floor(tanks))print("Left over:", totalFuel % tankSize)
local drift = math.random(-5, 5)print("Sensor drift:", drift)print("Corrected distance:", distance + drift)string.format and the %.1f placeholder.if.math.random(2, 12) is not the same thing.The first five lines should match exactly. The last two will differ every run — that is the point of them.
Distance: 47Fuel needed: 117.5Tanks to load: 3Full tanks: 2Left over: 37.5Sensor drift: -3Corrected distance: 44That is the whole of Lua's data toolkit except one thing. Numbers, strings, booleans, and nil are four of the five types you will ever use. The fifth is the table, it does the work of arrays, dictionaries, objects, and modules all at once, and it is what the rest of this unit is about.