In Fortran 90 and later, intrinsic types such as real and integer have a kind attribute which guarantees a specific precision and/or range. real*8
and counterparts should no longer be used (Chin, Worth, and Greenough, 2006, p. 5). double precision
is also no longer needed and can be thought of as a kind
of real
. The following example declares two real
variables a
and b
which are guaranteed to have at least 15 significant digits of precision and an exponent range of at least 307 (Lemmon and Schafer, 2005, p. 20–22):
integer, parameter :: dp = selected_real_kind(15, 307)
real(kind=dp) :: a
real(dp) :: b
One can also guarantee precision up to that of the machine-compiler-specific double precision real
by using the kind function to get the kind
of 1.d0
:
integer, parameter :: dp = kind(1.d0)
Another possibility, suggested by Metcalf, et. al (2004, p. 71), ensures that the double and quad types are actually twice and four times the precision of a single:
integer, parameter :: &
sp = kind(1.0), &
dp = selected_real_kind(2*precision(1.0_sp)), &
qp = selected_real_kind(2*precision(1.0_dp))
Finally, precision of 32-, 64-, and 128-bit reals can usually be obtained with the following constants:
integer, parameter :: sp = selected_real_kind(6, 37)
integer, parameter :: dp = selected_real_kind(15, 307)
integer, parameter :: qp = selected_real_kind(33, 4931)
In Fortran 2008, also the following constants are available:
use, intrinsic :: iso_fortran_env
integer, parameter :: sp = REAL32
integer, parameter :: dp = REAL64
integer, parameter :: qp = REAL128
Chin, L. S., D. J. Worth and C. Greenough (2006). Thoughts on using the Features of Fortran 95. Software Engineering Group Notes no. SEG-N-003.
Metcalf, M., J. Reid, and M. Cohen (2004). Fortran 95/2003 Explained. Oxford University Press.
Lemmon, D. R. and J. L. Schafer. (2005). Developing Statistical Software in Fortran 95. Springer.