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?

2
  • 3++++-++5 evaluates to -2. Guess - is 'stronger' than +? Weird... Commented Jan 23, 2016 at 3:34
  • 3
    The unary - negates the value, while unary + just returns the original value (like multiplying by -1 and 1, respectively) Commented Jan 23, 2016 at 3:50

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Therefore,

3++4

is parsed to

plus(3,uplus(4))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.