Re: question about initializing variables to 0



On May 3, 8:29�pm, "jamesgi...@xxxxxxx" <jamesgi...@xxxxxxx> wrote:
On May 3, 1:11 pm, Randall Mackie <rlmackie...@xxxxxxxxx> wrote:





Maybe this is a question of style more than substance, but when initializing
variables to zero, does it matter if you use:

        x = 0

or

        x = 0.0  (for real)

or

        x = 0.d0 (for double real)

or

        x = cmplx(0.d0, 0.d0, kind=r8)  (for example)

Does the first one (x = 0) always work regardless of how x is declared?

Yes.  Some purists would insist that you use the correct
type of literal regardless.  If the value were, say, one-tenth
it would make a difference which kind of literal you used.
By always using a matching kind (both type and KIND) of
literal, you have a habit which is perhaps safer.  I have no
strong opinion either way in Fortran.  I'd would prefer that
it not matter though.  I think Fortran is misdesigned in that
respect.

I do have one change to your above though.  When I have
any COMPLEX data at all, I always USE a module that defines
a generic operator for COMPLEX literals called .I.  Using
that, the last assignment would read:

   X = 0.d0 +.i. 0.d0

I don't use the CMPLX intrinsic at all anymore.

--
J. Giles

"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies."   --  C. A. R. Hoare- Hide quoted text -

- Show quoted text -

I respectfully disagree about your non use of the intrinsic CMPLX.

The problem is not how the code looks to you, but how it looks to a
future maintenance programmer.

When there is a choice between two ways of writing code, other things
being equal, the more standard one should be chosen.

David Flower

.



Relevant Pages

  • Re: newbie: self.member syntax seems /really/ annoying
    ... It's a matter of taste, so there is no point in bashing a valid ... It's not just a matter of taste. ... they can use any conventions they feel like. ... But if you're writing code that is going to be seen by others in the ...
    (comp.lang.python)
  • Re: Single Instance Limit
    ... It's a matter of writing code to handle it. ... Quite simply, when your app starts (in Sub Main or the Load event of a form, whichever you have set as your startup object), check App.PrevInstance. ...
    (microsoft.public.vb.general.discussion)
  • Re: question about initializing variables to 0
    ... strong opinion either way in Fortran. ... it not matter though. ... any COMPLEX data at all, I always USE a module that defines ... no deficiencies and the other way is to make it so complicated ...
    (comp.lang.fortran)
  • Re: question about initializing variables to 0
    ... strong opinion either way in Fortran. ... it not matter though. ... any COMPLEX data at all, I always USE a module that defines ... no deficiencies and the other way is to make it so complicated ...
    (comp.lang.fortran)
  • Re: Should I migrate from C to C++?
    ... >> It isn't a matter of winning, but it is a matter of learning what is and ... >> is not guaranteed by the language in question. ... We ordinary users just can't ... that's what you get for writing code that's specifically ...
    (alt.comp.lang.learn.c-cpp)