# bug #54491: Order of evaluation of power with unitary operators

 Submitted by: None Submitted on: Sat 11 Aug 2018 06:35:01 AM UTC Category: Interpreter Severity: 3 - Normal Priority: 3 - Low Item Group: WTF, Matlab?!? Status: Confirmed Assigned to: None Originator Name: rahnema1 Originator Email: -email is unavailable- Open/Closed: Open Release: dev Operating System: GNU/Linux

## Post a Comment

Add a New Comment

You are not logged in

Please log in, so followups can be emailed to you.

## Discussion

Tue 28 Aug 2018 12:56:50 AM UTC, comment #5:

Marking as a low priority.  I can't see the use case for this.

Rik <rik5>
Mon 13 Aug 2018 06:54:20 PM UTC, comment #4:

According to https://www.mathworks.com/help/matlab/matlab_prog/operator-precedence.html, it seems that these compound power are unary +/- operators are given some special treatment.  I have no idea why.  It means that an expression like

2^+2^+3^+4 == (2^+(2^+3))^+4 == (2^+8)^+4 == 256^+4 == 4294967296

will produce a different result from

2^2^3^4 == ((2^2)^3)^4 == (4^3)^4 == 64^4 == 16777216

I don't see these compound operators explained in the list of operators in the Matlab docs.  WTF?  I don't understand why this behavior would be desirable.

Octave doesn't have these as separate operators (and I suspect that Matlab doesn't either).  But when used this way in Octave, the evaluation is the same as for the power operator without the unary operator (left to right).

John W. Eaton <jwe>
Sat 11 Aug 2018 09:15:53 PM UTC, comment #3:

Copying the first 4 line into an .m function "bug54491a" (replacing function name "test" as that's an Octave function by "bug54491a") and then:

>> bug54491a
>> global a;
a = 1;
z=[7 8]
z =
7     8
>> z(a) ^=- bug54491a()
z(a) ^=- bug54491a()
↑
Error: Incorrect use of '=' operator. To assign a value to a variable, use '='. To compare
values for equality, use '=='.

Please get yourself behind a PC with Matlab and report what Matlab does (use 'verbatim' tags, see site help to the left) before claiming Octave does it wrong.

Closing as invalid.

Philip Nienhuis <philipnienhuis>
Sat 11 Aug 2018 07:10:21 PM UTC, comment #2:

function out = test()
global a;
a = 2;
end
global a;
a = 1;
z=[7 8]
z(a) ^=- test();

I don't have MATLAB but it should work:

function out = test()
global a;
a = 2;
end
global a;
a = 1;
z=[7 8]
z(a) ^- test();
a

Evaluation order from the left to right  prints 2 the order from the right to left it prints 1.
I think precedence, associativity and order of evaluation should be exactly defined so a programmer using that rules knows how to program with the programming language.

Anonymous
Sat 11 Aug 2018 06:25:47 PM UTC, comment #1:

Matlab r2018b prerelease;

>> z = 3;
>> out = z(a=1)^-(a=2);
out = z(a=1)^-(a=2);
↑
Error: Incorrect use of '=' operator. To assign a value to a variable, use '='. To compare
values for equality, use '=='.

This bug report isn't a strong story ... :-)

Philip Nienhuis <philipnienhuis>
Sat 11 Aug 2018 06:35:01 AM UTC, original submission:

According to the MATLAB documentation the operators (^-), (.^-), (^+), (.^+), (^~), and (.^~) work from second from the right to left but in Octave they are evaluated from the left to right.

Test case:

z = 3;
out = z(a=1)^-(a=2);
out = z(b=1).^-(b=2);
out = z(c=1)^+(c=2);
out = z(d=1).^+(d=2);
out = z(ee=1)^~(ee=2);
out = z(f=1).^~(f=2);

disp([a b c d ee f])

#shows [2 2 2 2 2 2] but it should produce [1 1 1 1 1 1]

Anonymous

## Attached Files

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

Comment:

No files currently attached

## Dependencies

Depends on the following items: None found

Items that depend on this one: None found

## Mail Notification Carbon-Copy List

Carbon-Copy List
• -email is unavailable- added by rik5 (Posted a comment)
• -email is unavailable- added by jwe (Posted a comment)
•

## Votes

Do you think this task is very important?
If so, you can add your encouragement to it.
This task has 0 encouragements so far.

Only project members can vote.

## History

Follow 7 latest changes.

Date Changed by Updated Field Previous Value => Replaced by
2018-08-28 rik5 Priority5 - Normal 3 - Low
2018-08-13 jwe Item GroupMatlab Compatibility WTF, Matlab?!?
StatusInvalid Confirmed
Open/ClosedClosed Open
Release4.4.0 dev
2018-08-11 philipnienhuis StatusNone Invalid
Open/ClosedOpen Closed

Powered by Savane 3.5