PARAMETER constants and conversions
Two kinds of declaration turn a literal into a name: the parameter, which is a constant the compiler folds into the program, and the enumerator, which is an integer constant with a name and an optional C binding. This lesson covers both, and then the conversions between the numeric types, because a conversion that the compiler does silently is where the rounding bugs live.
PARAMETER constants
parameter is an attribute: it makes the name a constant. The value must be a constant expression, so the parameter can be computed from other parameters but not read from a file or a function at run time. A parameter can be used where the language wants a constant: an array bound, a case label, the length of a character type.
The file params.f90:
program params
implicit none
integer, parameter :: n = 5
real, parameter :: pi = 3.14159265
character(len=*), parameter :: greeting = "hello"
integer :: values(n)
integer :: i
do i = 1, n
values(i) = i * i
end do
print *, "n :", n
print *, "pi :", pi
print *, "greeting:", greeting
print *, "values :", values
end program params
gfortran -Wall -Wextra -std=f2018 params.f90 -o params
./params
n : 5
pi : 3.14159274
greeting:hello
values : 1 4 9 16 25
character(len=*) takes its length from the value it is initialised with, which is the one place the star is a convenience rather than a placeholder: the parameter has exactly the length of "hello".
A parameter is not a variable, so any statement that assigns to it is a compile error, and that error is the reason to prefer it to a variable that the program never changes: the compiler enforces the promise.
ENUM and enumerators
Fortran 2003 added the C enumeration. An enum, bind(c) block declares a set of integer constants and gives them the C representation, so a value can be passed to a C function that expects its enum.
The file enums.f90:
program enums
use iso_c_binding, only: c_int
implicit none
enum, bind(c)
enumerator :: red = 1, green, blue
end enum
integer(c_int) :: colour
colour = green
print *, "red :", red
print *, "green:", green
print *, "blue :", blue
print *, "colour:", colour
end program enums
gfortran -Wall -Wextra -std=f2018 enums.f90 -o enums
./enums
red : 1
green: 2
blue : 3
colour: 2
The first enumerator sets the value and the rest count up from it, exactly as in C. Without bind(c) the block is still an enumeration, and the compiler may choose the representation; with it, the values are the C ones and the block may only be declared in a module or a program, not inside a procedure.
Most Fortran projects use integer, parameter for named codes and enum, bind(c) only at the C boundary, where the C header already defines the values and the Fortran side has to match them.
INT, REAL, CMPLX and their kind
A conversion in Fortran is a function call with an optional kind argument. int(x) truncates toward zero, real(i) converts exactly when the integer fits, and cmplx(a, b) builds a complex from two reals. Each takes a second argument that names the kind of the result: real(7, dp) is the double precision seven.
The file convert.f90:
program convert
implicit none
integer, parameter :: dp = kind(1.0d0)
real(dp) :: x
integer :: n
x = 2.75_dp
n = int(x)
print *, "int(2.75) :", n
print *, "int(-2.75) :", int(-2.75_dp)
print *, "nint(2.5) :", nint(2.5_dp)
print *, "nint(3.5) :", nint(3.5_dp)
print *, "nint(-2.5) :", nint(-2.5_dp)
print *, "floor(2.75) :", floor(2.75_dp), " floor(-2.75):", floor(-2.75_dp)
print *, "ceiling(2.25):", ceiling(2.25_dp)
print *, "real(7, dp) :", real(7, dp) / 2
print *, "cmplx(1, 2) :", cmplx(1, 2, dp)
print *, "aimag :", aimag(cmplx(1, 2, dp))
end program convert
gfortran -Wall -Wextra -std=f2018 convert.f90 -o convert
./convert
int(2.75) : 2
int(-2.75) : -2
nint(2.5) : 3
nint(3.5) : 4
nint(-2.5) : -3
floor(2.75) : 2 floor(-2.75): -3
ceiling(2.25): 3
real(7, dp) : 3.5000000000000000
cmplx(1, 2) : (1.0000000000000000,2.0000000000000000)
aimag : 2.0000000000000000
NINT, FLOOR and CEILING
Four intrinsics answer the four rounding questions, and the difference between them is the difference between two programs that agree on every input except the one that matters:
- int(x) truncates toward zero. int(2.75) is 2 and int(-2.75) is -2.
- nint(x) rounds to the nearest integer, with a half rounded away from zero. nint(2.5) is 3, nint(3.5) is 4 and nint(-2.5) is -3.
- floor(x) rounds down, toward minus infinity: floor(-2.75) is -3.
- ceiling(x) rounds up, toward plus infinity.
A loop that walks a grid uses nint to find the cell of a point, and a histogram that bins a value uses floor. Choosing the wrong one shifts every bin by half a cell, and the shift is invisible in a print of the totals.
The half-way rule of nint is worth stating in a test: it rounds away from zero, not to even. A program that needs the bankers' rounding of a language like Python has to write it, and the lesson on real arithmetic shows how.
Conversions that happen silently
An assignment between numeric types converts without a function call: real to integer truncates, integer to real rounds to the nearest representable value, and a double assigned to a single loses digits. The compiler says nothing at -Wall -Wextra; the diagnostics for these conversions are in -Wconversion-extra, which reports them as warnings.
Two habits keep the silent conversions harmless. Write the conversion intrinsic when the conversion is the point, so a reader sees it. And give every real literal the kind suffix of its variable, so an expression is computed at the precision it is stored in.
Exercises
- Declare n as a parameter and then assign to it. What does the compiler say?
- Print nint(0.5) and nint(-0.5). Which way does each half go?
- Convert -2.5 with int, nint, floor and ceiling. Write the four answers before you run it.
- Add -Wconversion-extra to the build of the conversion program. Which lines does it report?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the PARAMETER attribute do?
Answer
It makes the name a constant the compiler checks
An assignment to a parameter is a compile error, which is the promise it buys.
-
Where may a parameter be used?
Answer
Wherever a constant is required: an array bound or a CASE label
A parameter is a constant expression, so it fits every constant context.
-
What does
integer, parameter :: k = selected_int_kind(18)give?Answer
A named constant holding a kind value
The function is pure with constant arguments, so its result can be a parameter.
-
What does
int(-2.75)return?Answer
-2
INT truncates toward zero, so -2.75 becomes -2.
-
What does
nint(-2.5)return?Answer
-3
NINT rounds a half away from zero.
-
What does
floor(-2.75)return?Answer
-3
FLOOR goes toward minus infinity, which is the larger magnitude here.
-
What does this program print?
program p implicit none print *, nint(2.5) + int(2.5) end program pAnswer
5
nint(2.5) is 3, int(2.5) is 2, and the sum is 5.
-
What does
real(7, dp) / 2compute?Answer
Double precision division, giving 3.5 to double precision
The conversion makes the first operand a double, so the division happens in double.
-
Which flag reports the conversions an assignment performs silently?
Answer
-Wconversion-extra
The conversion diagnostics are not in -Wall; the extra flag adds them.
-
What does an
enum, bind(c)block give a constant?Answer
The C representation, so it can be passed to a C function
The bind(c) makes the enumeration match the C one at the boundary.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev