Unary operation

From Wikipedia, the free encyclopedia

In mathematics, a unary operation is an operation with only one operand, i.e. an operation with a single input, or in other words, a function of one variable (for the terminology see also operators versus functions).

Common notations are prefix notation (+, −, not), postfix notation (factorial: n!), and functional notation (sin x or sin (x)). In the case of the square root a horizontal bar over the argument extending the square root sign can indicate the extent of the argument, so that parentheses can be dispensed with.

Contents

[edit] Unary Negative and Positive

As unary operations have only one oprand they are evaluated before other operations containing them are. In this example using addition:

3 − −2

Here the first '−' represents the binary subtraction operation, while the second '−' represents the unary negation of the two, a clearer way to write the expression would be:

3 − (−2) = 5

Technically there is also a unary positive but it is not needed since we assume a value to be positive:

(+2) = 2

Unary positive does not change the sign of a negative operation:

(+(−2)) = (−2)

In the case a unary negative is needed to change the sign:

(−(−2)) = (+2)

[edit] Examples of Unary operations

[edit] Computer programming

Unary operators (called "monadic" in APL) are also used in programming languages.

[edit] C family of languages

In the C family of languages, the following operators are unary:

[edit] Other languages

[edit] Windows PowerShell

  • Increment: ++$x, $x++
  • Decrement: −−$x, $x−−
  • Positive: +$x
  • Negative: $x
  • Logical negation: -not $x
  • Invoke in current scope: .$x
  • Invoke in new scope: &$x
  • Cast: [type-name] cast-expression

[edit] Usage of Incremental and Decremental operators

In most programming languages, incremental and decremental operators can be preffixed and suffixed to the variable. The preffix and suffix can be useful when manipulating variables. A suffixed operation increments the value after it has been called.

[edit] Example 1

int i = 0;
printf (" %d \n %d ", i++, i++);

Output:

1
0

Whereas a prefixed operation will increment the value before it has been called.

[edit] Example 2

int i = 0;
printf (" %d \n %d ", ++i, ++i);

Output:

2
1

The usage of incremental and decremental operators is usually found in For loops.

[edit] See also