1
votes

I'm trying to read a code written in Fortran 90. In the variable declaration it has for example:

real(ra) :: X

I haven't been able to find out what ra could refer to in this code. I thought it would be something like real(kind=8) Could someone explain this to me?

1

1 Answers

3
votes

Yes

real(ra) :: X

is like

real(kind=8)

In fact it is the same thing if ra=8! The kind= is optional here.

As to finding it ra will be a parameter. It may be in the same routine as the declaration above, it may be in a module used by the routine, or I suppose it might be in a file included in the routine. There may well be different options, but it will be in scope somehow.

So why not use the second form? It is because the kind numbers are not portable and do vary from compiler to compiler - e.g.

[luser@cromer stackoverflow]$ cat kind.f90 
Program real_kinds

  Implicit None

  Real( 8 ) :: a

End Program real_kinds
[luser@cromer stackoverflow]$ gfortran kind.f90 
[luser@cromer stackoverflow]$ nagfor kind.f90
NAG Fortran Compiler Release 5.3.1 pre-release(904)
Warning: kind.f90, line 7: Unused local variable A
Error: kind.f90, line 5: KIND value (8) does not specify a valid representation method
Errors in declarations, no further processing for REAL_KINDS
[NAG Fortran Compiler error termination, 1 error, 1 warning]

It is therefore better to use a parameter initialised with the selected_real_kind intrinsic to specify the kind:

[luser@cromer stackoverflow]$ cat kind.f90 
Program real_kinds

  Implicit None

  Integer, Parameter :: wp = Selected_real_kind( 12, 70 )

  Real( wp ) :: a

  Write( *, * ) Kind( a )

End Program real_kinds
[luser@cromer stackoverflow]$ gfortran kind.f90
[luser@cromer stackoverflow]$ ./a.out
           8
[luser@cromer stackoverflow]$ nagfor kind.f90
NAG Fortran Compiler Release 5.3.1 pre-release(904)
[NAG Fortran Compiler normal termination]
[luser@cromer stackoverflow]$ ./a.out
 2

will probably do what you want. A common alternative to selected_real_kind is

Integer, Parameter :: wp = Kind( 1.0d0 )

So in summary it is the same, just better in that if done carefully it is more portable.

(and finally I really should say that kind values need have no relationship whatsoever to the number of bytes use to store the variable)