octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Website Updates


From: John W. Eaton
Subject: Re: Website Updates
Date: Thu, 9 Feb 2006 20:46:11 -0500

On  9-Feb-2006, Tom Holroyd (NIH/NIMH) [E] wrote:

| Then I could change where it says:
| 
|     * For a logical-or, Octave can use `|' or `||'; Matlab requires
| `|'. (note: Octave's '||' and '&&' return a scalar, '|' and '&'
| return matrices)
| 
| (found on http://wiki.octave.org/wiki.pl?MatlabOctaveCompatibility)
| 
| to this:
| 
|     * In Octave, the '|' operator does not shortcut, while '||'
| does; this is the same as the behavior in C.  In Matlab, both the
| '|' and the '||' operators shortcut.  Thus an expression like "f(a)
| | g(b)" will cause both f() and g() to run in Octave, but in Matlab
| g() won't run if f(a) returns true.
| 
| That old description is just wrong.  Not sure about the note.

The new description is not quite right either.  In Matlab, | and &
only short-circuit in the condition of an IF or WHILE statement, and
(I think) only if the first operand is a scalar.  So you can get
different behavior for

  t = f () | g ();
  if (t)
    ...
  end

vs

  if (f () | g ())
    ...
  end

if either F or G have side effects.  Also, there are some problems
with empty matrices.  I think this is what finally prompted the
MathWorks to introduce the && and || operators.  See for example the
comp.soft-sys.matlab thread in September of 2000 with the subject
"if (1 | []) : inconsistencies?" (http://tinyurl.com/7cuyl).

In Octave, && (||) was originally equivalent to & (|).  But when the
MathWorks decided to change the behavior of | and & so that they would
short-circuit (but only in the conditional part of IF and WHILE
statements) I decided that the operators should have separate meanings
to avoid the inconsistency.  There was a comp.soft-sys.matlab thread
with the subject "&,| aren't short-circuit operators"
(http://tinyurl.com/cbj6n).  It's interesting to see that the
MathWorks did not seem to care about the possible inconsistencies,
even when they were pointed out.  I introduced the new behavior for &&
and || in Octave during that discussion and announced the change to the
help-octave list: http://www.octave.org/mailing-lists/help-octave/1994/87.  

There was also another thread that touched on this topic right around
the time of the release of Matlab 5.0 in October of 1995 with the
subject "5.0 Preview at MATLAB Conference (long)"
(http://tinyurl.com/c4ohe).

jwe



reply via email to

[Prev in Thread] Current Thread [Next in Thread]