Hi, there, I cannot get accurate value for calculation. for example: ld<-sqrt(1*0.05*0.95*0.05*0.95) 0.05*0.95-ld=-6.938894e-18 0.05*0.95-ld==0 is False. I met this problem in my program, how can I handle it. Thanks. xj.
FAQ 7.31 (You need to understand what floating point numbers are) On 3/3/08, Xuejun Qin <xjq at chg.duhs.duke.edu> wrote:> Hi, there, > I cannot get accurate value for calculation. > for example: > ld<-sqrt(1*0.05*0.95*0.05*0.95) > 0.05*0.95-ld=-6.938894e-18 > 0.05*0.95-ld==0 is False. > > I met this problem in my program, how can I handle it. Thanks. > > > xj. > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
On Mon, 2008-03-03 at 12:52 -0500, Xuejun Qin wrote:> Hi, there, > I cannot get accurate value for calculation. > for example: > ld<-sqrt(1*0.05*0.95*0.05*0.95) > 0.05*0.95-ld=-6.938894e-18 > 0.05*0.95-ld==0 is False. > > I met this problem in my program, how can I handle it. Thanks.Answer 1: Read FAQ 7.31 - http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f Answer 2: Use all.equal()> ld<-sqrt(1*0.05*0.95*0.05*0.95) > 0.05*0.95-ld[1] -6.938894e-18> 0.05*0.95-ld==0[1] FALSE> all.equal(0.05*0.95-ld, 0)[1] TRUE> > > xj.HTH G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Uwe Ligges
2008-Mar-03 17:58 UTC
[R] looking for some reading on accuracy of binary computers and all.equal(); was: help for the first poster- a simple question
Xuejun Qin wrote:> Hi, there, > I cannot get accurate value for calculation. > for example: > ld<-sqrt(1*0.05*0.95*0.05*0.95) > 0.05*0.95-ld=-6.938894e-18 > 0.05*0.95-ld==0 is False. > > I met this problem in my program, how can I handle it. Thanks. > > > xj. > > ______________________________________________ > 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.Yes, PLEASE do read the posting guide, first poster! There you will find that there are FAQs where your first poster's question is already answered. And that the subject line can be used in a sensible manner. Best, Uwe Ligges
R FAQ 7.31. G. On Mon, Mar 03, 2008 at 12:52:43PM -0500, Xuejun Qin wrote:> Hi, there, > I cannot get accurate value for calculation. > for example: > ld<-sqrt(1*0.05*0.95*0.05*0.95) > 0.05*0.95-ld=-6.938894e-18 > 0.05*0.95-ld==0 is False. > > I met this problem in my program, how can I handle it. Thanks. > > > xj. > > ______________________________________________ > 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.-- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
Marianne Promberger
2008-Mar-03 18:49 UTC
[R] help for the first poster- a simple question
On 03/03/08 12:52, Xuejun Qin wrote:> Hi, there, > I cannot get accurate value for calculation. > for example: > ld<-sqrt(1*0.05*0.95*0.05*0.95) > 0.05*0.95-ld=-6.938894e-18 > 0.05*0.95-ld==0 is False. > > I met this problem in my program, how can I handle it. Thanks.I think what you are experiencing is this: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f (R FAQ "7.31 Why doesn't R think these numbers are equal?") Maybe handle it using round(). HTH m.
Thanks for all response. Here is more closer to my question, p<-0.05 f<-0.05 ld<-sqrt(f* (1 - f) * p * (1 - p)) D <- (p * (1 - f) - ld)/p d <- ((1 - p) * f - ld)/(1 - p) haplo<-data.frame(D,d)> haploD d 1 -1.387779e-16 -7.304099e-18 cond1<-pmin(haplo[,1],haplo[,2]) haplo[cond1>=0,] But the program cannot get right one, because -1.387779e-16>=0 is FALSE -7.304099e-18>=0 is FALSE Thank you! xj. ----- Original Message ----- From: "Gavin Simpson" <gavin.simpson at ucl.ac.uk> To: "Xuejun Qin" <xjq at chg.duhs.duke.edu> Cc: <r-help at r-project.org> Sent: Monday, March 03, 2008 12:57 PM Subject: Re: [R] help for the first poster- a simple question> On Mon, 2008-03-03 at 12:52 -0500, Xuejun Qin wrote: >> Hi, there, >> I cannot get accurate value for calculation. >> for example: >> ld<- >> 0.05*0.95-ld=-6.938894e-18 >> 0.05*0.95-ld==0 is False. >> >> I met this problem in my program, how can I handle it. Thanks. > > Answer 1: > > Read FAQ 7.31 - > > http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f > > Answer 2: > > Use all.equal() > >> ld<-sqrt(1*0.05*0.95*0.05*0.95) >> 0.05*0.95-ld > [1] -6.938894e-18 >> 0.05*0.95-ld==0 > [1] FALSE >> all.equal(0.05*0.95-ld, 0) > [1] TRUE > >> >> >> xj. > > HTH > > G > -- > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% > Dr. Gavin Simpson [t] +44 (0)20 7679 0522 > ECRC, UCL Geography, [f] +44 (0)20 7679 0565 > Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk > Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ > UK. WC1E 6BT. [w] http://www.freshwaters.org.uk > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% > >
Hi r-help-bounces at r-project.org napsal dne 03.03.2008 20:12:34:> Thanks for all response. Here is more closer to my question, > p<-0.05 > > f<-0.05 > > ld<-sqrt(f* (1 - f) * p * (1 - p)) > > D <- (p * (1 - f) - ld)/p > > d <- ((1 - p) * f - ld)/(1 - p) > > haplo<-data.frame(D,d) > > > haplo > D d > 1 -1.387779e-16 -7.304099e-18 > > cond1<-pmin(haplo[,1],haplo[,2]) > > haplo[cond1>=0,]Either do not use computers as they sometimes can not represent exactly fractional values. Or use rounding, haplo[round(cond1, 10)>=0,] Regards Petr> > But the program cannot get right one, because > > > -1.387779e-16>=0 is FALSE > > -7.304099e-18>=0 is FALSE > > > > Thank you! > > xj. > > > ----- Original Message ----- > From: "Gavin Simpson" <gavin.simpson at ucl.ac.uk> > To: "Xuejun Qin" <xjq at chg.duhs.duke.edu> > Cc: <r-help at r-project.org> > Sent: Monday, March 03, 2008 12:57 PM > Subject: Re: [R] help for the first poster- a simple question > > > > On Mon, 2008-03-03 at 12:52 -0500, Xuejun Qin wrote: > >> Hi, there, > >> I cannot get accurate value for calculation. > >> for example: > >> ld<- > >> 0.05*0.95-ld=-6.938894e-18 > >> 0.05*0.95-ld==0 is False. > >> > >> I met this problem in my program, how can I handle it. Thanks. > > > > Answer 1: > > > > Read FAQ 7.31 - > > > >http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-> numbers-are-equal_003f > > > > Answer 2: > > > > Use all.equal() > > > >> ld<-sqrt(1*0.05*0.95*0.05*0.95) > >> 0.05*0.95-ld > > [1] -6.938894e-18 > >> 0.05*0.95-ld==0 > > [1] FALSE > >> all.equal(0.05*0.95-ld, 0) > > [1] TRUE > > > >> > >> > >> xj. > > > > HTH > > > > G > > -- > > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% > > Dr. Gavin Simpson [t] +44 (0)20 7679 0522 > > ECRC, UCL Geography, [f] +44 (0)20 7679 0565 > > Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk > > Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ > > UK. WC1E 6BT. [w] http://www.freshwaters.org.uk > > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% > > > > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.