Learning Goals 3 min
By the end of this lesson you will be able to:
- Find the compound [abs v] of () block in the Operators palette and open its dropdown to see all fourteen options.
- Explain in your own words what abs, floor, ceiling, and sqrt each do to a number.
- Use [abs v] of () wrapped around a subtraction to measure a distance without a minus sign — for example, "how far is the cat from x = 0?".
Warm-Up — minus signs are annoying 7 min
Cat is at x: -120. Target line is at x: 80. Quick: how far apart are they, in pixels?
Reveal the answer
200 pixels. You probably did it in your head: "from -120 to 0 is 120, plus 0 to 80 is 80, total 200." Easy for a human. But what about Scratch?
If a script says ((x position) - (80)) when the cat is at -120, the answer is -120 - 80 = -200. Negative! Distance can never really be negative — a cat can't be "minus 200 pixels away" from a wall. The minus sign is a leftover from which direction, not how far.
Second puzzle. You divide 17 by 5. You want the answer "How many full groups of 5 fit in 17?" — i.e. 3, not 3.4. Which of these maths words best describes throwing away the decimal part and keeping only the whole part below?
- Round
- Floor
- Ceiling
Reveal the answer
Floor. Floor always pushes a number down to the next whole number. [floor v] of (3.9) still reports 3. Round would give 4; ceiling would also give 4. Floor is "drop the decimals, period".
New Concept — the maths dropdown 15 min
In the Operators palette there's a block that looks like nothing special:
([abs v] of (9))
Click the little ▾ next to the word "abs". A dropdown opens with fourteen options:
abs · floor · ceiling · sqrt · sin · cos · tan · asin · acos · atan · ln · log · e ^ · 10 ^
That's one block, fourteen different jobs. Most of them (sin, cos, atan, ln, log…) are for older learners doing trigonometry or science maths. Today we're focusing on the four most useful for ages 9–13. The rest are there waiting when you need them.
abs — always positive
"Abs" is short for absolute value. It throws away the minus sign.
- [abs v] of (9) reports
9. - [abs v] of (-9) also reports
9. - [abs v] of (0) reports
0.
The classic use case is distance. Subtract two positions and you might get -200 or +200 depending on which is bigger. Wrap the subtraction in abs and you always get the positive distance.
set [gap v] to ([abs v] of ((x position) - (target-x)))
floor — round down, always
Floor takes any decimal and drops it to the whole number below. Even 3.99 floors to 3. Negative numbers floor further from zero: [floor v] of (-2.1) reports -3, not -2.
set [groups v] to ([floor v] of ((sweets) / (5)))
sweets into bags of 5, how many full bags do I get?" 17 sweets → 3 full bags (with 2 left over).ceiling — round up, always
Ceiling is floor's opposite. It pushes any decimal up to the whole number above. [ceiling v] of (3.1) reports 4; [ceiling v] of (3.0) reports 3 (already whole).
Use ceiling when you'd rather have one too many than one too few — for example, "how many seats do I need to book if 23 kids each take one and chairs come in packs of 6?". 23 ÷ 6 = 3.83. Floor would book 3 packs (only 18 seats — not enough!). Ceiling books 4 packs (24 seats — everyone fits).
sqrt — square root
The square root of a number is "what number times itself gives this?". [sqrt v] of (9) reports 3 (because 3 × 3 = 9). [sqrt v] of (100) reports 10. [sqrt v] of (2) reports the messy decimal 1.41421….
In Scratch games, sqrt mostly shows up in true diagonal distance: the Pythagorean theorem. You'll meet that fully in a much later lesson — for now, just know the block exists and what it reports.
Worked Example — shipping the Lucky Number Machine 12 min
Final lesson of the arc. We add abs to measure the gap between the total and the target, floor to polish the final score, then we wrap everything up and save the finished game. Eight steps.
Step 1 — Open the Lucky Number Machine project
Open the project from Lesson 4. The cat has random dice rolls, the total, the even/odd bonus, and a rounded average.
Step 2 — Make the gap variable
Make a new variable gap. This will hold the distance between the total and the target.
Step 3 — Compute the gap with abs
After the bonus checks, add:
set [gap v] to ([abs v] of ((total) - (target)))
Step 4 — Convert gap to a closeness score
The closer the total is to the target, the smaller the gap. We want a bigger score for a smaller gap. Use subtraction: finalScore = (12 − gap) — 12 is the maximum gap (6 + 6 minus 2). Set it with floor to keep it clean:
set [finalScore v] to ([floor v] of ((12) - (gap)))
Step 5 — Announce the result
say (join [Gap: ] (gap)) for (2) seconds
say (join [Final score: ] (finalScore)) for (2) seconds
Step 6 — Add a win message
If the gap is 0 (exact match!), say something special:
if <(gap) = (0)> then
say [PERFECT! You hit the target!] for (3) seconds
end
Step 7 — Full assembled final script
when flag clicked
set [score v] to (0)
set [die1 v] to (pick random (1) to (6))
set [die2 v] to (pick random (1) to (6))
set [total v] to ((die1) + (die2))
set [score v] to (total)
if <((total) mod (2)) = (0)> then
change [score v] by (3)
say [Even bonus! +3] for (1) seconds
end
set [gap v] to ([abs v] of ((total) - (target)))
set [finalScore v] to ([floor v] of ((12) - (gap)))
say (join [Gap: ] (gap)) for (2) seconds
say (join [Final score: ] (finalScore)) for (2) seconds
if <(gap) = (0)> then
say [PERFECT! You hit the target!] for (3) seconds
end
Step 8 — Play it ten times and save
Click the flag ten times. Note your best score. Note whether you ever hit the target exactly. Save the project as Lucky Number Machine.sb3. The arc is complete.
What you just built: a full playable game — two random dice, maths, an even/odd bonus, abs measuring the gap to the target, and a polished score. Every maths block from Lessons 1–5 contributed one piece. That's the arc model: five small lessons, one real project.
Try It Yourself — three drills 15 min
Goal: Show the cat's floor-ed x-position in a speech bubble forever. Drag the cat around — the bubble should always show a clean whole number that drops decimals (not rounds them).
when flag clicked
forever
say ([floor v] of (x position))
end
Think: Try swapping floor for ceiling via the dropdown — same block, different behaviour. That dropdown is the whole point of today's lesson.
Goal: Make a "seat-pack planner". A variable kids set to 23. A variable packs that always equals just enough packs of 6 seats. Use ceiling. Click the flag — the watcher should show 4. Change kids to 24 in the watcher and click the flag again — still 4. Try 25 — should jump to 5.
when flag clicked
set [kids v] to (23)
set [packs v] to ([ceiling v] of ((kids) / (6)))
say (join [Book ] (join (packs) [ packs])) for (3) seconds
Think: Why ceiling and not round? Because rounding 23 ÷ 6 = 3.83 gives 4, fine. But rounding 25 ÷ 6 = 4.16 gives 4 — and 4 packs is only 24 seats! Ceiling never under-books.
Goal: Use sqrt to compute a true diagonal distance. Two variables, dx and dy, set to the differences in x and y between the cat and the mouse-pointer. Then dist = sqrt of (dx × dx + dy × dy). Show it in a speech bubble forever, rounded.
when flag clicked
forever
set [dx v] to ((x position) - (mouse x))
set [dy v] to ((y position) - (mouse y))
set [dist v] to ([sqrt v] of (((dx) * (dx)) + ((dy) * (dy))))
say (round (dist))
end
Think: This is the Pythagorean theorem. You'll see it again. For now, just notice: sqrt sits at the top, and the abs wrap isn't needed because dx × dx and dy × dy are always positive on their own.
Mini-Challenge — Faris's one-sided dragon 5 min
"It only attacks on the right!"
Faris is making a game where a dragon (Sprite2) should breathe fire whenever the player (this sprite) gets within 80 pixels of it. He writes this:
when flag clicked
forever
set [gap v] to ((x position) - (mouse x))
if <(gap) < (80)> then
say [Roar!] for (0.5) seconds
end
end
What's wrong, and what's the smallest fix?
Reveal one valid solution
Faris is comparing a raw subtraction to 80. When the player is at x: 0 and the mouse at x: -300, 0 - (-300) = 300 — clearly far, no roar. Good. But when the player is at x: 0 and the mouse at x: 300, 0 - 300 = -300 — and -300 < 80 is true! So the dragon roars whenever the mouse is far on the right side, because negative numbers are always less than 80.
The fix is to wrap the subtraction in abs, so the gap is always positive and the comparison means what you think it means:
when flag clicked
forever
set [gap v] to ([abs v] of ((x position) - (mouse x)))
if <(gap) < (80)> then
say [Roar!] for (0.5) seconds
end
end
Lesson: any time you subtract two positions and compare to a "how close" number, you almost always want abs around the subtraction. Without it, one side of the world behaves correctly and the other side is permanently inside the danger zone.
Recap 3 min
You met the compound maths block — one block, fourteen functions, all hidden behind a dropdown. Four of them earn their keep at your age: abs strips the minus sign so distances are always positive; floor drops a decimal down to the whole number below; ceiling pushes a decimal up to the whole number above; sqrt reports the square root, which we'll use for diagonal distance later. You used abs and floor to finish the Lucky Number Machine — and then you shipped it.
- Absolute value (abs)
- The size of a number with no minus sign. [abs v] of (-7) reports
7; [abs v] of (7) also reports7. - Floor
- Round down to the nearest whole number. [floor v] of (3.9) reports
3. Never goes up, even from3.99. - Ceiling
- Round up to the nearest whole number. [ceiling v] of (3.1) reports
4. Never goes down, even from3.01. - Square root (sqrt)
- The number that, multiplied by itself, gives the input. [sqrt v] of (25) reports
5because 5 × 5 = 25. - Compound block
- A block whose behaviour changes based on a dropdown inside it. The maths block is the biggest example — fourteen behaviours in one block.
Homework 2 min
The Four-Function Tour. One project, one sprite, four scripts — each one demonstrating a different dropdown choice.
- abs: a forever loop that sets [from-centre v] to [abs v] of (x position). Drag the cat around the Stage — the variable should always be positive.
- floor: a script that asks the user "How many sweets?" with ask [How many sweets?] and wait, then says say (join [Full bags of 5: ] ([floor v] of ((answer) / (5)))).
- ceiling: a script that asks "How many kids?" then says say (join [Packs of 6 needed: ] ([ceiling v] of ((answer) / (6)))).
- sqrt: a script that asks "Pick a number" then says say (join [sqrt is: ] (round ([sqrt v] of (answer)))).
Save as HW-L3-05-Maths-Dropdown.sb3. Run it, type a few numbers, watch each function behave differently. Pay special attention to how floor vs ceiling vs round disagree on numbers like 3.5 or 3.9.
Bring back next class:
- The
.sb3file. - A one-sentence answer to: "Why does abs make the cat's distance from x = 0 work the same on both sides of the Stage?"
Heads up for next class: SCR-L03-06 starts a whole new cluster — lists. So far every variable you've built holds one value. A list holds many. We'll look at why that matters and where to find the "Make a List" button. No code next class — just the big idea.