2

When writing a Fortran code, declaring a real variable by assigning kind=8 or double precision is one way of ensuring double precision. Another way is by not declaring anything explicitly in the code but rather using compiler options i.e -r8 (ifort) etc.

Is there a difference between the two?

1 Answers1

7

Carefuly read this, though it is not 100 percent duplicate: Fortran: integer*4 vs integer(4) vs integer(kind=4) and Fortran 90 kind parameter.

kind=8 is non-portable and will not work in some compilers and teaching it to beginners, which can be seen even in one Coursera course should be criminally prosecuted.

There are different compiler options with different effect. Ones promote all 4 byte reals or integers to 8 byte ones. Other set default kind to be 8. This breaks some standard assumptions about storage and about double precision being able to hold more than the default kind. For example in gfortran:

-fdefault-real-8

sets the default real as 8 byte real. This is not completely the same as:

-freal-4-real-8

which promotes all 4 byte reals to 8 byte. You must be careful to understand the differences and generally using these options is not a very good practice.

The recommended solution is always the same, using a named constant:

integer, parameter :: rp = ...

and always using

real(rp) :: x

You can have more of these constants. Set the values of the constants according to the referenced question (real64, kind(1.d0), selected_real_kind(...)).

Community
  • 1
  • 1