Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion binary data
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Tobias Burnus  
View profile  
 More options Sep 1 2012, 4:49 am
Newsgroups: comp.lang.fortran
From: Tobias Burnus <bur...@net-b.de>
Date: Sat, 01 Sep 2012 10:49:06 +0200
Local: Sat, Sep 1 2012 4:49 am
Subject: Re: binary data

James Van Buskirk wrote:
> As i have had occasion to point out previously, if the compiler
> allows free mixing of logical and integer types, then, assuming that
> 0 is the canonical .FALSE. value and 1 is a .TRUE. value:

But you cannot assume that .false. is 0 and .true. is 1. I know at least
one compiler were .true. is -1.

> if x is .TRUE., then 1 .AND. x must be .TRUE. as well, so the
> low bit of x must be set.  Similarly, if x is .FALSE. then
> 1 .AND. x must be .FALSE., so the low bit of x must be clear.

Well, you assume some bitwide handling of
   <logical-expr> .AND. <integer-expr>
where one then looks afterwards only at a single bit (i.e. treats it as
logical). However, the compiler is not necessarily the case. For
instance, in C99 it is not possible as the resulting expression is "int"
and if you do "if(<int-expr>)" you may not look at a single bit.

By contrast, a Fortran compiler is completely free to handle it as it
wants, but technically there is no reason which rules out that
   if (2 .or. .false.)
has the bit value of 2, 1 or -1. Some choices make more sense in
different contexts, but all are possible.

> Also .NOT. 0 is -1, so the canonical value of .TRUE. must be -1.
> Now, this could be consistent with the C _Bool type provided the
> C compiler considered only the low bit of an _Bool to contribute
> to its value and set the high bits when creating the true _Bool
> value.  Unfortunately C compilers paradoxically examine all bits
> of an _Bool when determining whether it is false (all bits zero)
> or true (anything else).

I have to admit that I do not understand this part. But I disagree with
several items:

I don't see why .NOT. 0 should be -1. You could also flip only the other
bit such that .NOT. 0 is 1.

Of course, that assumes that you either always look only at the single
bit - for instance because you know that the variable only has the two
values.

That assumption you can do in Fortran for a chosen implementation.
(That's independent whether you support integer expressions in logical
expressions; the implementation itself then depends on this choice.)

But that assumption you can also do in C as you know that _Bool only has
two values: 0 and 1.

The reason that C compilers often look at more than a single bit is that
most "Boolean" operators return and "int", e.g.

"The result of the logical negation operator ! is 0 if the value of its
operand compares unequal to 0, 1 if the value of its operand compares
equal to 0. The result has type int. The expression !E is equivalent to
(0==E)." (C99, 6.5.3.3)

But if the compiler knows that "E" is always either 0 or 1, it can flip
a single bit. But that's actually the case for C99's Boolean type (6.3.1.2):

"When any scalar value is converted to _Bool, the result is 0 if the
value compares equal to 0; otherwise, the result is 1."

I think for conditions, it doesn't matter that much whether one checks
whether "%register is zero", however, for ".not.logical_var", flipping a
single bit is advantageous.

> The C compiler could follow the extended
> Fortran behavior and still conform to the standard, but this choice
> is never made, so any Fortran compiler that permits mixing of real
> and logical expressions must set C_BOOL to -1

I am lost again. Why should a compiler set C_Bool to -1? It just has to
comply to C99 by ensuring that only 0 and 1 are passed to the register.
Or alternatively, the same-vendor C compiler has to handle it in the
same way. My impression is that this works rather well.

However, it doesn't work well if one mixes compilers of two vendors. If
you take a compiler which returns -1 for .true. and another one which
flips the lower bit, then ".not. true()" will be "-2" which might be
regarded as .true.

That's actually a real-world problem. But I still maintain that this is
independent of supporting integers as logical. For instance, both
compilers do support "logical b; b = 2".

Tobias


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.