Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contatenation of binary operators like "3 + + 2" in Matlab does not give errors

If I evaluate this expression:

3 + + 2

or also

3 + + + + + + 2

I obtain 5 as the result.

If I also insert the - operator I also obtain a result (1 in this case):

3 + - + - + - 2

I was thinking that the space between operators can be considered as zero, but if I use the times operator, I instead obtain an error:

3 * * 2 % Error: Unexpected MATLAB operator.

What's happening? What's the meaning of this syntax and why does it work with + and - but not with *?

Why this is valid syntax?

I'm using Matlab R2014a.

like image 250
Jepessen Avatar asked Jan 08 '18 12:01

Jepessen


1 Answers

+ and - can be binary or unary operators. * Can only be binary.

In your code, all + and - symbols after the first are probably being parsed as unary operators. So 3 + - 2 is interpreted as 3 + (-2) (the + is binary, the - is unary). Similarly, 3 - + - 2 is interpreted as 3 - (+-2), that is, 3 - (-2).

That doesn't work with * because it cannot be a unary operator.

like image 87
Luis Mendo Avatar answered Nov 08 '22 16:11

Luis Mendo