
Computers were built to do sums. But Turbo's whole numbers have edges, and a sum that runs off one does not stop with an error - it quietly gives you the wrong answer. This section is the arithmetic, and the edges.
In S4 you printed a sum: Writeln(12 * 12) printed 144. Writeln will print several things at once, separated by commas, one straight after the other:
Writeln('7 + 2 = ', 7 + 2);
7 + 2 = 9
The text in quotes is printed as it is, space and all; the sum is worked out and its answer printed. Numbers are printed with nothing round them - Writeln(7 + 2, 7 - 2, 7 * 2) prints 9514, which is 9, 5 and 14 jammed together. When you print more than one number, put something between them:
Writeln(7 + 2, ' ', 7 - 2, ' ', 7 * 2);
| Operator | Does | Example | Gives |
|---|---|---|---|
+ | add | 7 + 2 | 9 |
- | subtract | 7 - 2 | 5 |
* | multiply | 7 * 2 | 14 |
div | divide, whole number part | 7 div 2 | 3 |
mod | the remainder after div | 7 mod 2 | 1 |
div and mod are words, not signs, and they need spaces round them like any other word. Between them they answer the question "how many times does 2 go into 7, and what is left over?"
With a negative number, div throws away the fraction towards zero: -7 div 2 is -3, and -7 mod 2 is -1.
There is a sixth: /, the ordinary division sign. It does not give a whole number. Writeln(7 / 2) prints
3.5000000000E+00
which is 3.5, written the way Turbo writes numbers with fractions. That is a different kind of number altogether, and S9 is about it. Until then, divide whole numbers with div.
2 + 3 * 4 is 14, not 20: multiplying and dividing - *, /, div, mod - are done before adding and subtracting. Otherwise the work goes from left to right: 20 - 6 - 4 is 10, and 20 div 2 * 5 is 50.
Brackets are done first of all. (2 + 3) * 4 is 20. When you are not sure which way Turbo will take a sum, put brackets in, and the next person to read it - which is usually you - will not have to work it out either.
Put a colon and a number after something you print, and it is printed right-aligned in that many columns:
Writeln('[', 42:5, ']');
[ 42]
Three spaces and two digits make five. It is how you get a column of numbers to line up on the right. If the number needs more room than you gave it, it simply takes it: 42:1 prints 42.
Turbo's whole numbers - integers - run from -32768 to 32767. Turbo has a name for the top one, MaxInt: Writeln(MaxInt) prints 32767.
Go past the top and nothing stops you. The number wraps round to the bottom:
Writeln(32767 + 1);
-32768
No error, no warning: just a wrong answer. And it happens in the middle of a sum as well as at the end. 1000 * 100 div 50 ought to be 2000. But 1000 * 100 is 100,000, which is past the top, so it wraps round before the div is ever reached, and the whole sum prints -621.
Turbo does check the numbers you write in the program. Type a number too big to be an integer, such as 40000, and it will not compile:
Error 56: Error in integer constant. Press <ESC>
One oddity: you cannot write -32768 either, though it is an integer - the 32768 is refused, minus sign or no. If you ever need it, -32767 - 1 gets you there.
7 / 0 stops the program:
Run-time error 02, PC=0045
Program aborted
Searching
4 lines
Run-time error position found. Press <ESC>
A run-time error is one Turbo could not have seen when it compiled the program, because the sum is only done when the program runs. Press ESC and Turbo puts you in the editor at the statement that failed, just as it does for a compile error.
div and mod by zero are worse: they do not stop at all. 7 div 0 prints -1 and 7 mod 0 prints -7, and the program carries on with those. If a program of yours might divide by something that could be zero, it is up to you to check first - S12 shows how.
program Sums;
begin
Writeln('7 + 2 = ', 7 + 2);
Writeln('7 - 2 = ', 7 - 2);
Writeln('7 * 2 = ', 7 * 2);
Writeln('7 div 2 = ', 7 div 2);
Writeln('7 mod 2 = ', 7 mod 2);
Writeln('2 + 3 * 4 = ', 2 + 3 * 4);
end.
Starting from
Turbo freshly started, Y, a new work file called SUMS.
What you should see
7 + 2 = 9
7 - 2 = 5
7 * 2 = 14
7 div 2 = 3
7 mod 2 = 1
2 + 3 * 4 = 14
2 + 3 in the last line, both inside the quotes and outside them. What does it print now?7s in the div and mod lines to -7, quotes and all. Which way does the answer go?* line to use / instead. What do you get, and why is it so different from the lines round it?7.1 Print the number of minutes in a day.
7.2 Print the number of seconds in a day. Is the answer right? If not, what has happened to it?
7.3 One hundred sweets are shared among seven children, as many each as will go round equally. Print how many each child gets and how many are left over.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
| Numbers printed run into each other | Writeln puts nothing between numbers. Print ' ' between them. |
| A sum gives a negative answer, or a wildly wrong one | It went past 32767 - perhaps in the middle of the sum. Do the dividing first, or use smaller steps. |
Error 56: Error in integer constant | A number in the program is bigger than 32767 - or is -32768. |
Run-time error 02 | Something was divided by zero with /. ESC shows where. |
div or mod gives a nonsense answer | Possibly a division by zero, which div and mod do not report. |
+, -, *, div and mod work on whole numbers; *, div and mod come before + and -, brackets before everything. Writeln prints several things separated by commas, and :n lines a number up in n columns. Integers go from -32768 to 32767 and wrap round silently past either end; dividing by zero stops a program with / and does not with div or mod.
S8, Names For Numbers - keeping a number in a variable, changing it, and building a result a step at a time.