Pascal from the ground up

Arithmetic, div and mod

The operators Pascal gives you, why div throws the fraction away, how mod returns the remainder, and the one slash that always gives a real.

Part 3 of 12

Beginner14 min readBack to Courses

Five operators, two of them new

The familiar operators behave as you expect: + for addition, - for subtraction, and the star for multiplication. A number written next to a name is not multiplication in Pascal, so the star is always required.

The two new ones are div and mod. Pascal does not use a bare slash for whole number division, because the slash means something else here.

div and mod

div divides and throws the fractional part away. 7 div 2 is 3, not 3.5 — the half is dropped, never rounded. mod is the companion: it returns what is left over, so 7 mod 2 is 1.

Together they answer two questions at once. div says how many whole groups fit and mod says how much is left when the groups are taken out. That is why they are the pair to reach for with money in whole units, or with rows and columns of a table.

The result of 7 div 2 is not rounded up when the fraction is large: 9 div 2 is 4, not 5. If rounding is what you want, round is the function for it, and it does not do the same thing as div.

Neither operator is a rounding function. div answers how many whole groups fit and mod answers what is left, and together they account for every unit: the groups times the divisor plus the remainder is always the original number. That identity is worth remembering, because it is the check that tells you the pair was used correctly.

The slash always gives a real

/ is decimal division whatever its operands are. 7 / 2 is 3.5, and the result has type real, so it needs a format when you print it. This is the opposite of languages where dividing two whole numbers gives a whole number.

Precedence follows arithmetic: multiplication, division and both of its whole-number forms come before addition and subtraction, left to right within a level, and brackets win over everything. 2 + 3 * 4 is 14, and (2 + 3) * 4 is 20.

A common confusion is expecting 10 / 2 to be an integer because the answer happens to be whole. The type is decided by the operator, not by the value, so the result is still a real and still prints in decimal form unless you format it. Assigning it to an integer variable is a type error even when the division is exact.

The two divisions side by side

program Main;
var
  ratio: real;
begin
  writeln('div ', 7 div 2);
  writeln('mod ', 7 mod 2);
  ratio := 7 / 2;
  writeln('/   ', ratio:0:1);
end.

What happens below zero

div truncates toward zero, not toward negative infinity. -7 div 2 is -3, because the fractional part is dropped from the quotient, while 9 div 2 is 4. The same rule reaches mod: the remainder keeps the sign of the left operand, so -7 mod 2 is -1 and 7 mod -2 is 1.

That detail matters when mod tests evenness. n mod 2 = 0 is still a correct even test for a negative number, but n mod 2 = 1 is false for an odd negative number because the answer is -1. Write n mod 2 <> 0 when the question is simply odd or even. Mixing an integer with a real promotes the expression to real, and mod refuses a real on either side.

A worked change, in words

Take 137 and break it into fifties, twenties and tens. 137 div 50 is 2, because two fifties fit; 137 mod 50 is 37, the part left over. The next line asks how many twenties fit in 37 — one — and leaves 17. Then one ten fits, leaving 7, which no note can cover. Every step uses the same pair on a smaller number, and the final remainder is the amount that cannot be paid exactly.

Following the numbers by hand before running the program is the fastest way to see an error in the order of the lines. If you swap a div for a mod, the amount stops shrinking and the answer becomes nonsense immediately.

Three arithmetic mistakes to watch for: expecting div to round (it never does), writing 7 / 2 and then assigning the real result to an integer, and putting a real into mod, which the compiler rejects. Use round or trunc when you mean to convert, and keep div and mod for whole numbers.

Try these next

  • Change the amount to 9 and the people to 4; write down both answers before running.
  • Print -9 div 4 and -9 mod 4, then explain the signs to yourself.
  • Rewrite the ratio line with round and compare it with the formatted real.

Groups, leftovers and the slash

Run it, then change the two starting numbers and predict the quotient, the remainder and the decimal before you run it again.

Output
Click "Run" to execute your code

A division that looks like whole numbers can still be a real one. If you see a long decimal where you expected an integer, check for a slash where a div belongs.

Break an amount into notes

Run it, then change the amount to 248. Work out each note count on paper before running, and check the leftover at the end.

Output
Click "Run" to execute your code

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

What is 9 div 2?