e1 op e2
+
,
-
,
*
,
/
,
^
, %/% or %%.
NA
s) are allowed.
+
,
-
,
*
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
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
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
NA
s as long as the longer
operand.
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.
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