[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  Arithmetic: Arithmetic Operators

##  Description

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

##  Usage

    
    
    + x
    - x
    x + y
    x - y
    x * y
    x / y
    x ^ y
    x %% y
    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 ](/link/warning?package=base&version=3.6.2) ` 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”) and ` %/% ` indicates integer
division. It is guaranteed that

` x == (x %% y) + y * (x %/% y) `

    

(up to rounding error)

unless ` y == 0 ` where the result of ` %% ` is ` [ NA_integer_
](/link/NA_integer_?package=base&version=3.6.2) ` or ` [ NaN
](/link/NaN?package=base&version=3.6.2) ` (depending on the ` [ typeof
](/link/typeof?package=base&version=3.6.2) ` of the arguments) or for some
non- [ finite ](/link/finite?package=base&version=3.6.2) 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 ](/link/integer?package=base&version=3.6.2) , the type of
the result of ` / ` and ` ^ ` is [ numeric
](/link/numeric?package=base&version=3.6.2) 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
](/link/tsp?package=base&version=3.6.2) ` 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.

##  S4 methods

These operators are members of the S4 ` [ Arith
](/link/Arith?package=base&version=3.6.2) ` group generic, and so methods can
be written for them individually as well as for the group generic (or the `
Ops ` group generic), with arguments ` c(e1, e2) ` (with ` e2 ` missing for a
unary operator).

##  Implementation limits

R  is dependent on OS services (and they on FPUs) for floating-point
arithmetic. On all current  R  platforms IEC 60559 (also known as IEEE 754)
arithmetic is used, but some things in those standards are optional. In
particular, the support for _denormal_ aka _subnormal_ numbers (those outside
the range given by ` [ .Machine ](/link/.Machine?package=base&version=3.6.2) `
) may differ between platforms and even between calculations on a single
platform.

Another potent