What's the Significant Use of Unary Plus and Minus Operators

What's the significant use of unary plus and minus operators?

The Unary + operator converts its operand to Number type.
The Unary - operator converts its operand to Number type, and then negates it.
(per the ECMAScript spec)

In practice, Unary - is used for simply putting negative numbers in normal expressions, e.g.:

var x = y * -2.0;

That's the unary minus operator at work. The Unary + is equivalent to the Number() constructor called as a function, as implied by the spec.

I can only speculate on the history, but the unary +/- operators behave similarly in many C-derived languages. I suspect the Number() behavior is the addition to the language here.

What is the purpose of the unary plus (+) operator in C?

As per the C90 standard in 6.3.3.3:

The result of the unary + operator is the value of its operand. The integral promotion is
performed on the operand. and the result has the promoted type.

and

The operand of the unary + or - operator shall have arithmetic type..

What's the point of unary plus operator in Ruby?

Perhaps it's just a matter of consistency, both with other programming languages, and to mirror the unary minus.

Found support for this in The Ruby Programming Language (written by Yukihiro Matsumoto, who designed Ruby):

The unary plus is allowed, but it has no effect on numeric operands—it simply returns the value of its operand. It is provided for symmetry with unary minus, and can, of course, be redefined.

What is the purpose of Java's unary plus operator?

The unary plus operator performs an automatic conversion to int when the type of its operand is byte, char, or short. This is called unary numeric promotion, and it enables you to do things like the following:

char c = 'c';
int i = +c;

Granted, it's of limited use. But it does have a purpose. See the specification, specifically sections §15.15.3 and §5.6.1.

What does the unary plus operator do?

It's there to be overloaded if you feel the need; for all predefined types it's essentially a no-op.

The practical uses of a no-op unary arithmetic operator are pretty limited, and tend to relate to the consequences of using a value in an arithmetic expression, rather than the operator itself. For example, it can be used to force widening from smaller integral types to int, or ensure that an expression's result is treated as an rvalue and therefore not compatible with a non-const reference parameter. I submit, however, that these uses are better suited to code golf than readability. :-)

operator before expression in javascript: what does it do?

The unary + operator can be used to convert a value to a number in JavaScript. Underscore appears to be testing that the .length property is a number, otherwise it won't be equal to itself-converted-to-a-number.

What's the purpose of the + (pos) unary operator in Python?

I believe that Python operators where inspired by C, where the + operator was introduced for symmetry (and also some useful hacks, see comments).

In weakly typed languages such as PHP or Javascript, + tells the runtime to coerce the value of the variable into a number. For example, in Javascript:

   +"2" + 1
=> 3
"2" + 1
=> '21'

Python is strongly typed, so strings don't work as numbers, and, as such, don't implement an unary plus operator.

It is certainly possible to implement an object for which +obj != obj :

>>> class Foo(object):
... def __pos__(self):
... return "bar"
...
>>> +Foo()
'bar'
>>> obj = Foo()
>>> +"a"

As for an example for which it actually makes sense, check out the
surreal numbers. They are a superset of the reals which includes
infinitesimal values (+ epsilon, - epsilon), where epsilon is
a positive value which is smaller than any other positive number, but
greater than 0; and infinite ones (+ infinity, - infinity).

You could define epsilon = +0, and -epsilon = -0.

While 1/0 is still undefined, 1/epsilon = 1/+0 is +infinity, and 1/-epsilon = -infinity. It is
nothing more than taking limits of 1/x as x aproaches 0 from the right (+) or from the left (-).

As 0 and +0 behave differently, it makes sense that 0 != +0.

What is unary plus / minus in R?

The arity of an operator tells on how many arguments it operates. Unary works on a single argument, binary works on two arguments, ternary works on three arguments, etc.

-a
^

That is an unary minus. It negates the value of the single argument/expression that follows it. You might think of it as a function call like minus(a) that changes the sign of its argument and returns that as result. Unary plus also exists but it is basically a no-op.

a - b
^

That is a binary minus. It takes the value of its two arguments/expressions and subtracts the second one from the first one. You might think of it as a function call like minus(a,b) that takes two arguments and returns their difference. Binary plus returns the sum.


As noted by @BondedDust, in R (and in other languages that support vector processing) some operators actually take vector arguments and then perform their action on each element separately. For example, the unary minus sign-inverts all elements of a vector:

> -(-2:2)
[1] 2 1 0 -1 -2

or as a function call:

> `-`(-2:2)
[1] 2 1 0 -1 -2

The binary minus subtracts two vectors element-wise:

> 1:5 - 5:1
[1] -4 -2 0 2 4

or as a function call:

> `-`(1:5, 5:1)
[1] -4 -2 0 2 4

The minus operator in R is a function with two arguments:

> `-`
function (e1, e2) .Primitive("-")

When both arguments are present, it performs the operation of the binary minus, i.e. subtracts e2 from e1 element-wise. When only e1 is present, it operates as a unary minus and sign-inverts the elements of e1.

The same applies to the plus operator. One has to be careful and to not confuse the plus operator + with the sum function. + operates element-wise on one (as an unary operator) or on two (as a binary operator) vector arguments while sum sums all values present in its arguments. And while sum could take any number of arguments:

> sum
function (..., na.rm = FALSE) .Primitive("sum")

the + operator takes only one or two:

> `+`(1, 2, 3)
Error in `+`(1, 2, 3) : operator needs one or two arguments

Does the unary + operator have any practical use?

char ch = 'a';
std::cout << ch << '\n';
std::cout << +ch << '\n';

The first insertion writes the character a to cout. The second insertion writes the numeric value of ch to cout. But that's a bit obscure; it relies on the compiler applying integral promotions for the + operator.



Related Topics



Leave a reply



Submit