These unary and binary operators perform arithmetic on numeric or complex vectors (or objects which can be coerced to them).
Usage
## Default S3 method:
+x
x + y
## Default S3 method:
-x
x - y
# S3 method for default
*(x, y)
# S3 method for default
/(x, y)
# S3 method for default
^(x, y)
Arguments
- x, y
numeric or complex vectors or objects which can be coerced to such, or other objects for which methods have been written.
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 operators are +
for addition,
-
for subtraction, *
for multiplication, /
for
division and ^
for exponentiation.
%%
indicates x mod y
(“x modulo y”), i.e.,
computes the ‘remainder’
r <- x %% y
, and
%/%
indicates integer division, where R uses “floored”
integer division, i.e., q <- x %/% y := floor(x/y)
, as promoted
by Donald Knuth, see the Wikipedia page on ‘Modulo operation’,
and hence sign(r) == sign(y)
. It is guaranteed that
x == (x %% y) + y * (x %/% y)
(up to rounding error)
unless y == 0
where the result of %%
is
NA_integer_
or NaN
(depending on the
typeof
of the arguments) or for some non-finite
arguments, e.g., when the RHS of the identity above
amounts to Inf - Inf
.
If either argument is complex the result will be complex, otherwise if one or both arguments are numeric, the result will be numeric. If both arguments are of type integer, the type of the result of
/
and ^
is numeric and for the other operators it
is integer (with overflow, which occurs at
\(\pm(2^{31} - 1)\),
returned as NA_integer_
with a warning).
The rules for determining the attributes of the result are rather complicated. Most attributes are taken from the longer argument. Names will be copied from the first if it is the same length as the answer, otherwise from the second if that is. If the arguments are the same length, attributes will be copied from both, with those of the first argument taking precedence when the same attribute is present in both arguments. For time series, these operations are allowed only if the series are compatible, when the class and
tsp
attribute of whichever is a time series (the same,
if both are) are used. For arrays (and an array result) the
dimensions and dimnames are taken from first argument if it is an
array, otherwise the second.
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
given if this is detected.
%%
and x %/% y
can be used for non-integer y
,
e.g. 1 %/% 0.2
, but the results are subject to representation
error and so may be platform-dependent. Because the IEC 60559
representation of 0.2
is a binary fraction slightly larger than
0.2
, the answer to 1 %/% 0.2
should be 4
but
most platforms give 5
.
Users are sometimes surprised by the value returned, for example why
(-8)^(1/3)
is NaN
. For double inputs, R makes
use of IEC 60559 arithmetic on all platforms, together with the C
system function pow for the ^
operator. The relevant
standards define the result in many corner cases. In particular, the
result in the example above is mandated by the C99 standard. On many
Unix-alike systems the command man pow
gives details of the
values in a large number of corner cases.
Arithmetic on type double in R is supposed to be done in ‘round to nearest, ties to even’ mode, but this does depend on the compiler and FPU being set up correctly.
Examples
x <- -1:12
x + 1
#> [1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13
2 * x + 3
#> [1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27
x %% 3 # is periodic 2 0 1 2 0 1 ...
#> [1] 2 0 1 2 0 1 2 0 1 2 0 1 2 0
x %% -3 # (ditto) -1 0 -2 -1 0 -2 ...
#> [1] -1 0 -2 -1 0 -2 -1 0 -2 -1 0 -2 -1 0
x %/% 5
#> [1] -1 0 0 0 0 0 1 1 1 1 1 2 2 2
x %% Inf # now is defined by limit (gave NaN in earlier versions of R)
#> [1] Inf 0 1 2 3 4 5 6 7 8 9 10 11 12