For Programmers: Free Programming Magazines  


Home > Archive > Fortran > November 2005 > Re: printing logical variables as "true" and "false"









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: printing logical variables as "true" and "false"
Richard E Maine

2005-11-29, 7:01 pm

Nestor Grion <ngrion@gmail.com> wrote:
[on the conversion of logical to integer]

> merge(1,0,logical_var)


That would work.

> Two questions. Why standar does not establish an implicit conversion?
> This does not diminish the utility of the logical variables (integers
> are used)?


Such "why" questions are largely imponderable. Allow me to suggest that
some people (lots of them, even) don't find it obvious that there is any
relationship between logicals and integers. Or, if there is a
relationship, what the "intuitive" one is.

I challenge you (hypothetically, not seriously) to answer with "zero" or
"one" next time some "ordinary person" (us computer gs are
disqualified) asks you a true or false question. Consider whether they
would find the meaning intuitively obvious. I'd posit that if they
understand the meaning, you accidentally found a computer g. :-)

Why is false the same as 0 and true the same as 1? Or did I get it
backwards? Why not 1 and 2? This is, after all, Fortran and not C.
Arrays are by default 1-based (and prior to f77, you couldn't even
override that default). Or why not +1 and -1? Oh, I can see some logic
to 0 and 1, but I also see logic to the other choices. In the standards
business, it is important to have enough perspective to see multiple
points of view. (Or failing that, to have other people on the committee
who will point out the other viewpoints even if you fail to see them).

So I'd turn around the question and ask why one would think it natural
to have such a relation between integer and logical, and to have the
standard force same version of the relation on all users? Are you next
going to suggest that, of course, characters are really the same thing
as integers... and that the Fortran standard should specify that all
computers use the same character encoding? Seems pretty comparable to
me.

Realize that as of f90 you *CAN* define such a conversion youself and
have it work just like one that was defined by the language, so that,
for example the assignments

logical = integer
integer = logical

will do whatever you want them to.

But those matters aside, I think that the original reasons are actually
simpler to understand than that. The Fortran standard was not written in
an attempt to force particular hardware designs. Quite the opposite, the
Fortran standard was very intentionally written to facilitate writing
portable code that would run on a wide variety of hardware. Certainly in
the early days of Fortran, different hardware architectures had
different internal coding schemes that were more natural and efficient.
requiring particular choices of internal representation for logicals
would have caused performance penalties on some hardware. Performance
has traditionally been a significant issue in Fortran.

--
Richard Maine | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov | -- Mark Twain
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com