short-circuit logical operators for Octave

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

short-circuit logical operators for Octave

John Eaton-4
[This was also posted to comp.soft-sys.matlab, but I'm also posting
here since some people on this list may not read that newsgroup. --jwe]

Based on the discussion of the past few days in comp.soft-sys.matlab,
I've implemented short-circuit logical operators for Octave.  An
experimental patch is available via anonymous ftp from
ftp.che.utexas.edu in the file /pub/octave/short-circuit-diffs.  It
seems to work, but I have not done extensive testing.

The changes alter the behavior of Octave's logical `&&' and `||'
operators so that they are no longer simply different forms of the
logical `&' and `|' operators.  Instead, they have the following
properties:

  * The value of the expressions formed with these operators is either
    1 (true) or 0 (false).

  * The operands can be matrices, but an implicit all(all()) is
    applied to each operand before it is used.

  * Evaluation is performed in a short-circuit fashion.

      a || b  returns true if either a or b is determined to be true,
              but only evaluates b if a is determined to be false.

      a && b  returns true if both a and b are determined to be true,
              but only evaluates b if a is determined to be true.

  * `&&' and `||' have the same precedence, now just below that
    of `&' and `|'.

The behavior of the logical `&' and `|' operators is not changed.

These changes will probably break some of Octave's functions as they
are currently implemented because they use only the `&&' and `||'
forms, but some of the functions may actually need to use the `&' and
`|' operators.  These problems will be fixed if this change is
adopted, but no patches are currently available.  If you would like to
volunteer to help, please contact [hidden email].

If you have other comments, suggestions, or bug reports, please send
them to [hidden email] as well.

Thanks,

--
Virtually all of the programs obtained by our sampling | John W. Eaton
procedure were written in FORTRAN.  -- Donald E. Knuth | [hidden email]