help-octave
[Top][All Lists]

## Re: precedence

 From: Paul Kienzle Subject: Re: precedence Date: Tue, 6 Aug 2002 16:11:47 -0400

```On Tue, Aug 06, 2002 at 12:40:23PM -0500, John W. Eaton wrote:
> The documentation for Matlab on the MathWorks web site now lists
> operator precedence.  It may have been there for a while now, but this
> is the first time I remember seeing it.
>
> Here is their list:
>
>    1. Parentheses ()
>    2. Transpose (.'), power (.^), complex conjugate transpose ('),
>       matrix power (^)
>    3. Unary plus (+), unary minus (-), logical negation (~)
>    4. Multiplication (.*), right division (./), left division(.\),
>       matrix multiplication (*), matrix right division (/),
>       matrix left division (\)
>    5. Addition (+), subtraction (-)
>    6. Colon operator (:)
>    7. Less than (<), less than or equal to (<=), greater than (>),
>       greater than or equal to (>=), equal to (==), not equal to (~=)
>    8. Element-wise AND (&)
>    9. Element-wise OR (|)
>   10. Short-circuit AND (&&)
>   11. Short-circuit OR (||)
>
> Ignoring the indexing operators '.' and '{' (not sure why they were
> omitted from the Matlab list), the separator operators (';', ',',
> '\n'), the LSHIFT (<<), RSHIFT (>>), increment (++) and decrement (--)
> operators (Matlab does not have these), and all the assignment
> operators (in Matlab, assignment is not an expression), Octave's
> precedence list is:
>
>    1. Parentheses '('
>    2. Power (.^), matrix power (.^)
>    3. Unary plus (+), Unary minus (-), logical negation (~)
>    4. Transpose (.'), complex conjugate transpose ('),

Really?  So why does A^x' equal A^(x') in Octave 2.1.35?

>    5. Multiplication (.*), right division (./), left division(.\),
>       matrix multiplication (*), matrix right division (/),
>       matrix left division (\)
>    6. Addition (+), subtraction (-)
>    7. Colon operator (:)
>    8. Less than (<), less than or equal to (<=), greater than (>),
>       greater than or equal to (>=), equal to (==), not equal to (~=)
>    9. Element-wise AND (&), Element-wise OR (|)
>   10. Short-circuit AND (&&), Short-circuit OR (||)
>
> (Note that in the latest release, they have have finally introduced
> the || and && operators to do short-circuiting instead of relying on
> whether the | and & operators appear in if/while conditions to decide
> whether to short-circuit.)
>
> Differences:
>
>   * In Matlab, transpose and power have the same precedence, higher
>     than unary +/- and negation.

Transpose and unary negation are associative, so no problem switching
the precedence of those.

I would rather have A^x' equal (A^x)' than the current octave behaviour of
A^x' equal A^(x').

>
>   * In Matlab, element-wise AND (&) has higher precedence than
>     element-wise OR (|), in Octave they have the same precedence.
>
>   * In Matlab, short-circuit AND (&&) has higher precedence than
>     short-circuit OR (||), in Octave they have the same precedence.

I use parentheses when mixing "and" and "or" in an expression so
that I don't have to think about precedence, so this won't affect me.

I believe matlab's precedence corresponds to the usual convention
in logic so I suppose it is a better choice.

You might consider warning users if they have mixed "and" and "or" in the
same expression.

>
> How much trouble would it cause if Octave were changed to match the
> Matlab precedence rules?

Paul Kienzle

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

```