0

Why is this a valid MATLAB query?

3++4

which evaluates to 7. Even more disturbing:

3+-5

evaluates to -2.

Given the following, I expected

3+*5

to evaluate to 15. Instead it throws an error.

Possible resolution related to thewaywewalk's answer to my previous question at Why is a trailing comma in a cell array valid Matlab syntax?

Community
  • 1
  • 1
chessofnerd
  • 1,219
  • 1
  • 20
  • 40

2 Answers2

4

+ and - are not only binary operators, they are also unary operators.

Documentation:

http://de.mathworks.com/help/matlab/ref/uplus.html http://de.mathworks.com/help/matlab/ref/uminus.html

For this reasons, the first two lines are evaluated as 3+(+4) and 3+(-5) but the last fails because no unary multiplication exists.

Daniel
  • 36,610
  • 3
  • 36
  • 69
2

Because Matlab's operator precedence places the unary plus above binary plus.

Therefore,

3++4

is parsed to

plus(3,uplus(4))
TroyHaskin
  • 8,361
  • 3
  • 22
  • 22