4

Does anyone know why this works in MATLAB?

>> 1 ++ 2
ans =
     3

Coming from coding in C, python, Java etc, I find it most counterintuitive that this should work at all. Presumably there's something important about the parser that I don't understand?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Sanjay Manohar
  • 6,920
  • 3
  • 35
  • 58

1 Answers1

6

There's a difference between plus and uplus. I suspect MATLAB takes the first + as plus, and all the others as uplus. Since uplus is by default just "return what's behind", you add 1 and 2, and use a lot of "return what's behind" in between.

a=2;
c=+a % unitary plus
c =
     2
1+2 % addition
ans =
     3
1+++2 % addition and two uplusses
ans =
     3

The reason uplus exists is to allow operator overloading in classes. The same works in other languages, e.g. in C#, to allow for operator overloading in confined classes.


The other reason mentioned in that C# thread is that is changes unsigned shorts to integers, which is not the case for MATLAB:

d=uint8(1)
d =
  uint8
   1
+d
ans =
  uint8
   1
a=+d
a =
  uint8
   1

It does, however, convert a boolean to a double, thanks to Cris Lunego for pointing that out:

+true
ans =
     1
+false
ans =
     0

The following however remains a mystery to me, inspired by Sanjay Manohar's comment:

>> [1 ++ 2]
ans =
     1     2 % Two unary plusses
>> [1 + + 2]
ans =
     3 % A normal plus and a unary one
>> [1++2]
ans =
     3 % A normal plus and a unary one

The same works with multiple plusses, [1 +++..+++ 2], so with all plusses consecutively in the middle generates [1 2], all other combinations (as far as I tested) result in 3. I asked a separate question about this: Why do the plus and unary plus behave strange in array syntax?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • Ah yes, silly me. It seems that `++` isn't lexed as a single token. I guess this means the lexer and parser aren't separate things in Matlab? I had never noticed this, and it doesn't seem to be a very common feature among languages? – Sanjay Manohar Sep 28 '18 at 13:23
  • @SanjayManohar I have no idea what a lexer or parser is; `++` is not, as you might expect from other languages, "add one". Basically MATLAB's interpreter tries to use a plus first as addition, and once that doesn't work, it defaults to the unary plus, which in turn doesn't do anything with the input. MATLAB does not require white space; `1*exp(i+3/5)` is just as valid as `1 * exp( i + 3 / 5 )` – Adriaan Sep 28 '18 at 13:24
  • 1
    @Sanjay Manohar MATLAB doesn't have a `++` AFAIK, so there's no problem for the lexer here. –  Sep 28 '18 at 13:25
  • 2
    For those interested: Unarticulated plus also converts a Boolean to a double: `+true` is `1.0` (though that conversion would also happen in any other arithmetic context). – Cris Luengo Sep 28 '18 at 13:29
  • @CrisLuengo Thanks! But it doesn't promote: `class(+uint8(1))` gives `uint8`. – Sanjay Manohar Sep 28 '18 at 13:31
  • @JETM Yes that is crucial - the fact that Matlab *doesn't* have `++` is necessary for this to work. – Sanjay Manohar Sep 28 '18 at 13:34
  • @Sanjay: True, it returns the same class if the input is numeric, but converts to double if the input is Boolean. `uint8(1)+1` also returns an 8-bit integer. MATLAB always keeps the class of the non-double numeric values, I believe. – Cris Luengo Sep 28 '18 at 13:36