bugGNU Octave - Bugs: bug #54491, Order of evaluation of power with...

 
 

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

Submitter:  None
Submitted:  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 Fixed Release:  None
Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

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>
Group administrator
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>
Group administrator
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>
Group Member
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>
Group Member
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

 

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

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

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

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    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 / Not an Octave Bug Confirmed
        Open/ClosedClosed Open
        Release4.4.0 dev
    2018-08-11 philipnienhuis StatusNone Invalid / Not an Octave Bug
        Open/ClosedOpen Closed

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code