ID EN
Math Functions

Arithmetic

R Base 3.6.2

These unary and binary operators perform arithmetic on numeric or complex vectors (or objects which can be coerced to them).

Syntax

R
+ x
- x
x + y
x - y
x * y
x / y
x ^ y
x %% y
x %/% y

Arguments

Parameter Description
x, y numeric or complex vectors or objects which can be coerced to such, or other objects for which methods have been written.

Return Value

Unary + and unary - return a numeric or complex vector. All attributes (including class) are preserved if there is no coercion: logical x is coerced to integer and names, dims and dimnames are preserved. The binary operators return vectors containing the result of the element by element operations. If involving a zero-length vector the result has length zero. Otherwise, the elements of shorter vectors are recycled as necessary (with a warning when they are recycled only fractionally). The operat

Details

The unary and binary arithmetic operators are generic functions: methods can be written for them individually or via the Ops group generic function. (See Ops for how dispatch is computed.) If applied to arrays the result will be an array if this is sensible (for example it will not if the recycling rule has been invoked). Logical vectors will be coerced to integer or numeric vectors, FALSE having value zero and TRUE having value one. 1 ^ y and y ^ 0 are 1, always. x ^ y should also give the proper limit result when either (numeric) argument is infinite (one of Inf or -Inf). Objects such as arrays or time-series can be operated on this way provided they are conformable. For double arguments, %% can be subject to catastrophic loss of accuracy if x is much larger than y, and a warning is give

Examples

Example
R
# NOT RUN {
x <- -1:12
x + 1
2 * x + 3
x %% 2 #-- is periodic
x %/% 5
x %% Inf # now is defined by limit (gave NaN in earlier versions of R)
# }

See Also

sqrt for miscellaneous and Special for special mathematical functions. Syntax for operator precedence. %*% for matrix multiplication.