short circuit & and | in if and while

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

short circuit & and | in if and while

Ernst Reissner
Is there anyone who can explain me,

why in matlab, and thus in octave,

if(a&b) is interpreted like if(a&&b),

whereas c=a&b differs from c=a&&b

which means that

if(a&b) is not the same as c=a&b;if(c)...

Very strange.

This feature also applies to while but not to until...

Who can explain me???


Reply | Threaded
Open this post in threaded view
|

Re: short circuit & and | in if and while

Mike Miller-4
On Thu, Sep 14, 2017 at 02:15:16 +0200, Ernst Reissner wrote:

> Is there anyone who can explain me,
>
> why in matlab, and thus in octave,
>
> if(a&b) is interpreted like if(a&&b),
>
> whereas c=a&b differs from c=a&&b
>
> which means that
>
> if(a&b) is not the same as c=a&b;if(c)...
>
> Very strange.
>
> This feature also applies to while but not to until...
>
> Who can explain me???
Here is an official answer from Mathworks Support that describes the
behavior

  https://www.mathworks.com/matlabcentral/answers/99518-is-the-logical-operator-in-matlab-a-short-circuit-operator

This is a "what", not a "why". I don't know who you can ask for an
answer to "why", it just is.

--
mike

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: short circuit & and | in if and while

Ernst Reissner
Yes, Mike, I know the what, but not the why and I really cannot figure
out a reason.
The reason why I ask is among other things, what about until in octave:
Maybe short circuiting should be also for until, as it is for while and if:
while (false & error("both")) "hi";end shortcuts and thus no error while
do "hi" until(true |error("both")); yields and error.

This seems a little inconsistent,
but on the other hand, ... what sense makes the matlab feature at all?!

> On Thu, Sep 14, 2017 at 02:15:16 +0200, Ernst Reissner wrote:
>> Is there anyone who can explain me,
>>
>> why in matlab, and thus in octave,
>>
>> if(a&b) is interpreted like if(a&&b),
>>
>> whereas c=a&b differs from c=a&&b
>>
>> which means that
>>
>> if(a&b) is not the same as c=a&b;if(c)...
>>
>> Very strange.
>>
>> This feature also applies to while but not to until...
>>
>> Who can explain me???
> Here is an official answer from Mathworks Support that describes the
> behavior
>
>   https://www.mathworks.com/matlabcentral/answers/99518-is-the-logical-operator-in-matlab-a-short-circuit-operator
>
> This is a "what", not a "why". I don't know who you can ask for an
> answer to "why", it just is.
>


Reply | Threaded
Open this post in threaded view
|

Re: short circuit & and | in if and while

John W. Eaton
Administrator
On 09/13/2017 09:25 PM, Ernst Reissner wrote:
> Yes, Mike, I know the what, but not the why and I really cannot figure
> out a reason.
> The reason why I ask is among other things, what about until in octave:
> Maybe short circuiting should be also for until, as it is for while and if:
> while (false & error("both")) "hi";end shortcuts and thus no error while
> do "hi" until(true |error("both")); yields and error.
>
> This seems a little inconsistent,
> but on the other hand, ... what sense makes the matlab feature at all?!

It doesn't make much sense.

Have you read

   http://wiki.octave.org/FAQ#Short-circuit_.26_and_.7C_operators

Originally in Octave && was equivalent to &, and || was equivalent to |.
  Neither form did short-circuiting.

I changed the && and || operators (but not the & and | operators) to do
short-circuiting in 1994, apparently.  Here is the message I posted then:

   http://lists.gnu.org/archive/html/help-octave/1994-05/msg00012.html

When TMW introduced short-circuit behavior for the & and | operators in
Matlab, they thought they were being clever by not introducing new
operators, and just making the existing ones do something different in
some contexts.  To me, this seemed like a really bad choice and I could
immediately see cases where behavior would be different depending on
whether the expression appeared directly inside and IF/WHILE condition
or not.

The Matlab behavior is highly unusual and causes trouble in various ways
(some documented in the Octave FAQ).  We have it for compatibility ONLY.
  Using this feature is STRONGLY discouraged.  I wish to limit the
damage, so I have no interest in extending the behavior to other places
in the language.  Supporting this feature for compatibility is bad enough.

jwe


Reply | Threaded
Open this post in threaded view
|

Re: short circuit & and | in if and while

Ernst Reissner
On 09/14/2017 06:07 AM, John W. Eaton wrote:

> On 09/13/2017 09:25 PM, Ernst Reissner wrote:
>> Yes, Mike, I know the what, but not the why and I really cannot figure
>> out a reason.
>> The reason why I ask is among other things, what about until in octave:
>> Maybe short circuiting should be also for until, as it is for while
>> and if:
>> while (false & error("both")) "hi";end shortcuts and thus no error while
>> do "hi" until(true |error("both")); yields and error.
>>
>> This seems a little inconsistent,
>> but on the other hand, ... what sense makes the matlab feature at all?!
>
> It doesn't make much sense.
>
> Have you read
>
>   http://wiki.octave.org/FAQ#Short-circuit_.26_and_.7C_operators
>
> Originally in Octave && was equivalent to &, and || was equivalent to
> |.  Neither form did short-circuiting.
>
> I changed the && and || operators (but not the & and | operators) to
> do short-circuiting in 1994, apparently.  Here is the message I posted
> then:
>
>   http://lists.gnu.org/archive/html/help-octave/1994-05/msg00012.html
>
> When TMW introduced short-circuit behavior for the & and | operators
> in Matlab, they thought they were being clever by not introducing new
> operators, and just making the existing ones do something different in
> some contexts.  To me, this seemed like a really bad choice and I
> could immediately see cases where behavior would be different
> depending on whether the expression appeared directly inside and
> IF/WHILE condition or not.
>
> The Matlab behavior is highly unusual and causes trouble in various
> ways (some documented in the Octave FAQ).  We have it for
> compatibility ONLY.  Using this feature is STRONGLY discouraged.  I
> wish to limit the damage, so I have no interest in extending the
> behavior to other places in the language.  Supporting this feature for
> compatibility is bad enough.
>
> jwe
>
>
ok so in short: there is nothing to be understood; it is just a bad
design choice of TMW.

This explains also why the operators | and & in conditions in until
statements are not shortcut,

i.e. implicitly replaced by && and ||: because until does not exist in
matlab.

Maybe good idea to document this....


Reply | Threaded
Open this post in threaded view
|

Re: short circuit & and | in if and while

John W. Eaton
Administrator
On 09/14/2017 05:11 AM, Ernst Reissner wrote:

> This explains also why the operators | and & in conditions in until
> statements are not shortcut,
>
> i.e. implicitly replaced by && and ||: because until does not exist in
> matlab.

The short-circuit behavior of | and & inside IF and WHILE conditions is
not identical to the short-circuit behavior of || and &&.

With || and &&, the operands are evaluated and determined whether they
are true or false.  In Octave, this means that if an operand is an array
(for example) it is considered to true if all of the elements are
non-zero.  In Matlab, I believe it is an error if the operands are not
scalar values.

In the | and & expressions inside an IF or WHILE condition, short
circuiting can only happen if the first argument is a scalar value.  It
might seem that these are equivalent, at least in Matlab, but consider
this case:

   if (1 | x) ...; end

vs.

   cond = 1 | x;
   if (cond) ...; end

Are these always equivalent?  Unfortunately no, because in the first
case, the operator always short-circuits because the first operand is a
scalar and is true, but in the second case, short-circuiting doesn't
happen and if X is an empty matrix, then cond will also be an empty
matrix and in the IF condition it is considered false.

It seems to me that because of this bizarre behavior, it's best to NEVER
use | or & inside IF or WHILE conditions, and only use them as
element-wise array operators.

Note that using || instead, there is no difference in behavior, so

   if (1 || x) ...; end

and

   cond = 1 || x;
   if (cond) ...; end

should always do the same thing, regardless of the value of X.

jwe