Reputation: 2666
Simple question: I'm looking at Fortran code in an atmospheric model, and whenever a real variable is declared, it is always declared e.g. as real :: foo = 1.
instead of real :: foo = 1
. Is that trailing decimal point required?
And would these rules change when reading parameters from a namelist file? For example can I write in the namelist
&main_nml
foo = 1
/
instead of
&main_nml
foo = 1.
/
and read the variable in the original module with namelist /main_nml/ foo
?
Finally, does this answer change for different versions of Fortran?
Upvotes: 1
Views: 79
Reputation: 32366
When occuring in source code such as
real :: x=1
real :: y=1.
there is a difference at a lexical level. In the first case, 1
is a literal integer constant; in the second 1.
is a literal (default) real constant. That said, the integer 1
is converted to the type of x
as part of the initialization. It is highly likely that x
and y
will be initialized to the same value.
Both of these cases are legitimate declarations, so in that sense the decimal symbol is not required although there is a technical difference.
With namelist input things are slightly different. In the input files 1
and 1.
do not represent literal constants (not being in source code). Instead the value in the input is interpreted as the type of the variable to which it will be applied. In your case, assuming foo
is default real, then both 1
and 1.
will be considered default real values.
A real value in a namelist input is not required to have a decimal symbol. Without it the value has no fractional part. In this sense there is no difference between the presence and absence of the decimal symbol and the values will be the same.
Without exploring in depth, I am not aware of any changes to how these things are interpreted between revisions of the language.
This is a good time to note that things are very different when considering formatted (non-namelist) input. The absence of a decimal symbol can be very confusing with certain formats.
Upvotes: 3