Next: Declarations
Up: Arithmetic Expressions
Previous: More About Emacs

INTEGER vs. REAL

Fortran provides two kinds of numbers. Integers are positive and negative whole numbers (for example, -10, 0, and 23) while reals are the usual positive and negative decimal numbers (for example, -15.2, 1., 20.3).

You may be wondering why Fortran bothers with both kinds of numbers. After all, every integer is also a real number. We will examine this issue in the next lesson. For now let's look at the consequences of having two different kinds of numbers. Let's take a look at another example program. Take a look at ``average2.f'' (or view it directly).

This program is virtually identical to ``average1.f'', with a few exceptions. Since both programs should currently be displayed in an Emacs window, it should be easy to compare them. How do they differ?

Click here for the answer

Compile and run ``average2.f'', and give as input two fractional numbers (such as 2.3 or 5.6). Try to explain what happens.

Click here for explanation

There are lots of other ways in which the difference between integers and reals crops up in Fortran programs. To see another, try running the program again. First give it as input two even integers. Then run it again, and give it one even integer and one odd integer as input. Try to explain what happens.

Click here for explanation

This is what you should remember: if all of the arguments to an arithmetic operator are integers, then an integer operation is carried out. If at least one of the arguments to an arithmetic operator is a real, then all of the integer arguments are converted to reals and a real operation is carried out. The difference between integer and real operations is most obvious in the case of division, though we will see other important differences in the next lesson.

Modify the example program by changing the divisor 2 (an INTEGER) to 2.0 (a REAL, by virtue of the decimal point). Now that the divisor in the expression is a REAL, what do you expect will be printed when you run the program again (giving a odd number and an even number as input)? Try it out and explain what happens.

Click here for explanation

Let's try another modification. Replace both occurrences of K in the program above with a REAL variable (such as AVE). Compile and run the program; it should work properly now.

Let's try one final modification. Restore the 2.0 to a 2 and compile and run again. Try to explain what happens.

Click for for explanation

You should keep in mind that the program we have been looking at was only for demonstration purposes. It is a mistake to use INTEGER variables in a program to compute averages because of the truncation problems. We will try to get a better idea of when to use INTEGERS and when to use REALS in the next lesson. For now, the important thing to know is that conversions between INTEGER and REAL will occur when you mix them in expressions and assignment statements. You must be careful to understand when and how the conversions will take place.


Next: Declarations
Up: Arithmetic Expressions
Previous: More About Emacs

Hamlet Project
hamlet@cs.utah.edu