zaen
zaen

Reputation: 326

Is there a short, portable way to specify "kind" in Fortran?

I generally use this form to get double precision in my Fortran code:

use, intrinsic :: ISO_FORTRAN_ENV
real(kind=real64) :: a = 5d0

However, when defining several variables in a row, real(kind=real64) gets very repetitive. It's tempting to just use real*8, although I've avoided doing so because I've been taught that, although unlikely, it has the potential to make the program non-portable.

From another question I found on the subject I see that real(real64) works, but I don't know if this is good practice; it seems to be the same as using real(8) since, at least on my system, real64 = 8. Is there a shorter way than real(kind=real64) to specify a kind in a variable declaration? Is there even a realistic chance of real*8 causing problems?

Upvotes: 2

Views: 336

Answers (1)

Ian Bush
Ian Bush

Reputation: 7433

The difference between

use, intrinsic :: ISO_FORTRAN_ENV
Real( kind = real64 ) :: a

and

use, intrinsic :: ISO_FORTRAN_ENV
Real( real64 ) :: a

is purely stylistic. Both are equally portable, and are identical as far as the code is concerned. In fact I am having problems understanding why you think they are different. If you think real64 is too long to type you can always use

use, intrinsic :: ISO_FORTRAN_ENV
Integer, Parameter :: wp = real64
Real( wp ) :: a

What is not technically portable is using 5d0 as a constant and assuming it is a real with the same kind as real64. Double precision is obsolete, forget it, rather use the kind mechanism properly with

use, intrinsic :: ISO_FORTRAN_ENV
Integer, Parameter :: wp = real64
Real( wp ) :: a = 5.0_wp

Upvotes: 5

Related Questions