On 2010-10-09 4:47, skan wrote:

Hello

I've seen the answer at stackoverflow.
They also said I must use zapsmall to avoid roundup problems.
I didn't expect this behaviour when division gives an integer number.

The trouble is that your expectations may not coincide with reality.
That's why people refer you to FAQ 7.31.

Even replacing the rep(0, ....) with just 0 will not necessarily
give the expected result:

 eps1 <- 1e-16
 eps2 <- 1e-15

 ## try to generate a 3-by-4 matrix:

 matrix(0, nrow = 3 - eps1, ncol = 4)
 #     [,1] [,2] [,3] [,4]
 #[1,]    0    0    0    0
 #[2,]    0    0    0    0
 #[3,]    0    0    0    0


 matrix(0, nrow = 3 - eps2, ncol = 4)
 #     [,1] [,2] [,3] [,4]
 #[1,]    0    0    0    0
 #[2,]    0    0    0    0


 matrix(0, nrow = zapsmall(3 - eps2), ncol = 4)
 #     [,1] [,2] [,3] [,4]
 #[1,]    0    0    0    0
 #[2,]    0    0    0    0
 #[3,]    0    0    0    0


 ## Note that your calculation did _not_ yield an integer:
  1 + ((1.5 - 0.1) / 0.05) - 29
 #[1] -3.552714e-15

Such are the vagaries of floating-point arithmetic. Play it
safe; use zapsmall.

  -Peter Ehlers

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to