[R] strange thing with sd

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Mon Mar 29 16:32:19 CEST 2004


Peter Flom wrote:
> I tried your sample code, and a few other variations, and got 0 for all
> of them, running R 1.8.1 on a Windows machine

  Not only do I get 0 on R 1.8.1 on Unix (linux, RH8), I get _exactly_ 
0. I thought this was going to be a simple arithmetic precision problem, 
but this test script shows the answers to be exactly zero:

zertest <- function(q){
   if( q != 0 ){
     theCall <- deparse(substitute(q))
     cat(paste("failed",theCall,' : got ',q,'\n',sep=' '))
   }
}

# these fail, as expected:
zertest(sd(c(1,2,3)))
zertest(sd(c(rep(0.01,12),0.00000001)))

# these pass as zero:
zertest(sd(rep(0.01,      13)))  #1.805557e-18
zertest(sd(rep(0.001,     13)))  #4.513894e-19
zertest(sd(rep(0.00001,   13)))  #0
zertest(sd(rep(0.00000001,13)))  #0

  Might be a compiler/config funny. Andreas: what 'UNIX' is this? Did 
you compile R yourself? Did you run 'make check'?

Baz




More information about the R-help mailing list