Next: Constants
Up: Numbers and Constants
Previous: Numbers and Constants

INTEGER and REAL

We have already seen that a programmer must be careful to understand the distinction between INTEGER and REAL expressions in Fortran programs. In fact, this is so important that it is desirable to explicitly declare the type of each variable in order to avoid mistakes.

The obvious question, and one that we have raised before, is why bother with integers at all? Since every integer is a real number, why not do away with the INTEGER type and just support REAL values in Fortran programs? Through a series of examples, we will show you why integers are necessary.

Let's take a look at our first example program. Look at ``rdemo.f'' from your ``examples'' directory (or view it directly).

Compile and run it twice with the following two inputs: 12345 and 1234567. Do you understand what you see?

Click here for an explanation

Now try running the program again with 123456789 as the input. What happens?

Click here for an explanation

In Fortran (and other programming languages), integers and integer arithmetic are exact, whereas reals and real arithmetic are approximate. To understand why this is the case, let's look at a second example program, ``rdemo2.f'' (or view it directly).

This program is much like the first one, except that it reads a number as a REAL and then transforms it into an INTEGER. Run this program several times. Begin by giving it a nine-digit integer as input. Give it larger and larger integers until something interesting happens. What happens?

Click here for an explanation

Only a finite number of numbers can be represented on a computer. In Fortran, the INTEGER type represents all of the integers between a most negative integer and a most positive integer. Within this range, integer arithmetic is exact. The REAL type represents some of the real numbers between a most negative and a most positive real. Within this range (which is much larger than the INTEGER range), real arithmetic is approximate.


Next: Constants
Up: Numbers and Constants
Previous: Numbers and Constants

Hamlet Project
hamlet@cs.utah.edu