Fortran provides three numerical data types out of the box: integer, real, and complex.
Integer numbers
The integer is the simplest numeric type in Fortran. Here are some examples of integer literals:
0 1 42 100 -500 +1234567
You declare one or more integers like this:
integer :: i, n
This statement instructs the compiler to reserve the space in memory for integer variables i and n. It’s made of the type statement (integer) on the left, double colons (::) in the middle, and a list of variables separated by commas.
- Integers are always signed–they can be both negative and positive, as well as zero.
- They have a limited range that’s determined by their type kind. Larger type kinds result in a wider range.
- Exceeding the permissible range of a variable results in an overflow. In that event, the value of the variable will wrap around its range limits.
- The default integer size in memory isn’t defined by the Fortran standard and is system dependent. However, on most systems, the default integer size is 4 bytes.
Real numbers
Real numbers, also known as floating-point numbers, describe any number that has a value along a continuous (nondiscrete) axis. Here are some examples of real numbers:
0.0 1.000 42. 3.14159256 -5e2 +1.234567e5
The first four of these are intuitive enough–the decimal point separates the whole part of the number on the left and the fractional part of the number on the right. The last two may seem strange, as they’re written using exponential notation. They consist of an integer or real number on the left side of the character e, and an integer on its right side that denotes the decimal exponent. -5e2 thus corresponds to -5 × 102, and +1.234567e5 corresponds to 1.234567 × 105. For positive numbers, the unary plus symbol is optional. We’ll talk more about formatting real numbers in chapter 6.
Be mindful about the decimal point!
When writing literal constants, there’s a fine line between what the compiler will understand as an integer or a real. A single period after the number makes the difference. For example, 42 is an integer, but 42. is a real. This is the same behavior as in C or Python.
We declare real numbers using the keyword real:
real :: x
This declaration statement is analogous to the one for integers, except for the type and variable names.
Complex numbers
A complex number is simply a pair of real numbers, one for the real component and one for the imaginary component. They’re declared and initialized like this:
complex :: c = (1.1, 0.8)
The complex intrinsic type was introduced into Fortran to make arithmetic with complex numbers easier to program. Depending on your application, you’ll either use them often or not at all.