On Friday, May 14, 2021 at 9:50:29 PM UTC-7, jfh wrote:
> On Saturday, May 15, 2021 at 3:11:56 PM UTC+12, Beliavsky wrote:
> > Why does Fortran require .eqv. to test two logical variables for equality
> > instead of .eq. or == as for other basic types?
> Because .eqv. and .eq. have different precedences, another relevant question
> is why they were chosen to be what they are.
> You can overload == for logical variables of course, but your version
> of == will have a precedence different from either .eqv. or the non-logical == .
There is a story about how the bitwise C operators, & and |, got their low
precedence, as the && and || logical operators were added late.
The .NEQV. and .EQV. were added much later, as they are less needed.
These are the operators that other languages call exclusive-or and
(usually) exclusive-nor. (The latter more rarely seen.)
They are rarely enough used that even C doesn't have them.
OK, C doesn't have logical variables, but uses integer values that
are 0 (false) or non-zero (true). There are, then, the && and || operators
that work based on those values. But the relational operators always
give 0 or 1, so you can use the bitwise exclusive or operator if needed.
Otherwise, you can use ! (the .NOT. operator) to convert to a 0 or 1:
if((!a)^(!b)) ...
or
if((!a)==(!b)) ...
Java, which has boolean variables, uses == and != for them.
Just to be sure, do note that you don't use them for testing the value
of a logical variable. You don't say:
if(V.EQV..TRUE.) ...
or
if(V.NEQV..FALSE.) ...
but instead just:
if(V) ...
Note one funny thing in Java.
C uses the == operator as a relational operator, but allows use of the
assignment operator (=) in expressions. A not so rare bug in C comes
from writing
if(x=y) ...
when you meant:
if(x==y) ...
where the former assigns y to x, and then tests its value.
In Java, if() requires a boolean expression, so you can't get away
with if(x=y), except in the case that x and y are boolean variables.
I suspect, though, that it is rare enough that it doesn't cause problems.
Do you have an actual use for .EQV. or .NEQV. in Fortran?