
Integers are exact and limited: no fractions, and nothing past 32767. For money, measurements, averages and anything big, Pascal has a second kind of number, the real. It is the kind / makes, the kind that printed as 3.5000000000E+00 in S7. This section is about using it, and above all about printing it so that a person can read the answer.
A real variable is declared like an integer one, with the type Real:
var
Radius, Area: Real;
A real can hold a fraction, and it can hold far bigger numbers than an integer: 24.0 * 60 * 60 - the seconds in a day that S7 could not manage - is no trouble. Writing 24.0 rather than 24 makes the sum a real one from the start.
An integer value can be put into a real variable: Radius := 2 is fine, and Radius then holds 2.0.
Left to itself, Writeln prints a real like this:
3.3333333333E-01
That is exponent form. The part before the E is a number between 1 and 10; the part after says how many places to move the decimal point - E-01 means one place to the left, so this is 0.33333333333. 1.9634937500E+01 is 19.634937500. It is always the same width - 18 columns, with a space where a minus sign would go - but nobody wants to read it.
Give a real two numbers after it, :w:d, and it is printed the ordinary way, with d digits after the decimal point, right-aligned in w columns:
Writeln(1/3:10:4);
0.3333
The last digit is rounded: 2.75:1:1 prints 2.8. With d as 0 you get no decimal point at all, rounded to a whole number: 2.5:0:0 prints 3. Width 0 is the useful trick - the number takes as much room as it needs and no more, which is what you want in the middle of a sentence:
Writeln('Area: ', Area:0:2);
A real keeps eleven significant digits. 12345678901.0 is kept exactly; one digit more, 123456789012.0, comes back as 123456789010. For most purposes eleven digits is far more than enough - but it means that a real, unlike an integer, is not always exact, and it is worth rounding when you print one.
An integer can go into a real, but not the other way. Area := 3.14159 * Radius * Radius with Area declared as an Integer will not compile:
Error 44: Type mismatch. Press <ESC>
Turbo will not throw away a fraction without being told how. There are two ways to tell it:
| Function | Does | Examples |
|---|---|---|
Round(x) | The nearest whole number; halves go away from zero | Round(3.5) is 4, Round(2.5) is 3, Round(-3.5) is -4 |
Trunc(x) | Just drops the fraction | Trunc(3.9) is 3, Trunc(-3.9) is -3 |
Both give an integer, so I := Round(7 / 2) compiles, and I is 4.
| Function | Gives | Example |
|---|---|---|
Sqrt(x) | The square root | Sqrt(2):0:10 prints 1.4142135624 |
Sqr(x) | x times x | Sqr(5) is 25, Sqr(1.5) is 2.25 |
Abs(x) | x without its minus sign | Abs(-4) is 4 |
Int(x) | The whole-number part, still a real | Int(3.9) is 3.0 |
Frac(x) | The fraction part | Frac(3.9) is 0.9 |
Sqr(5) and Abs(-4) give integers; Sqr(1.5) gives a real.
Reals have edges too, much further out than integers'. You can write 1E37 in a program, which is a one followed by 37 noughts, but 1E38 is refused (Error 57: Error in real constant). A sum that goes past the top stops the program:
Run-time error 01, PC=004E
Program aborted
A result far too small to hold - 1E-38 / 1E10, say - quietly becomes 0. And a square root of a negative number stops the program with Run-time error 03.
program Circle;
var
Radius, Area: Real;
begin
Radius := 2.5;
Area := 3.14159 * Radius * Radius;
Writeln('Radius: ', Radius:0:1);
Writeln('Area: ', Area:0:2);
Writeln('Area: ', Area);
end.
Starting from
Turbo freshly started, Y, a new work file called CIRCLE.
What you should see
Radius: 2.5
Area: 19.63
Area: 1.9634937500E+01
The same area twice: once as people write it, once as Turbo does by default.
Area:0:2 to Area:0:0. What is printed, and has it been rounded or cut?Radius := 2.5 to Radius := 2, a whole number. Does Turbo mind? How is the radius printed?Area is an Integer (and take the :0:2 off). What does Turbo say, and what would you use to make it compile?9.1 Three test marks are 7, 8 and 10. Print their average to two decimal places.
9.2 Print the number of seconds in a day, correctly this time.
9.3 Convert 100 degrees Fahrenheit to Celsius - take away 32, multiply by 5, divide by 9 - and print the answer to one decimal place, and then rounded to a whole number.
Worked solutions are in Appendix II.
| Symptom | Cause |
|---|---|
A number prints as 1.9634937500E+01 | It is a real printed without :w:d. Add :0:2, or however many decimals you want. |
Error 44: Type mismatch | A real going into an Integer. Use Round or Trunc, or make the variable Real. |
Error 57: Error in real constant | A number too big for a real, such as 1E38. |
Run-time error 01 | A real sum went past the top of the range. |
Run-time error 03 | The square root of a negative number. |
| A long number comes back with its last digits changed | Reals keep eleven significant digits. |
Real holds fractions and very large numbers, to eleven significant digits. Printed bare it comes out in exponent form; :w:d prints it with d decimals, rounded, and :0:d in just the room it needs. An integer goes into a real freely; a real goes into an integer only through Round or Trunc. Sqrt, Sqr, Abs, Int and Frac do what their names say.
S10, Words - letters and strings of letters: Char, string, and cutting text up and putting it together.