15
votes

Have this burning question on my mind right now: What is the "accepted" way to declare double precision real in modern Fortran? In order from oldest to newest, the story seems to go like this: DOUBLE PRECISION, then REAL(kind=8), then INTEGER, PARAMETER :: dp=kind(1.d0) with REAL(kind=dp)--Metcalf now says dp=kind(0.d0)--and now float32=selected_real_kind(6,37) or float64=selected_real_kind(15,307). So...

  1. How should I be declaring double precision real now?
  2. Is kind redundant in REAL(kind=dp)?
  3. Are there any special flags needed at compile time to invoke double precision real with gfortran or ifort?
3
I would ask what YOU mean by "double precision". The standard simply says that the precision must be greater than that of default real. If you have some specific requirement, then use SELECTED_REAL_KIND. - Steve Lionel

3 Answers

24
votes

Personally I now write

use, intrinsic :: iso_fortran_env

which includes parameters such as int32,real64 which have the obvious meanings, and can be used like this:

real(real64) :: a_64_bit_real_scalar

Note that kind=8 is not guaranteed, by the standard, to deliver an 8-byte kind. The values that kind parameters take are not standardised and do vary from compiler to compiler.

You could, if you want, write statements such as

use, intrinsic :: iso_fortran_env, dp=>real64
...
real(dp) :: a_64_bit_real_scalar
4
votes

1)How should I be declaring double precision real now?

I personally prefer using the

integer, parameter :: wp = selected_real_kind(15,307)
real(wp) :: var

method for this. But as Mark points out, the iso_fortran_env is another straight-forward method. If you plan on using interfaces to C, you may want to try the ISO_C_BINDING module and use

use iso_c_binding
real(c_double) :: var

And get the double precision you want.

2) Is kind redundant in REAL(kind=dp)?

Yes it is.

3) Are there any special flags needed at compile time to invoke double precision real with gfortran or ifort?

For gfortran you can use the compilation flag -fdefault-real-8. For ifort, you can use the -real-size=64 flag.

0
votes

The current recommended way of declaring double precision reals in Fortran is:

INTEGER, PARAMETER :: rk = SELECTED_REAL_KIND(15, 307)
REAL(rk) :: x
...