Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Initialization of scalar or arrays

netphilou31
New Contributor II
918 Views

Dear all,

I have a question that could look strange at a first glance.
I am frequently reviewing source code written by other developers and I was just wondering why they initialize very often scalars and arrays or define constants in this way:

[fortran]a_scalar = 0.0d0

another_scalar = 2.0d0

array(:) = 0.0d0

[/fortran]

What I am curious to know is, why they add an additional 0 just after the decimal separator or more precisely just before the d0. Is there anything special behind the scene for doing this or it is just a way of writing the code. For me this has just no meaning and makes sometimes the source code less readable. Am I right or is there something special behind this ?

Best regards.
Phil.

0 Kudos
9 Replies
andrew_4619
Honored Contributor III
918 Views

I do not think there is any significance, perhaps others will know of some. I always add the extra 0 because personally I think it looks wrong if I do not, and I also find it is more readable.

0 Kudos
TimP
Honored Contributor III
918 Views

Fortran accepts a variety with identical results.  Even the decimal point is optional.

It's considered good form to specify the data type of constants, e.g. 0d0 (old way) or

integer, parameter :: dpk = selected_real_kind(12)

....  0_dpk ....

Although in such cases where single and double precision have identical value, you don't need to specify precision.

0 Kudos
JVanB
Valued Contributor II
918 Views

Ada requires a digit on both sides of the deicmal point, so this is considered good style by some and enforced by Ada compilers.  There are those who consider d0 to be bad style, preferring to qualify as 1.0_dp, so the kind in use could be upgraded to quad precision easily.  There are also arguments for writing 1 when you mean 1, rather than 1.0d0 or 1.0_dp, you just have to be careful about integer division and overflow when you do that.

0 Kudos
Steven_L_Intel1
Employee
918 Views

Regarding:

[fortran]
array(:) = 0.0d0
[/fortran]

I dislike the superfluous use of (:) here - it isn't necessary and will prevent automatic reallocation of allocatable arrays.

0 Kudos
netphilou31
New Contributor II
918 Views

Hi

Thanks for your comments and advices.

To Steve, can you clarify the last part of your sentence  : 

...prevent automatic reallocation of allocatable arrays.

What do you mean exactly?

Personnaly, I prefer to use the (:) notation in order to indicate that the variable being initialized is an array and not a simple scalar. I think that this is more readable for a developer who is looking at the source code for the first time.

Best regards

Phil.

0 Kudos
DavidWhite
Valued Contributor II
918 Views

Regarding 0.0d0, not only is the 0 not required after the decimal point, but the decimal point itself is superfluous.  I normally use 0d0.

About the comment regarding using 1.0_dp, etc, I would be interested to know whether there is "a" preferred way to define kind for double precision variables, both for consistency with the traditional default type, but also to future proof code.

Regards,

David

0 Kudos
qolin
Novice
918 Views

 

1.0d0 and 1d0 are indeed equivalent. But what about 1.0_dp and 1_dp ???

 

0 Kudos
Steven_L_Intel1
Employee
918 Views

Phil,

As of Fortran 2003, if you have an allocatable array A and have:

A = <some expression>

then if A is not already allocated to the shape of <some expression>, it gets (re)allocated to that shape. (In Intel Fortran you need to compile with /standard-semantics or /assume:realloc_lhs to get this behavior.)

But if you write instead:

A(:) = <some expression>

you don't get this behavior. Also, it is possible that in some circumstances the compiler might miss optimizations if you use (:) unnecessarily.

qolin,

1.0_dp and 1_dp are not equivalent - the latter is an integer of whatever kind dp happens to be.

0 Kudos
netphilou31
New Contributor II
918 Views

Steve,

Many thanks for the clarification.

Hopefully, I do not use this particularity of the standard 2003, I use it only for initializing to zero a newly allocated array.

Phil.

0 Kudos
Reply