Rev 54463 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/Arithmetic.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2011 R Core Development Team% Distributed under GPL 2 or later\name{Arithmetic}\title{Arithmetic Operators}\usage{x + yx - yx * yx / yx ^ yx \%\% yx \%/\% y}\alias{+}\alias{-}\alias{*}\alias{**}\alias{/}\alias{^}\alias{\%\%}\alias{\%/\%}\alias{Arithmetic}\concept{remainder}\concept{modulo}\concept{modulus}\concept{quotient}\concept{division}\description{These binary operators perform arithmetic on numeric or complex vectors(or objects which can be coerced to them).}\arguments{\item{x, y}{numeric or complex vectors or objects which can becoerced to such, or other objects for which methods have been written.}}\value{These operators return vectors containing the result of the elementby element operations. The elements of shorter vectors are recycledas necessary (with a \code{\link{warning}} when they are recycled only\emph{fractionally}). The operators are \code{+} for addition,\code{-} for subtraction, \code{*} for multiplication, \code{/} fordivision and \code{^} for exponentiation.\code{\%\%} indicates \code{x mod y} and \code{\%/\%} indicatesinteger division. It is guaranteed that \code{x == (x \%\% y) + y * ( x\%/\% y )} (up to rounding error) unless \code{y == 0} where theresult is \code{\link{NA_integer_}} or \code{\link{NaN}} (depending onthe \code{\link{typeof}} of the arguments). See\url{http://en.wikipedia.org/wiki/Modulo_operation} for therationale.If either argument is complex the result will be complex, otherwise ifone or both arguments are numeric, the result will be numeric. Ifboth arguments are of type \link{integer}, the type of the result of\code{/} and \code{^} is \link{numeric} and for the other operators itis integer (with overflow, which occurs at\eqn{\pm(2^{31} - 1)}{+/- (2^31 - 1)},returned as \code{NA_integer_} with a warning).The rules for determining the attributes of the result are rathercomplicated. Most attributes are taken from the longer argument, thefirst if they are of the same length. Names will be copied from thefirst if it is the same length as the answer, otherwise from thesecond if that is. For time series, these operations are allowed onlyif the series are compatible, when the class and \code{\link{tsp}}attribute of whichever is a time series (the same, if both are) areused. For arrays (and an array result) the dimensions and dimnamesare taken from first argument if it is an array, otherwise the second.}\details{The binary arithmetic operators are generic functions: methods can bewritten for them individually or via the\code{\link[=S3groupGeneric]{Ops}} group generic function. (See\code{\link[=S3groupGeneric]{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,\code{FALSE} having value zero and \code{TRUE} having value one.\code{1 ^ y} and \code{y ^ 0} are \code{1}, \emph{always}.\code{x ^ y} should also give the proper limit result wheneither argument is infinite (i.e., \code{+- \link{Inf}}).Objects such as arrays or time-series can be operated on thisway provided they are conformable.For real arguments, \code{\%\%} can be subject to catastrophic loss ofaccuracy if \code{x} is much larger than \code{y}, and a warning isgiven if this is detected.\code{\%\%} and \code{x \%/\% y} can be used for non-integer \code{y},e.g. \code{1 \%/\% 0.2}, but the results are subject to representationerror and so may be platform-dependent. Because the IEC 60059representation of \code{0.2} is a binary fraction slightly larger than\code{0.2}, the answer to \code{1 \%/\% 0.2} should be \code{4} butmost platforms give \code{5}.Users are sometimes surprised by the value returned, for example why\code{(-8)^(1/3)} is \code{NaN}. For \link{double} inputs, \R makesuse of IEC 60559 arithmetic on all platforms, together with the Csystem function \samp{pow} for the \code{^} operator. The relevantstandards define the result in many corner cases. In particular, theresult in the example above is mandated by the C99 standard. On manyUnix-alike systems the command \command{man pow} gives details of thevalues in a large number of corner cases.Arithmetic on type \link{double} in \R is supposed to be done in\sQuote{round to nearest, ties to even} mode, but this does depend onthe compiler and FPU being set up correctly.}\section{S4 methods}{These operators are members of the S4 \code{\link{Arith}} group generic,and so methods can be written for them individually as well as for thegroup generic (or the \code{Ops} group generic), with arguments\code{c(e1, e2)}.}\note{\code{**} is translated in the parser to \code{^}, but this wasundocumented for many years. It appears as an index entry in Becker\emph{et al} (1988), pointing to the help for \code{Deprecated} butis not actually mentioned on that page. Even though it had beendeprecated in S for 20 years, it was still accepted in \R in 2008.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth & Brooks/Cole.D. Goldberg (1991) \emph{What Every Computer Scientist Should Knowabout Floating-Point Arithmetic} ACM Computing Surveys, \bold{23(1)}.\crPostscript version available at\url{http://www.validlab.com/goldberg/paper.ps}Extended PDF version at \url{http://www.validlab.com/goldberg/paper.pdf}}\seealso{\code{\link{sqrt}} for miscellaneous and \code{\link{Special}} for specialmathematical functions.\code{\link{Syntax}} for operator precedence.\code{\link{\%*\%}} for matrix multiplication.}\examples{x <- -1:12x + 12 * x + 3x \%\% 2 #-- is periodicx \%/\% 5}\keyword{arith}