Coerce Small Numbers to Zero for Printing

DESCRIPTION:

Returns an object like the input but with numbers which are close to zero, and much smaller than the largest values of the input, rounded to zero.

USAGE:

zapsmall(x, digits = .Options$digits) 

REQUIRED ARGUMENTS:

x
numeric vector, matrix, or array.

OPTIONAL ARGUMENTS:

digits
number of digits to be printed after decimal point.

VALUE:

object like x but with small numbers "zapped" to be exactly zero. If x consists solely of small numbers, it is returned unmodified.

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 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.

SEE ALSO:

.

EXAMPLES:

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