zapsmall(x, digits = .Options$digits)
x but with small numbers "zapped" to be exactly
zero. If
x consists solely of small numbers, it is returned
unmodified.
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 then 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.
c(sin(pi), cos(pi))
[1] 1.224606e-16 -1.000000e+00
zapsmall(c(sin(pi), cos(pi)))
[1] 0 -1
A <- matrix(c(2, 5, -4, 3), ncol = 2)
A %*% solve(A)
# Gives the following output:
[,1] [,2]
[1,] 1 -2.220446e-16
[2,] 0 1.000000e+00
zapsmall(.Last.value)
# Gives the following output:
[,1] [,2]
[1,] 1 0
[2,] 0 1