Re: Regarding using kind in initializing a variable



Eric wrote:
I'm looking through someone else's code and they are not only using a
kind variable to declare a variable, but they are also using it to
initialize it. For example:
_____________________________________________
program what

integer, parameter :: rd = selected_real_kind(13)
real(kind=rd) :: var

var = 1.0_rd

end program what
____________________________________________

Why is var initialized with '_rd'?

-----%<-----
PROGRAM Display_Literals
IMPLICIT NONE
INTEGER, PARAMETER :: rd = SELECTED_REAL_KIND(13)
REAL(rd) :: var1, var2, var3
var1 = 10.0_rd
var2 = 3.1415927 * var1
var3 = 3.1415927_rd * var1
WRITE(*,'("var2 = ",f19.15)')var2
WRITE(*,'("var3 = ",f19.15)')var3
END PROGRAM Display_Literals
-----%<-----

In the program above, note the difference is that one literal is typed (3.1415927_rd), the other is not (3.1415927). In
the latter case you are assigning a *single* precision literal to a *double* precision variable (in this case, due to
the argument to selected_real_kind, "rd" == "double precision" kind type).


$ gfortran display_literals.f90
$ a.out
var2 = 31.415927410125732
var3 = 31.415927000000000

The precision of the result is dependent on the precision of the literal, regardless of all the type declarations.

cheers,

paulv
.