Unary operation

Unary operation

Discussion
Ask a question about 'Unary operation'
Start a new discussion about 'Unary operation'
Answer questions from other users
Full Discussion Forum
 
Encyclopedia
In mathematics
Mathematics
Mathematics is the study of quantity, space, structure, and change. Mathematicians seek out patterns and formulate new conjectures. Mathematicians resolve the truth or falsity of conjectures by mathematical proofs, which are arguments sufficient to convince other mathematicians of their validity...

, a unary operation is an operation
Operation (mathematics)
The general operation as explained on this page should not be confused with the more specific operators on vector spaces. For a notion in elementary mathematics, see arithmetic operation....

 with only one operand
Operand
In mathematics, an operand is the object of a mathematical operation, a quantity on which an operation is performed.-Example :The following arithmetic expression shows an example of operators and operands:3 + 6 = 9\;...

, i.e. a single input. Specifically, it is a function
where A is a set. In this case f is called a unary operation on A.

Common notations are prefix notation (e.g. +, −, not), postfix notation (e.g. factorial
Factorial
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n...

: n!), functional notation (e.g. sin x or sin (x)), and superscripts (e.g. transpose
Transpose
In linear algebra, the transpose of a matrix A is another matrix AT created by any one of the following equivalent actions:...

 AT). Other notation exists as well, for example in the case of the square root
Square root
In mathematics, a square root of a number x is a number r such that r2 = x, or, in other words, a number r whose square is x...

 a horizontal bar over the argument extending the square root sign can indicate the extent of the argument.

Unary negative and positive


As unary operations have only one operand they are evaluated before other operations containing them. Here is an example using negation:
3 − −2


Here the first '−' represents the binary subtraction operation, while the second '−' represents the unary negation of the 2. Therefore, the expression is equal to:
3 − (−2) = 5


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

Unary positive does not change the sign of a negative operation:
) = (−2)

In this case a unary negative is needed to change the sign:
) = (+2)

Computer programming


Unary operators (called "monadic" in APL
APL programming language
APL is an interactive array-oriented language and integrated development environment, which is available from a number of commercial and noncommercial vendors and for most computer platforms. It is based on a mathematical notation developed by Kenneth E...

) are also used in programming languages.

C family of languages


In the C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 family of languages, the following operators are unary:
  • Increment: ++x, x++
  • Decrement: −−x, x−−
  • Address
    Reference (computer science)
    In computer science, a reference is a value that enables a program to indirectly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing those data is called...

    : &x
  • Indirection
    Indirection
    In computer programming, indirection is the ability to reference something using a name, reference, or container instead of the value itself. The most common form of indirection is the act of manipulating a value through its memory address. For example, accessing a variable through the use of a...

    : *x
  • Positive: +x
  • Negative: x
  • One's complement
    Signed number representations
    In computing, signed number representations are required to encode negative numbers in binary number systems.In mathematics, negative numbers in any base are represented by prefixing them with a − sign. However, in computer hardware, numbers are represented in binary only without extra...

    : ~x
  • Logical negation
    Negation
    In logic and mathematics, negation, also called logical complement, is an operation on propositions, truth values, or semantic values more generally. Intuitively, the negation of a proposition is true when that proposition is false, and vice versa. In classical logic negation is normally identified...

    : !x
  • Sizeof: sizeof x, sizeof(type-name)
  • Cast
    Type conversion
    In computer science, type conversion, typecasting, and coercion are different ways of, implicitly or explicitly, changing an entity of one data type into another. This is done to take advantage of certain features of type hierarchies or type representations...

    : (type-name) cast-expression

Windows PowerShell

  • Increment: ++$x, $x++
  • Decrement: −−$x, $x−−
  • Positive: +$x
  • Negative: $x
  • Logical negation: -not $x
  • Invoke in current scope
    Scope (programming)
    In computer programming, scope is an enclosing context where values and expressions are associated. Various programming languages have various types of scopes. The type of scope determines what kind of entities it can contain and how it affects them—or semantics...

    : .$x
  • Invoke in new scope: &$x
  • Cast: [type-name] cast-expression

See also

  • Binary operation
    Binary operation
    In mathematics, a binary operation is a calculation involving two operands, in other words, an operation whose arity is two. Examples include the familiar arithmetic operations of addition, subtraction, multiplication and division....

  • Ternary operation
    Ternary operation
    In mathematics, a ternary operation is an n-ary operation with n = 3. A ternary operation on a set A takes any given three elements of A and combines them to form a single element of A. An example of a ternary operation is the product in a heap....

  • Arity
    Arity
    In logic, mathematics, and computer science, the arity of a function or operation is the number of arguments or operands that the function takes. The arity of a relation is the dimension of the domain in the corresponding Cartesian product...

  • Operation (mathematics)
    Operation (mathematics)
    The general operation as explained on this page should not be confused with the more specific operators on vector spaces. For a notion in elementary mathematics, see arithmetic operation....

  • Operator (programming)
    Operator (programming)
    Programming languages typically support a set of operators: operations which differ from the language's functions in calling syntax and/or argument passing mode. Common examples that differ by syntax are mathematical arithmetic operations, e.g...