Cristina Fdez. Aragón
2012-Sep-25 23:39 UTC
[R] small negative values instead of zeros in nested loop
Hi,I am trying to run a code with a series of nested for loops, here is a simplified version of it: J<-seq(0,1,0.2) D<-seq(0,1,0.2) for (x in 1:10) { for (j in 1:length(J)) { for (d in 1:(length(D)-j+1)) { x.end<-x*(1-J[j]-D[d]) print(x) print(J[j]) print(D[d]) print(x.end) } }} The problem is that whenever x.end should be zero it takes a small negative value really close to zero instead. Any idea of why this might be happening and how can I fix it?Thanks! [[alternative HTML version deleted]]
Jim Holtman
2012-Sep-26 01:47 UTC
[R] small negative values instead of zeros in nested loop
FAQ 7.31 then look at 'all.equal' Sent from my iPad On Sep 25, 2012, at 19:39, Cristina Fdez. Arag?n <cf_aragon at hotmail.com> wrote:> > > > > Hi,I am trying to run a code with a series of nested for loops, here is a simplified version of it: > J<-seq(0,1,0.2) D<-seq(0,1,0.2) > for (x in 1:10) { for (j in 1:length(J)) { for (d in 1:(length(D)-j+1)) { x.end<-x*(1-J[j]-D[d]) print(x) print(J[j]) print(D[d]) print(x.end) } }} > The problem is that whenever x.end should be zero it takes a small negative value really close to zero instead. Any idea of why this might be happening and how can I fix it?Thanks! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
Daniel Nordlund
2012-Sep-26 06:37 UTC
[R] small negative values instead of zeros in nested loop
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Cristina Fdez. Arag?n > Sent: Tuesday, September 25, 2012 4:40 PM > To: r-help at r-project.org > Subject: [R] small negative values instead of zeros in nested loop > > > > > > Hi,I am trying to run a code with a series of nested for loops, here is a > simplified version of it: > J<-seq(0,1,0.2) D<-seq(0,1,0.2) > > for (x in 1:10) { for (j > in 1:length(J)) { for (d > in 1:(length(D)-j+1)) { > x.end<-x*(1-J[j]-D[d]) > print(x) print(J[j]) > print(D[d]) print(x.end) } > }} > The problem is that whenever x.end should be zero it takes a small > negative value really close to zero instead. Any idea of why this might be > happening and how can I fix it?Thanks! >The reason for why this happens can be found in R FAQ 7.31. Dan Daniel Nordlund Bothell, WA USA