Arithmetic Operators

DESCRIPTION:

The arithmetic operations of addition, subtraction, multiplication, division, exponentiation, integer division, and modulo. All but the latter two accept complex as well as numeric arguments.

USAGE:

e1 op e2 

REQUIRED ARGUMENTS:

op
one of +, -, *, /, ^, %/% or %%.
e1,e2
numeric or complex. Missing values ( NAs) are allowed.

VALUE:

numeric or complex result, with the shorter argument used cyclically, if necessary: +, -, * and / are the usual arithmetic operators and ^ is exponentiation.
%/% is integer divide; the operands should be numeric and the result is floor(e1/e2) if e2!=0 and 0 if e2==0.
%% is the modulo function; it also expects numeric operands and is defined as e1-floor(e1/e2)*e2 if e2!=0 and e1 otherwise (see Knuth, 1968, section 1.2.4). Thus %/% and %% always satisfy e1==(e1%/%e2)*e2+e1%%e2

Classes:

This function will be used as the default method for classes that do not inherit a specific method for the function or for the Ops group of functions. The result will retain the class and the attributes. If this behavior is not appropriate, the designer of the class should provide a method for the function or for the Ops group

DETAILS:

For ^ with numeric arguments and negative e1: if e2 is not an integer, the value is NA; if e2 is a negative integer, the value is 1/(e1^(-e2)). A machine-dependent test is performed to decide if a number is exactly or nearly an integer; do not count on its behavior in doubtful cases.
Section 5.6.1 of Becker, Chambers and Wilks describes the rules for dealing with operands possessing attributes. Also see section 5.1.5 for details on domains and branch cuts in the case of complex arguments for exponentiation.
The .Uminus function performs the unary minus operation. For example, -2 is the same as .Uminus(2).
These are generic functions since they are part of the "Arith" group within the "Ops" group. Group methods exist for data frames. They also exist for factors and ordered factors, but arithmetic is not allowed for these classes.
In S-PLUS 5.0 and later, zero-length operands in arithmetic operations force zero-length results. That is, numeric(0) + anything yields numeric(0). Earlier versions of S-PLUS returned a vector of NAs as long as the longer operand.

REFERENCES:

Knuth, D. E. (1968). The Art of Computer Programming, Volume 1: Fundamental Algorithms Addison-Wesley, Reading, Mass.
Becker, R.A., Chambers, J.M., and Wilks, A.R. (1989). The New S Language Wadsworth, Belmont, CA.

SEE ALSO:

, , , , .

EXAMPLES:

x-mean(x)  # deviations from the mean; second argument used repeatedly 
(1+(5:8)/1200)^12     # compound interest, 5:8 per annum monthly 
get("%/%") # print the definition