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 can keep the fraction
Ask for it plainly and you get the fraction:
SHOW 7 / 23.5
That is often what you want: half a second or a position between pixels. This pseudocode keeps the fraction. Real languages and machines have their own division rules and finite precision; we will name those when we use them.
Sometimes you need whole things: sealed bags, players in a team or tiles in a row. Then you want to know how many fit and what is left behind.
How many whole ones fit
ROUND DOWN gives the greatest whole number no larger than the input:
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.
For positive values, that looks like dropping the fractional digits. Negative values
show the difference: ROUND DOWN (-7 / 2) is -4, not -3. On a number line,
-4 is below -3.5; -3 is above it. Dropping the fraction would move towards zero.
What is left over
For this introduction, use a non-negative whole number of things and a positive
whole number to divide by. REMAINDER gives the part that did not fit:
SHOW 7 REMAINDER 21
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:
original = whole groups * group size + remainder
7 = 3 * 2 + 1The remainder is at least zero and smaller than the positive group size. We have not defined a rule for negative remainder inputs here; check the rule of the language you use.
A remainder of nothing
When a number divides exactly, nothing is left:
SHOW 8 REMAINDER 2
SHOW 9 REMAINDER 20 1
Eight is even, so halving it leaves nothing behind. Nine is odd, so one is stranded.
A remainder of 0 against 2 identifies an even whole number. Testing a counter against 4 identifies multiples of four, including zero; where the counter starts decides when that first event happens.
For a non-negative position on a 32-column screen, position REMAINDER 32 gives
a wrapped column from 0 through 31: 31 stays 31, 32 becomes 0 and 33 becomes 1.
Use the remainder value as the new column. Testing whether it equals zero only
identifies exact multiples of 32. Negative positions need a stated rule too.
Rounding to the nearest
ROUND DOWN always goes down. ROUND goes to whichever whole number is nearer:
SHOW ROUND 3.2
SHOW ROUND 3.73 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. For the non-negative values in these examples, we choose the higher whole number
when exactly halfway: ROUND (7 / 2) is 4. Other languages may use another
halfway rule. We have not chosen a rule for negative halfway values here.
Do 7 / 2, ROUND DOWN (7 / 2), 7 REMAINDER 2 and ROUND (7 / 2) answer the same question?
No. The results are 3.5, 3, 1 and 4. They give the full quotient, the number of whole groups, the leftover and the nearest whole number under our stated halfway rule. Three groups of two plus one leftover account for all seven.
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. With our non-negative whole inputs
and positive divisor, the remainder is smaller than the divisor. If it is not, check which number went where:
7 REMAINDER 2and2 REMAINDER 7are different questions. - You divided by zero. Division by zero has no defined result in these examples. A program can reject that input before dividing; check that the divisor is positive here.
What you’ve learnt
/keeps a fractional quotient in this notation; target division rules may differ.ROUND DOWNgoes to the greatest whole number no larger than the input.REMAINDERgives the part left over for our non-negative whole inputs and positive divisor.- Together those two account for the original number.
- A remainder of 0 means it divided exactly, which can identify multiples of two or four.
ROUNDgoes to the nearest whole number, which is a different question again.
Try it in a game
Choose a new secret
shows how Spectrum BASIC uses INT to turn a non-negative random fraction into a
whole-number range. Read its operation table; playing the preceding game is optional.
Centre Text on a Character Screen uses half the unused width as a starting column. With 22 columns and 7 letters, there are 15 spare columns: rounding down puts 7 on the left and 8 on the right. You can reason about that choice without reading its optional assembly listing.
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.