zapsmall
Coerce Small Numbers to Zero for Printing
Description
Returns an object like the input, but with numbers
that are close to zero and much smaller than the
largest values of the input, rounded to zero.
Usage
zapsmall(x, digits = getOption("digits"))
Arguments
x |
a numeric vector, a matrix, or an array.
|
digits |
the number of digits to be printed after the decimal point.
The default is the value of the digits option.
|
Details
The value specified for digits is
modified internally to be max(digits - log10(m), 0), where
m is max(abs(x[!is.na(x)])). This value is used as the
digits argument to the round function. Thus, if x consists solely
of very small numbers of approximately equal magnitude, no values
are zapped.
The usual use for zapsmall is to massage the results of double-precision
arithmetic, so that values that might be expected to be zero actually
appear as zero. For example, matrix multiplications that are supposed
to yield triangular or diagonal matrices often result in small values
in the off-diagonal entries. Using zapsmall can help you confirm
that the correct answer is being calculated.
Value
returns an object like x, but with small numbers "zapped" to be exactly
zero. If x consists solely of small numbers, it is returned
unmodified.
References
Chambers, John M. 1998. Programming with Data. New York, NY: Springer.
See Also
Examples
c(sin(pi), cos(pi))
zapsmall(c(sin(pi), cos(pi)))
e <- exp(1)*10^(-5:1)
print(e, digits = 4)
# 2.718e-05 2.718e-04 2.718e-03 2.718e-02 2.718e-01 2.718e+00 2.718e+01
zapsmall(e, digits = 4)
# 0.000 0.000 0.003 0.027 0.272 2.718 27.183