



Integer and Real Data Types Computers can store numbers in several different ways: the most common numerical data types are those called integer and real. Integer variables store numbers exactly and are mainly used to count discrete objects. Real variables are useful many other circumstances as they store numbers using a floating-point representation which can handle numbers with a fractional part as well as whole numbers. The disadvantage of the real data type is that floating-point numbers are not stored exactly: typically only the first six or seven decimal digits will be correct. It is important to select the correct type for every data item in the program. In the last example, the number of years was an integer, but all of the other variables were of real type.
The data type of a constant is always evident from its form: character constants,
for example, are enclosed in a pair of apostrophes. In numerical constants
the presence of a decimal point indicates that they are real and not integer
constants: this is why the value one was represented as "1.0" and not just
"1".
There are several ways to specify the data type of a variable. One is to use explicit type statements at the beginning of the program. For example, the previous program could have begun like this:
PROGRAM LOAN INTEGER NYEARS REAL AMOUNT, PCRATE, RATE, REPAY |
| initial letters I-N | integer type |
| initial letters A-H and O-Z | real type. |
In the preceding program, because the period of the loan was called NYEARS (and
not simply YEARS) it automatically became an integer, while all the other variables
were of real type.