- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
1.0d0 and 1d0 are indeed equivalent. But what about 1.0_dp and 1_dp ???
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page