16

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?
Joel DeWitt
  • 1,226
  • 2
  • 13
  • 26
  • 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 Mar 12 '14 at 20:36

3 Answers3

25

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
High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
  • This is also my preference. `real64` is a Fortran 2008 enhancement of the `iso_fortran_env`. To me, that seems generally available, e.g., starting with gfortran 4.5. The `iso_c_binding` types have been available longer. – M. S. B. Mar 12 '14 at 20:12
  • Please note, that `real128` is bugged in gfortran, where it returns a 10 byte real instead of a 16 byte real. (Just in case, you need extra high precision.) – Stefan Mar 13 '14 at 07:44
  • gfortran has supported true quad-precision since version 4.6 in 2011. See http://gcc.gnu.org/ml/fortran/2011-07/msg00064.html. – M. S. B. Mar 13 '14 at 20:53
  • I have been using the gfortran compiler option `-fdefault-real-8`, so far; now for other reason I want to use the `iso_fortran_env` module, and that option conflicts with this module's `NUMERIC_STORAGE_SIZE` named constant (it's just a warning, but I fear I could regret, should I ignore it). How do you suggest to act? – Enlico Feb 22 '18 at 20:08
  • @EnricoMariaDeAngelis what do you mean with "conflicts" – kvantour Jun 28 '18 at 15:56
  • 1
    @kvantour, I mean that using the `-fdefault-real-8` compile option while the `USE`ing `ISO_FORTRAN_ENV` is in some source file raises `Warning: Use of the NUMERIC_STORAGE_SIZE named constant from intrinsic module ISO_FORTRAN_ENV at (1) is incompatible with option -fdefault-real-8` at compile time. I posted [a question](https://stackoverflow.com/q/49588914/5825294) about it and received an answer. – Enlico Jun 28 '18 at 16:26
  • 1
    @EnricoMariaDeAngelis, aha excellent way forward to ask the question. +1 – kvantour Jun 28 '18 at 16:28
  • How does `dp=>real64` work? I've tried it in my code and verified that it does work, but I don't understand what's going on. Is `dp` a pointer to `real64`? – zaen Aug 01 '19 at 21:35
5

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.

Kyle Kanos
  • 3,257
  • 2
  • 23
  • 37
  • 2
    +1 for completeness, be careful though with `-fdefault-real-8`, as it promotes `double precision` to 16 bytes, consult `man gfortran` and look also at `-fdefault-double-8`. – steabert Mar 12 '14 at 20:13
0

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

INTEGER, PARAMETER :: rk = SELECTED_REAL_KIND(15, 307)
REAL(rk) :: x
...
Alex
  • 699
  • 1
  • 10
  • 20