2

And is the following code well defined?

print_factor(p(++k), p(--k));

And how do things look like in octave?

Amro
  • 123,847
  • 25
  • 243
  • 454
not-a-user
  • 4,088
  • 3
  • 21
  • 37

1 Answers1

3

Do not do this! The code is valid in both MATLAB and Octave, but behaves very differently.


MATLAB:

The code you have is actually valid MATLAB code. However, it doesn't do what you expect.

In many languages, ++k means increment k and return it. In MATLAB, ++k is just the same as k, because in MATLAB: 3 == +3 == ++3 == +++++++3. The same goes for --k. This is --k == -(-k) == +k == k. Similarly, in many languages k++ means return k, then increment it. In MATLAB however, are k++ and k-- not valid syntax and causes syntax error.

Your code is (in MATLAB) equivalent to:

print_factor(p(k), p(k));

Testing the code in MATLAB with two example functions for p and print_factor:

p = @(x) 2*x;
print_factor = @(x,y)disp([x,y]);
k = 2;
print_factor(p(++k), p(--k));
 4     4
k    
k =    
     2

Octave:

In Octave, the operators are defined, and are evaluated left to right, but I don't think the official documentation regarding the increment operators says anything about this. It might be platform dependent, as it is in C++. However, it's likely interpreted in the same way other expressions are evaluated, see the example in the bottom.

You can test it online here.

The exact same code in Octave:

p = @(x) 2*x;
print_factor = @(x,y)disp([x,y]);
k = 2;

print_factor(p(++k), p(--k));
   6   4
k
k =  2

print_factor(p(++k), p(++k));
   6   8
k
k =  4    

As Dan commented: Writing code in Octave that doesn't follow the MATLAB syntax is not a good idea. You will have to rewrite it in case you ever want to run it in MATLAB.

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
  • I wouldn't say this being a valid code but a parser bug that cannot raise an error. – percusse May 23 '16 at 13:36
  • 1
    Huh? In MATLAB? Why? Would you consider: `k = -3; p(-k);` invalid too? – Stewie Griffin May 23 '16 at 13:53
  • I mean this being `p(++++++k)` valid is just wrong. – percusse May 23 '16 at 14:03
  • This is not strange at all. The `-` operator is an unary operator when it is used in front of a variable. That would mean that `---3 == -(--3) == -(-(-3)) == -(-(-(3)))`. In case the JIT (or this might actually be done in the static code analysis?) can understand this it will operate on the first expression, which returns an r-value, which the second unary minus operates on, ... and so on. Perfectly natural :). – patrik May 23 '16 at 14:26
  • @patrik That's just too much faith on the parser. That's just wrong coding. That would be OKish if matlab had an in-place replacement parsing habit and expanded all the nested groups. But having this at the high-level is just adding lipstick. Note that it doesn't do the same thing with `k-----` which has the same mentality – percusse May 23 '16 at 14:44
  • @percusse you must never mix up `k---` with `---k`. This is **not** the same. While `-k` is an unary operator and `k-` is a binary operator. You see a lot of examples this in c++ as well. You may want to test `++++i` in c++. I have never tried it but I would guess it will work. That should be the same as `++(++i)`. You se other examples [here](http://stackoverflow.com/questions/20456546/how-to-determine-i-j-in-c) and [here](http://stackoverflow.com/questions/5236706/what-is-i-increment-in-c). – patrik May 24 '16 at 08:29
  • @patrik You should also not mix syntax with function! operators are defined at the parser level and does not relate to anything low level. YOU define it what it should do. Google pre- and post- incremental syntaxes. Repeated signs are just nonsensical for my taste. Math is something programming is another. – percusse May 24 '16 at 11:43
  • @percusse Do you have a reference on this? I have never heard that multiple signs would be forbidden in maths. Notations like -(-6) is commonly used. Normally paranthesis are used to clarify precedence, but a computer does not need that (though it is still allowed), -(-(-(6))). A program for maths should not disallow permitted operations. An operation never used may not be implemented due to time issues, but typically you will not spend time preventing completely legal operations. Example of operations, http://math.stackexchange.com/questions/1393314/what-is-the-value-of-6/1393326#1393326 – patrik May 26 '16 at 07:00
  • @patrik It is of course not forbidden in maths. But also considered to be a very very bad practice without parens because it is not that minus sign cancels minus sign it is a recursive parsing until you find an entity with a sign in our case `i` or `k` because the grouping (or nesting) is implicit. And that implicit part can be anything in programming. As I mentioned you can group post increments in double ++ or -- etc. Programming is a different interaction with computers and matlab is one of the least innocent ones in that context. Unaryness and binaryness of an operator is just syntactic. – percusse May 27 '16 at 19:08