To initialize and assign value to arrays in Fortran we do as the following:
Initializing:
real(kind=8):: r(3,4) ... r(:,:) = 0.0_8
what if we use only
real(kind=8):: r(3,4) ... r = 0.0_8
and what if we do as:
real(kind=8):: r(3,4) ... r = 0
also for situation such as:
real(kind=8):: r(3,4), q(3,4), p(30,40) ... q = 0 r = q r = p(1:3,21:24)
we prefer to do as:
real(kind=8):: r(3,4), q(3,4), p(30,40) ... q = 0.0_8 r(:,:) = q(:,:) r(:,:) = p(1:3,21:24)
we are not sure so hope you provide us some reasons for each one you prefer.
Do you think that “kind=8” means 8 bytes? That isn’t true for all compilers. The maximum portability is achieved using the selected_real_kind intrinsic to define a kind value:
Of some of the options that you list, I prefer
r = 0.0_8, or betterr=0.0_DRK.This defines the entire array. There is no need to designate array sections in this case, since you are calling out the entire array: r (:, :). Steve Lionel has a discussion of why trying to make arrays obvious with “:” isn’t a good idea — there are differences between array and array (:). As an argument, the first has it declared dimensions, while the second always begins at 1 — see http://software.intel.com/en-us/blogs/2008/03/31/doctor-it-hurts-when-i-do-this/
Since r is real, r=0 implies a conversion. For the value zero this almost certainly doesn’t matter, but it could make an important difference with other values. e.g.,
and
will give different values to
rbecause the constants are different, the first being converted to single precision before the assignment.