When It Doesn't Divide Evenly
Seven does not split into twos. What you do with the part left over is a question every program has to answer.
Some sums come out exactly. Six divided by two is three, and nothing is left over.
Most do not. Seven divided by two is where the trouble starts, and every program has to decide what to do about it.
Division gives you the whole answer
Ask for it plainly and you get the fraction:
SHOW 7 / 2
3.5
That is often what you want. Half a second, half a pixel.
Sometimes it is useless. You cannot give three and a half sweets to a child, put half a player on a screen row, or fit 3.5 things in a row of boxes. For those you want whole things, and you want to know what was left behind.
How many whole ones fit
ROUND DOWN throws away anything after the point:
SHOW ROUND DOWN (7 / 2)
3
Three whole twos fit into seven. The brackets say work the division out first, which is the rule from Unit 1 earning its keep.
What is left over
REMAINDER gives you the part that did not fit:
SHOW 7 REMAINDER 2
1
Seven sweets shared between two children: three each, one left in the bag. ROUND DOWN
gives the three, REMAINDER gives the one.
The two answers together account for everything. Three twos is six, and one more makes seven.
A remainder of nothing
When a number divides exactly, nothing is left:
SHOW 8 REMAINDER 2
SHOW 9 REMAINDER 2
0 1
Eight is even, so halving it leaves nothing behind. Nine is odd, so one is stranded.
That is the trick behind a surprising amount of game code. A remainder of 0 against 2 means even. Against 4, it means every fourth one. Against the width of the screen, it means a sprite that has run off the right edge and should come back on the left. You will meet all three once your programs can make choices and repeat themselves.
Rounding to the nearest
ROUND DOWN always goes down. ROUND goes to whichever whole number is nearer:
SHOW ROUND 3.2
SHOW ROUND 3.7
3 4
Use ROUND when you want the closest answer, and ROUND DOWN when you want to know how
many whole ones fit. They are different questions. Three and a half hours is four hours to
the nearest hour, but it is still only three whole hours.
When it’s wrong, see why
- You lost a fraction you wanted.
ROUND DOWNdiscards it on purpose. If you needed 3.5, do not round at all. - Everything came out one too small. You rounded down where you wanted the nearest.
ROUND DOWN 3.7is 3, andROUND 3.7is 4. - The remainder was bigger than you expected. A remainder is always smaller than the
number you divided by. If it is not, check which number went where:
7 REMAINDER 2and2 REMAINDER 7are different questions. - You divided by zero. Nothing divides by zero, and no program can rescue you from asking. Make sure the second number cannot be nought.
What you’ve learnt
/gives the whole answer, fraction and all.ROUND DOWNgives how many whole ones fit, throwing away the rest.REMAINDERgives the part left over.- Together those two account for the original number.
- A remainder of 0 means it divided exactly, which is how a program spots every second or every fourth thing.
ROUNDgoes to the nearest whole number, which is a different question again.
What’s next
Your programs work things out, but they still run the same lines in the same order every time. In the next module, Decisions, they learn to decide.