10

This is a rhetorical question about the uplus function in MATLAB, or its corresponding operator, the unary plus +.

Is there a case where this operator is useful? Even better, is there a case where this operator is necessary?

3
  • I can't say I've ever had to use it, or even thought of using it before. In some cases it might be useful for the sake of clarity to the reader. This similar question in C# might be of interest.
    – eigenchris
    Commented Feb 27, 2015 at 14:57
  • 3
    I don't think you actually meant rhetorical so I took the liberty of answering this. ;-)
    – horchler
    Commented Feb 27, 2015 at 15:01
  • @horchler This is not a practical question on a specific problem, but more a topic on which I wanted to get your toughts. Thanks for answering, anyway.
    – Ratbert
    Commented Feb 27, 2015 at 15:02

3 Answers 3

13

It is not necessary, a language without a unary plus does not allow to write +1. Obviously you could also write 1 but when importing data which always writes the + or - it's very nice to have.

Searching some source codes, I found a curious use of +

A=+A

which replaced the code:

if ~isnumeric(A)
    A=double(A);
end

It casts chars and logicals to double, but all numeric data types remain untouched.

5
  • 1
    Well, there is a lot of things that is not necessary. For example: classes, structs, cells, functions ... Still they make it easier to do programming. So as you say that it can be used to make it simpler when importing some data it is an as good reason as any to have it. Anyway +1 for providing a good reason
    – patrik
    Commented Feb 27, 2015 at 15:04
  • 2
    I was just typeing my answer with the cast use!
    – Luis Mendo
    Commented Feb 27, 2015 at 15:12
  • Yes, importing data is a key application. Sometimes it's easier in code to do + or - instead of nothing or -. printf style functions have the option always putting a leading sign. The string output from Matlab's symbolic math (before it's converted back to sym class) also often includes prefixed + operators, presumably because it's easier for MuPAD to parse.
    – horchler
    Commented Feb 27, 2015 at 15:19
  • @Daniel: Very nice cast usage. I'm waiting a bit to see if anyone has other ideas before marking the topic as solved.
    – Ratbert
    Commented Feb 27, 2015 at 15:36
  • 5
    Personally, I would rather read the longer explicit if-statement version every day of the week, rather than an obscure A=+A that will leave me puzzled about what it really does. Commented Feb 27, 2015 at 18:17
8

It can be useful when defining new numeric types.

Suppose you define quaternion and overload uplus:

classdef quaternion
    ...
end

Then in your code you can write:

x = quaternion(...);
y = [+x, -x];
z = +quaternion.Inf;
t = -quaternion.Inf;

If you don't you cannot have same syntax as for other numeric.

PS: To the question "is it useful" (in the sence mandatory for some syntaxes) ... well I can't find any reason ... but sometimes writting '+x' make things clearer when reading back the code.

4
  • 3
    Somewhat related to your answer: I sometimes write +Inf in comments and documentation for my code as I think it comes across more clearly (and makes it obvious that Inf is not a misspelling of something else).
    – horchler
    Commented Feb 27, 2015 at 15:31
  • 3
    @horchler +1 for the +Inf
    – Ratbert
    Commented Feb 27, 2015 at 15:34
  • 3
    @CrazyRat: where would Stack Overflow be without the unary plus?!
    – horchler
    Commented Feb 27, 2015 at 15:42
  • @horchler You are right, the unary plus completely makes sense in SO !
    – Ratbert
    Commented Feb 27, 2015 at 15:43
4

I'm not sure if this fully constitutes "useful" or if it's the best programming practice, but in some cases, one may wish to use the unary + for symmetry/clarity reasons. There's probably a better example, but I'm thinking of something like this:

A = [+1 -1 +1;
     -1 +1 -1;
     +1 -1 +1];

As for the uplus function, it's kind of a NOOP for numeric operations. If one writes a function that requires a function handle input to specify an operation to perform, it might be useful to have do nothing option.

Lastly, numeric operators can be overloaded for other classes. The uplus function could have more use in other built-in classes or even one you might want write yourself.

1
  • 1
    OK, I get the convenience/clarity point, though it's not the meaning of "useful" I was thinking of. The overloading usage is interesting. Can you think of a case where the uplus syntax would be more handy than a regular method ? Besides the fact that it takes less characters to write ...
    – Ratbert
    Commented Feb 27, 2015 at 15:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.