Fortran Kinds & Precision
Each numeric type in Fortran comes in several kinds: different sizes with different range and precision. A default real is usually 32-bit single precision (about 7 decimal digits), which is often not enough for scientific work. Choosing kinds explicitly is one of the most important habits in modern Fortran.
The iso_fortran_env kinds
The standard module iso_fortran_env provides named constants for the common sizes. Use them as the kind parameter in parentheses after the type:
program kinds_demo
use iso_fortran_env, only: int32, int64, real32, real64
implicit none
integer(int32) :: small
integer(int64) :: big
real(real32) :: single
real(real64) :: double
small = huge(small) ! largest value the kind can hold
big = huge(big)
single = 1.0 / 3.0
double = 1.0_real64 / 3.0_real64
print *, small
print *, big
print *, single
print *, double
end program kinds_demo 2147483647
9223372036854775807
0.333333343
0.33333333333333331
| Kind constant | Meaning | Typical range / precision |
|---|---|---|
int8, int16 |
8-, 16-bit integer | ±127, ±32 767 |
int32 |
32-bit integer (default integer) |
±2.1 × 10⁹ |
int64 |
64-bit integer | ±9.2 × 10¹⁸ |
real32 |
single precision (default real) |
~7 digits, up to ~10³⁸ |
real64 |
double precision | ~16 digits, up to ~10³⁰⁸ |
real128 |
quadruple precision (if supported) | ~34 digits |
Kind suffixes on literals
This is the most common precision bug in Fortran. A literal such as 0.1 is a default real even if you assign it to a real64 variable: it is rounded to single precision first, then widened. Attach the kind with an underscore suffix: 0.1_real64.
program literal_kinds
use iso_fortran_env, only: real64
implicit none
real(real64) :: a, b
a = 0.1 ! single-precision literal, then converted
b = 0.1_real64 ! double-precision literal
print '(a, f20.17)', 'a = ', a
print '(a, f20.17)', 'b = ', b
end program literal_kindsa = 0.10000000149011612
b = 0.10000000000000001
a is only correct to about 8 digits. Many codes define a short alias for the kind so the suffix stays readable:
program dp_alias
use iso_fortran_env, only: real64
implicit none
integer, parameter :: dp = real64
real(dp), parameter :: pi = 4.0_dp * atan(1.0_dp)
real(dp) :: r = 1.5_dp
print '(a, f18.15)', 'pi = ', pi
print '(a, f18.15)', 'area = ', pi * r**2
end program dp_aliaspi = 3.141592653589793
area = 7.068583470577035
4 * atan(1) is a classic way to get π to full precision for the chosen kind.
Asking for a precision: selected_real_kind
Instead of naming a size, you can ask for the precision you need. selected_real_kind(p, r) returns a kind with at least p decimal digits and an exponent range of at least r. Similarly selected_int_kind(r) returns an integer kind that can hold numbers up to 10ʳ.
program select_kinds
implicit none
integer, parameter :: wp = selected_real_kind(p=15, r=300)
integer, parameter :: ik = selected_int_kind(12)
real(wp) :: x
integer(ik) :: population
x = 2.0_wp
population = 8100000000_ik
print '(a, i0)', 'kind wp = ', wp
print '(a, i0, a, i0)', 'precision = ', precision(x), ', range = ', range(x)
print '(a, es22.15)', 'sqrt(2) = ', sqrt(x)
print '(a, i0)', 'population = ', population
end program select_kindskind wp = 8
precision = 15, range = 307
sqrt(2) = 1.414213562373095E+00
population = 8100000000
Note: The kind number (here
8) is compiler-specific: gfortran happens to use the byte size, other compilers may not. Never writereal(8)orreal*8in new code; use a named constant such asreal64or aselected_real_kindresult.
Inquiry functions
Fortran can tell you everything about a kind:
| Function | Returns |
|---|---|
huge(x) / tiny(x) |
largest / smallest positive normal number |
epsilon(x) |
smallest e with 1 + e /= 1 |
precision(x), range(x) |
decimal digits, decimal exponent range |
kind(x) |
the kind number of x |
digits(x) |
binary digits in the significand |
program inquiry
use iso_fortran_env, only: real32, real64
implicit none
real(real32) :: s = 1.0
real(real64) :: d = 1.0
print '(a, es12.5, a, es12.5)', 'epsilon: ', epsilon(s), ' vs ', epsilon(d)
print '(a, es12.5, a, es12.5)', 'huge: ', huge(s), ' vs ', huge(d)
print '(a, i0, a, i0)', 'digits: ', digits(s), ' vs ', digits(d)
end program inquiryepsilon: 1.19209E-07 vs 2.22045E-16
huge: 3.40282E+38 vs 1.79769+308
digits: 24 vs 53
Look at the huge value for real64: the exponent has three digits, so the es12.5 descriptor drops the letter E to make room. You'll learn how to control exponent width in the formats lesson (es12.5e3).
Test yourself
Which declaration gives a 64-bit (double-precision) real?
real :: xreal(real64) :: xwithuse iso_fortran_envdouble :: xreal(2) :: x
Why is x = 0.1 imprecise even when x is real(real64)?
- Fortran can't store 0.1
- The literal is a default (single-precision) real, rounded before conversion
- Assignment always truncates
- real64 has only 7 digits
What does selected_real_kind(p=15) return?
- The number 15
- A real kind with at least 15 significant decimal digits
- The value 10**15
- A random kind