Hello, everyone, There's a problem about zero in R and I really need your help. I have a vector shown as x=c(0.1819711,0.4811463,0.1935151,0.1433675), The sum of this vector is shown as 1 in R, but when I type 1-sum(x), the value is not zero, but -2.220446e-16. I can accept that this value is quite small and could be seen as zero, but there would be a problem when it's not really zero but a negative value in my algorithm. Therefore I would like to know that how could it be avoid. One way I think is to define the value 1-sum(x) as zero when it is smaller than a particular value, but the particular value is not be set yet. I would like to know more about the definition of the shown zero in R. Thank you so much. Yen [[alternative HTML version deleted]]
Hi Yen, See http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f HTH, Jorge On Tue, Jun 15, 2010 at 6:00 PM, Yen Lee <> wrote:> Hello, everyone, > > There's a problem about zero in R and I really need your help. > > > > I have a vector shown as x=c(0.1819711,0.4811463,0.1935151,0.1433675), > > The sum of this vector is shown as 1 in R, but when I type 1-sum(x), the > value is not zero, but -2.220446e-16. > > I can accept that this value is quite small and could be seen as zero, but > there would be a problem when it's not really zero but a negative value in > my algorithm. > > > > Therefore I would like to know that how could it be avoid. > > One way I think is to define the value 1-sum(x) as zero when it is smaller > than a particular value, but the particular value is not be set yet. > > I would like to know more about the definition of the shown zero in R. > > > > Thank you so much. > > > > Yen > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Hello Yen, You may find ?zapsmall helpful. If your algorithm expects 1-sum(x) to have a lower bound of zero, then also simply setting a cutoff point seems reasonable. Josh On Tue, Jun 15, 2010 at 3:00 PM, Yen Lee <b88207001 at ntu.edu.tw> wrote:> Hello, everyone, > > There's a problem about zero in R and I really need your help. > > > > I have a vector shown as x=c(0.1819711,0.4811463,0.1935151,0.1433675), > > The sum of this vector is shown as 1 in R, but when I type 1-sum(x), the > value is not zero, but -2.220446e-16. > > I can accept that this value is quite small and could be seen as zero, but > there would be a problem when it's not really zero but a negative value in > my algorithm. > > > > Therefore I would like to know that how could it be avoid. > > One way I think is to define the value 1-sum(x) as zero when it is smaller > than a particular value, but the particular value is not be set yet. > > I would like to know more about the definition of the shown zero in R. > > > > Thank you so much. > > > > Yen > > > ? ? ? ?[[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. >-- Joshua Wiley Ph.D. Student Health Psychology University of California, Los Angeles
On Tue, Jun 15, 2010 at 6:00 PM, Yen Lee <b88207001 at ntu.edu.tw> wrote:> Hello, everyone, > > There's a problem about zero in R and I really need your help. > > > > I have a vector shown as x=c(0.1819711,0.4811463,0.1935151,0.1433675), > > The sum of this vector is shown as 1 in R, but when I type 1-sum(x), the > value is not zero, but -2.220446e-16. > > I can accept that this value is quite small and could be seen as zero, but > there would be a problem when it's not really zero but a negative value in > my algorithm. > > > > Therefore I would like to know that how could it be avoid. > > One way I think is to define the value 1-sum(x) as zero when it is smaller > than a particular value, but the particular value is not be set yet. > > I would like to know more about the definition of the shown zero in R. >As others have pointed out please read the R FAQ. Actually on my machine: Windows Vista running C2D BLAS, "R version 2.11.1 Patched (2010-05-31 r52167)" I do get zero:> x <- c(0.1819711,0.4811463,0.1935151,0.1433675) > 1-sum(x)[1] 0 Also check out sum.exact in the caTools package.