Back

Activity 1.5: Doing the Math

divider

Activity 1.5

Doing the Math

Key Concepts

Six Operators

Division Keeps the Decimal

Order of Operations

The Operators

print(10 + 3)
print(10 - 3)
print(10 * 3)
print(10 / 3)
print(10 % 3)
print(2 ^ 3)

The Operators

print(10 + 3)
print(10 - 3)
print(10 * 3)
print(10 / 3)
print(10 % 3)
print(2 ^ 3)
Terminal window
13
7
30
3.3333333333333
1
8

Two You Have Not Seen

%
The remainder. 10 % 3 is 1, because 3 goes into 10 three times with 1 left over.
^
To the power of. 2 ^ 3 is 8.

% looks useless right now. It is how you will get the leftover slices, the seconds past the minute, and every second row of a grid.

Division Does Not Round

print(10 / 2)
print(10 / 4)
print(17 / 5)

Division Does Not Round

print(10 / 2)
print(10 / 4)
print(17 / 5)
Terminal window
5
2.5
3.4

You keep the decimal, always. When the answer happens to be whole, there is just nothing after the point to show.

When You Want a Whole Number, Ask

print(10 / 4)
print(math.floor(10 / 4))
Terminal window
2.5
2

math.floor throws away everything after the point. It does not round. 2.9 becomes 2.

Order of Operations

print(2 + 3 * 4)
print((2 + 3) * 4)

Order of Operations

print(2 + 3 * 4)
print((2 + 3) * 4)
Terminal window
14
20

Same rules as math class. *, / and % happen before + and -, and parentheses beat everything.

Which Is Why This Is Wrong

local a = 80
local b = 90
local c = 100
print("Average: " .. a + b + c / 3)

Which Is Why This Is Wrong

local a = 80
local b = 90
local c = 100
print("Average: " .. a + b + c / 3)
Terminal window
Average: 203.33333333333

It divided only the 100, then added the other two on.

Parentheses Say What You Meant

print("Average: " .. (a + b + c) / 3)
Terminal window
Average: 90

Parentheses Say What You Meant

print("Average: " .. (a + b + c) / 3)
Terminal window
Average: 90

No error, no warning, no crash. An average of 203 out of 100 is the only thing that tells you.

Use parentheses when you are not certain. Nobody has ever been marked down for extra ones.

Today's Objectives

  • Use all six arithmetic operators
  • Say what % gives you
  • Explain why division keeps the decimal
  • Fix a wrong answer with parentheses

Key Terms

Operator
A symbol that combines two values, like +.
Remainder
What is left over after dividing, given by %.
Precedence
Which operator happens first when there are several.
math.floor
Drops everything after the decimal point.

'F' → Fullscreen

divider

Build

Task 1: Run the Six

Copy template-console to 1-05-math and write this:

main.lua
print(10 + 3)
print(10 - 3)
print(10 * 3)
print(10 / 3)
print(10 % 3)
print(2 ^ 3)
Terminal
13
7
30
3.3333333333333
1
8

Copy all six answers onto your sheet. Then change the numbers and predict each answer before you run it. Keep going until you stop being surprised.

Task 2: The Average That Is Not

Add this to the bottom of the same file:

main.lua
local a = 80
local b = 90
local c = 100
print("Average: " .. a + b + c / 3)
Terminal
Average: 203.33333333333

Three test scores of 80, 90 and 100, and it reports an average of 203. Work out on paper what it actually calculated, write that on your sheet, then fix the line with parentheses and check you get 90.

Task 3: Seconds Into Minutes

Copy the template to 1-05-time. This one asks a question, so it needs tonumber from last session:

main.lua
io.write("How many seconds? ")
local total = tonumber(io.read())
local minutes = math.floor(total / 60)
local seconds = total % 60
print(total .. " seconds is " .. minutes .. " minutes and " .. seconds .. " seconds.")
Terminal
How many seconds? 500 [Enter]
500 seconds is 8 minutes and 20 seconds.

Test it with 500, then 60, then 59, then 3600. Write down what it says for each.

Those two lines are the whole idea of this session. math.floor(total / 60) is how many whole minutes fit, and total % 60 is what did not fit. Together they account for every second.

Challenge (Optional): Break It on Purpose

Divide by zero:

main.lua
print(10 / 0)
Terminal
inf

No error. inf is short for infinity, and it is a value like any other — you can store it, print it, and add to it. Try print(10 / 0 + 5) and write down what you get.

Every other language you meet will do something different here. Several of them crash.

Challenge (Optional): Change for a Dollar

Write a program that asks for a number of cents and answers with how many quarters, dimes, nickels and pennies to hand back — largest coins first. 117 cents is 4 quarters, 1 dime, 1 nickel and 2 pennies.

You have everything you need: math.floor for how many fit and % for what is left.

divider

Same Idea, Exam Notation

The arithmetic on the exam is the arithmetic you just did. The operators are the same and so is the order of operations.

Lua
local total = tonumber(io.read())
local minutes = math.floor(total / 60)
local seconds = total % 60
print(minutes)
print(seconds)
Exam Notation
total ← INPUT()
seconds ← total MOD 60
minutes ← (total - seconds) / 60
DISPLAY(minutes)
DISPLAY(seconds)
LuaExam notation
Add, subtract, multiply, divide+ - * /+ - * /
Remainder17 % 517 MOD 5
Ask the userio.read()INPUT()
Show a valueprint(x)DISPLAY(x)

Three differences worth knowing

  • There is no math.floor on the exam, and there is no rounding operator at all. To drop the decimal you subtract the remainder first, then divide — which is what the right hand column above does.
  • There is no ^ on the exam. A power has to be written as repeated multiplication.
  • DISPLAY puts a space after what it shows. Two of them in a row give you space-separated output, not two things jammed together. This matters on trace questions.
17 / 5 is 3.4 on the exam, exactly as it is in Lua. Division never truncates in either place. If you have used a language where it does, forget that here.
divider

Checkpoint

  • All six operator results are written on your sheet
  • You can say what the broken average line actually calculated
  • 1-05-time is correct for 500, 60, 59 and 3600
  • You can point at the line that gives whole minutes and the line that gives leftover seconds
divider

Reflection

Answer the following questions before submitting your work.

  1. The broken average printed 203.33333333333 instead of 90. Explain, step by step, what the computer did with a + b + c / 3 and why it is not a bug in Lua.
  2. math.floor(total / 60) and total % 60 answer two different questions about the same number. Say what each one answers, in words, without using the word divide.
  3. Dividing by zero printed inf and did not stop the program. Describe a situation where that is worse than crashing.
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete