Arithmetic
Arithmetic Operators
Description
The arithmetic operations of addition, subtraction, multiplication,
division, exponentiation, integer division, and modulo. All except
integer division and modulo accept both complex and numeric arguments.
Integer division and modulo accept only numeric arguments.
Usage
e1 op e2
Arguments
op |
+, -, *, /, ^, %/% or
%%.
+, -, * and / are the usual arithmetic
operators.
^ is exponentiation.
%/% is integer division.
%% is modulo.
Integer division (%/%) and modulo
(%%) always satisfy e1==(e1%/%e2)*e2+e1%%e2.
|
e1,e2 |
numeric or complex. Missing values (NAs) are allowed.
For both integer divison and modulo, the operands must be numeric.
|
Details
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.
These are generic functions, because 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.
Zero-length operands in arithmetic operations force
zero-length results. That is, numeric(0) + anything yields
numeric(0).
Value
+, -, *, /, and ^ (arithmetic
functions) | returns a numeric or complex result, with the shorter
argument used cyclically if necessary. |
%/% (integer division) | if e2!=0,
returns floor(e1/e2).
If e2==0, and if e1 is an integer, returns 0.
Otherwise, returns Inf. |
%% (modulo) | returns e1-floor(e1/e2)*e2.
(See Knuth, 1968, section 1.2.4.). |
Classes
This function is 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 retains 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.
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=runif(10)
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