Turbo Pascal 2.0 course cover
← Back to Courses
module
9

Numbers With Fractions

Introduction

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.

Real Variables

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.

How A Real Prints

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.

Printing It So It Reads

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);

How Accurate

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.

Reals Into Integers

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:

FunctionDoesExamples
Round(x)The nearest whole number; halves go away from zeroRound(3.5) is 4, Round(2.5) is 3, Round(-3.5) is -4
Trunc(x)Just drops the fractionTrunc(3.9) is 3, Trunc(-3.9) is -3

Both give an integer, so I := Round(7 / 2) compiles, and I is 4.

Arithmetic Functions

FunctionGivesExample
Sqrt(x)The square rootSqrt(2):0:10 prints 1.4142135624
Sqr(x)x times xSqr(5) is 25, Sqr(1.5) is 2.25
Abs(x)x without its minus signAbs(-4) is 4
Int(x)The whole-number part, still a realInt(3.9) is 3.0
Frac(x)The fraction partFrac(3.9) is 0.9

Sqr(5) and Abs(-4) give integers; Sqr(1.5) gives a real.

The Edges

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.

The Code

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.

Change One Thing

  • Change Area:0:2 to Area:0:0. What is printed, and has it been rounded or cut?
  • Change Radius := 2.5 to Radius := 2, a whole number. Does Turbo mind? How is the radius printed?
  • Change the declaration so that Area is an Integer (and take the :0:2 off). What does Turbo say, and what would you use to make it compile?

Exercises

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.

When It Goes Wrong

SymptomCause
A number prints as 1.9634937500E+01It is a real printed without :w:d. Add :0:2, or however many decimals you want.
Error 44: Type mismatchA real going into an Integer. Use Round or Trunc, or make the variable Real.
Error 57: Error in real constantA number too big for a real, such as 1E38.
Run-time error 01A real sum went past the top of the range.
Run-time error 03The square root of a negative number.
A long number comes back with its last digits changedReals keep eleven significant digits.

Summary

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.

Next

S10, Words - letters and strings of letters: Char, string, and cutting text up and putting it together.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.