Inlogic,mathematics, andcomputer science,arity (/ˈærɪti/ ⓘ) is the number ofarguments oroperands taken by afunction,operation orrelation. In mathematics, arity may also be called rank,[1][2] but this word can have many other meanings. In logic andphilosophy, arity may also be calledadicity anddegree.[3][4] Inlinguistics, it is usually namedvalency.[5]
In general, functions or operators with a given arity follow the naming conventions ofn-basednumeral systems, such asbinary andhexadecimal. ALatin prefix is combined with the -ary suffix. For example:
Aconstant can be treated as the output of an operation of arity 0, called anullary operation.
Also, outside offunctional programming, a function without arguments can be meaningful and not necessarily constant (due toside effects). Such functions may have somehidden input, such asglobal variables or the whole state of the system (time, free memory, etc.).
Examples ofunary operators in mathematics and in programming include theunary minus and plus, the increment and decrement operators inC-style languages (not in logical languages), and thesuccessor,factorial,reciprocal,floor,ceiling,fractional part,sign,absolute value,square root (the principal square root),complex conjugate (unary of "one"complex number, that however has two parts at a lower level of abstraction), andnorm functions in mathematics. In programming thetwo's complement,address reference, and thelogical NOT operators are examples of unary operators.
All functions inlambda calculus and in somefunctional programming languages (especially those descended fromML) are technically unary, but seen-ary below.
According toQuine, the Latin distributives beingsinguli,bini,terni, and so forth, the term "singulary" is the correct adjective, rather than "unary".[6]Abraham Robinson follows Quine's usage.[7]
In philosophy, the adjectivemonadic is sometimes used to describe aone-place relation such as 'is square-shaped' as opposed to atwo-place relation such as 'is the sister of'.
Most operators encountered in programming and mathematics are of thebinary form. For both programming and mathematics, these include themultiplication operator, the radix operator, the often omittedexponentiation operator, thelogarithm operator, theaddition operator, and thedivision operator. Logical predicates such asOR,XOR,AND,IMP are typically used as binary operators with two distinct operands. InCISC architectures, it is common to have two source operands (and store result in one of them).
The computer programming languageC and its various descendants (includingC++,C#,Java,Julia,Perl, and others) provide theternary conditional operator?:
. The first operand (the condition) is evaluated, and if it is true, the result of the entire expression is the value of the second operand, otherwise it is the value of the third operand. This operator has a lazy or 'shortcut'evaluation strategy that does not evaluate whichever of the second and third arguments is not used. Some functional programming languages, such asAgda, have such an evaluation strategy for all functions and consequently implementif...then...else
as an ordinary function; several others, such asHaskell, can do this but for syntactic, performance or historical reasons choose to define keywords instead.
ThePython language has a ternary conditional expression,xifCelsey
. InElixir the equivalent would beif(C,do:x,else:y)
.
TheForth language also contains a ternary operator,*/
, which multiplies the first two (one-cell) numbers, dividing by the third, with the intermediate result being a double cell number. This is used when the intermediate result would overflow a single cell.
The Unixdc calculator has several ternary operators, such as|
, which will pop three values from the stack and efficiently compute witharbitrary precision.
Many (RISC)assembly language instructions are ternary (as opposed to only two operands specified in CISC); or higher, such asMOV%AX,(%BX,%CX)
, which will load (MOV) into registerAX the contents of a calculated memory location that is the sum (parenthesis) of the registersBX andCX.
Thearithmetic mean ofn real numbers is ann-ary function:
Similarly, thegeometric mean ofnpositive real numbers is ann-ary function: Note that alogarithm of the geometric mean is the arithmetic mean of the logarithms of itsn arguments
From a mathematical point of view, a function ofn arguments can always be considered as a function of a single argument that is an element of someproduct space. However, it may be convenient for notation to considern-ary functions, as for examplemultilinear maps (which are not linear maps on the product space, ifn ≠ 1).
The same is true for programming languages, where functions taking several arguments could always be defined as functions taking a single argument of somecomposite type such as atuple, or in languages withhigher-order functions, bycurrying.
In computer science, a function that accepts a variable number of arguments is calledvariadic. In logic and philosophy, predicates or relations accepting a variable number of arguments are calledmultigrade, anadic, or variably polyadic.[8]
Latinate names are commonly used for specific arities, primarily based on Latindistributive numbers meaning "in group ofn", though some are based on Latincardinal numbers orordinal numbers. For example, 1-ary is based on cardinalunus, rather than from distributivesingulī that would result insingulary.
n-ary | Arity (Latin based) | Adicity (Greek based) | Example in mathematics | Example in computer science |
---|---|---|---|---|
0-ary | nullary (fromnūllus) | niladic | aconstant | a function without arguments,True,False |
1-ary | unary | monadic | additive inverse | logicalNOT operator |
2-ary | binary | dyadic | addition | logicalOR,XOR,AND operators |
3-ary | ternary | triadic | triple product of vectors | ternary conditional operator |
4-ary | quaternary | tetradic | ||
5-ary | quinary | pentadic | ||
6-ary | senary | hexadic | ||
7-ary | septenary | hebdomadic | ||
8-ary | octonary | ogdoadic | ||
9-ary | novenary (alt. nonary) | enneadic | ||
10-ary | denary (alt. decenary) | decadic | ||
more than 2-ary | multary and multiary | polyadic | ||
varying | variadic | sum; e.g., Σ | variadic function,reduce |
n-ary means havingn operands (or parameters), but is often used as a synonym of "polyadic".
These words are often used to describe anything related to that number (e.g., undenary chess is achess variant with an 11×11 board, or theMillenary Petition of 1603).
The arity of arelation (orpredicate) is the dimension of thedomain in the correspondingCartesian product. (A function of arityn thus has arityn+1 considered as a relation.)
Incomputer programming, there is often asyntactical distinction betweenoperators andfunctions; syntactical operators usually have arity 1, 2, or 3 (theternary operator?: is also common). Functions vary widely in the number of arguments, though large numbers can become unwieldy. Some programming languages also offer support forvariadic functions, i.e., functions syntactically accepting a variable number of arguments.
A monograph available free online: