Instable evaluation of combined logical expressions

David Bateman dbateman at dbateman.org
Fri Feb 13 11:26:59 CST 2009


John W. Eaton wrote:
> On 13-Feb-2009, Jörg Bretschneider wrote:
>
> | Bug report for Octave 3.0.3 configured for i686-pc-msdosmsvc
> | 
> | Description:
> | -----------
> | 
> | Octave seems to evaluate all expressions in combined logical condition
> | expressions,
> | i.e.    cond1 LOGICALOPERATOR cond2 (LOGICALOPERATOR ...)
> | even if the logical result of the condition is already determined by the
> | first condition
> | An error occurs if the syntactical validity of the second or further
> | conditions depends
> | on a FALSE value of the first condition, e.g. an index request into an
> | empty matrix.
> | It is not unusual to check the length of an array first in order to
> | request a condition
> | on some of its elements. The example works with Matlab for years, but
> | not Octave 3.0.x
> | 
> | Repeat-By:
> | ---------
> | 
> |   x=[];   if ~isempty(x)   &   x(1) ~= 0,   disp('do something'),   end
> | 
> | Fix:
> | ---
> | 	A work around for this was to pose multiple separate IF conditions,
> | 	but this is not the preferred way, as octave can easily understand
> | 	that an AND expression is FALSE if its first condition is, as well
> | 	as an OR expression is TRUE if its first condition is, there may be
> | 	other instances but the first one is the most important case.
> | 	I had to adapt hundreds of lines of M-code to make it compatible.
>
> We consider the way that | and & short-circuit to be a bug in Matlab,
> because they only short-circuit in some instances (IF and WHILE
> conditions) and the behavior is different if you do
>
>   t = a & b;
>   if (t) ...
>
> vs
>
>   if (a & b) ...
>
> A better solution for you is to use the && and || operators which
> always do short-circuiting.
>
> jwe
>
>   
A even better solution would have been to read the FAQ

http://www.gnu.org/software/octave/FAQ.html#MATLAB-compatibility

before posting

D.


-- 
David Bateman                                dbateman at dbateman.org
35 rue Gambetta                              +33 1 46 04 02 18 (Home)
92100 Boulogne-Billancourt FRANCE            +33 6 72 01 06 33 (Mob)



More information about the Bug-octave mailing list