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
.



Relevant Pages

  • Re: Regarding using kind in initializing a variable
    ... kind variable to declare a variable, but they are also using it to ... initialize it. ... var = 1.0_rd ... the underscore followed by a KIND number is the generic form to specify the KIND of a constant. ...
    (comp.lang.fortran)
  • Re: Regarding using kind in initializing a variable
    ... kind variable to declare a variable, but they are also using it to ... Why is var initialized with '_rd'? ... Simply as a matter of form, to make clear that it should initialize to the same type as its declaration. ... Tim Prince ...
    (comp.lang.fortran)
  • Re: variable scope problem..
    ... >> if you declare a var in a block it will be accessable in the entire ... >> Tommy Karlsson ... initialize them before you use them. ...
    (comp.lang.java.help)
  • Re: Regarding using kind in initializing a variable
    ... kind variable to declare a variable, but they are also using it to ... initialize it. ... Why is var initialized with '_rd'? ... without the _rd, is single precision, while var is higher precision. ...
    (comp.lang.fortran)
  • Re: Dynamic variable names
    ... Note that var defines the scope of a variable. ... variable you initialize belongs to the scope where it has been ... But it has been added no assignment yet, ...
    (comp.lang.javascript)