Andrea Gonnermann
2012-Sep-11 14:23 UTC
[R] R calculates small numbers, where the result should be zero
Hi all, my problem is that R does wrong calculations, when the result should be zero. I have the following code: RD_Var1 <- ((0.1*(1-0.1))/100)+((0.2*(1-0.2))/100) RD_Var2 <- ((0.1*(1-0.1))/100)+((0.2*(1-0.2))/100) RD_1 <- 0.1-0.2 RD_1 RD_2 <- 0.1-0.2 FEM_Eff_RD <- ((1/RD_Var1)*RD_1+(1/RD_Var2)*RD_2)/(1/RD_Var1+1/RD_Var2) Q_RD <- 1/RD_Var1*((RD_1-FEM_Eff_RD)^2)+1/RD_Var2*((RD_2-FEM_Eff_RD)^2) Q_RD My result is: 1.540744e-31 When I put the calculated numbers directly into the last formula, I get zero, which is the correct answer: Q_RD <- 1/0.0025*((-0.1--0.1)^2)+1/0.0025*((-0.1--0.1)^2) Why does R calculate the other number instead of zero? Thank you very much for your help!
PIKAL Petr
2012-Sep-11 14:48 UTC
[R] R calculates small numbers, where the result should be zero
Hi> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Andrea Gonnermann > Sent: Tuesday, September 11, 2012 4:24 PM > To: r-help at r-project.org > Subject: [R] R calculates small numbers, where the result should be > zero > > Hi all, > > my problem is that R does wrong calculations, when the result should be > zero. > I have the following code: > > RD_Var1 <- ((0.1*(1-0.1))/100)+((0.2*(1-0.2))/100) > RD_Var2 <- ((0.1*(1-0.1))/100)+((0.2*(1-0.2))/100) > RD_1 <- 0.1-0.2 > RD_1 > RD_2 <- 0.1-0.2 > FEM_Eff_RD <- > ((1/RD_Var1)*RD_1+(1/RD_Var2)*RD_2)/(1/RD_Var1+1/RD_Var2) > Q_RD <- 1/RD_Var1*((RD_1-FEM_Eff_RD)^2)+1/RD_Var2*((RD_2- > FEM_Eff_RD)^2) > Q_RD > My result is: > 1.540744e-31 > When I put the calculated numbers directly into the last formula, I get > zero, which is the correct answer: > Q_RD <- 1/0.0025*((-0.1--0.1)^2)+1/0.0025*((-0.1--0.1)^2) > > Why does R calculate the other number instead of zero?Because you are probably using computer. Some fractional decimal numbers cannot be expressed as binary finite precision numbers. You can round your result to get 0. BTW it is covered in FAQ 7.31 (I believe) Regards Petr> > Thank you very much for your help! > > ______________________________________________ > 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.
S Ellison
2012-Sep-11 14:51 UTC
[R] R calculates small numbers, where the result should be zero
See R Faq (http://cran.r-project.org/doc/FAQ/R-FAQ.html) with particular attention to FAQs 7.31 and 9.1 paragraph 3. Essentially, computational precision is finite. S Ellison> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Andrea Gonnermann > Sent: 11 September 2012 15:24 > To: r-help at r-project.org > Subject: [R] R calculates small numbers, where the result > should be zero > > Hi all, > > my problem is that R does wrong calculations, when the result > should be zero. > I have the following code: > > RD_Var1 <- ((0.1*(1-0.1))/100)+((0.2*(1-0.2))/100) > RD_Var2 <- ((0.1*(1-0.1))/100)+((0.2*(1-0.2))/100) > RD_1 <- 0.1-0.2 > RD_1 > RD_2 <- 0.1-0.2 > FEM_Eff_RD <- > ((1/RD_Var1)*RD_1+(1/RD_Var2)*RD_2)/(1/RD_Var1+1/RD_Var2) > Q_RD <- > 1/RD_Var1*((RD_1-FEM_Eff_RD)^2)+1/RD_Var2*((RD_2-FEM_Eff_RD)^2) > Q_RD > My result is: > 1.540744e-31 > When I put the calculated numbers directly into the last > formula, I get zero, which is the correct answer: > Q_RD <- 1/0.0025*((-0.1--0.1)^2)+1/0.0025*((-0.1--0.1)^2) > > Why does R calculate the other number instead of zero? > > Thank you very much for your help! > > ______________________________________________ > 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. >******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
Bryan Hanson
2012-Sep-11 14:52 UTC
[R] R calculates small numbers, where the result should be zero
This is the result of how any computer program stores numbers, it's not unique to R. A slightly different question is discussed in R FAQ 7.31 but it applies to your situation. You may also enjoy the R Inferno at www.burns-stat.com/pages/Tutor/R_inferno.pdf Bryan *********** Bryan Hanson Professor of Chemistry & Biochemistry DePauw University On Sep 11, 2012, at 10:23 AM, Andrea Gonnermann <dreli14 at googlemail.com> wrote:> Hi all, > > my problem is that R does wrong calculations, when the result should be zero. > I have the following code: > > RD_Var1 <- ((0.1*(1-0.1))/100)+((0.2*(1-0.2))/100) > RD_Var2 <- ((0.1*(1-0.1))/100)+((0.2*(1-0.2))/100) > RD_1 <- 0.1-0.2 > RD_1 > RD_2 <- 0.1-0.2 > FEM_Eff_RD <- ((1/RD_Var1)*RD_1+(1/RD_Var2)*RD_2)/(1/RD_Var1+1/RD_Var2) > Q_RD <- 1/RD_Var1*((RD_1-FEM_Eff_RD)^2)+1/RD_Var2*((RD_2-FEM_Eff_RD)^2) > Q_RD > My result is: > 1.540744e-31 > When I put the calculated numbers directly into the last formula, I > get zero, which is the correct answer: > Q_RD <- 1/0.0025*((-0.1--0.1)^2)+1/0.0025*((-0.1--0.1)^2) > > Why does R calculate the other number instead of zero? > > Thank you very much for your help! > > ______________________________________________ > 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.
Andrea Gonnermann
2012-Sep-11 15:02 UTC
[R] R calculates small numbers, where the result should be zero
Many,many thanks to all of you and your helpful links! I actually experineced something similar in SAS and my "research" on google was not good enough to figure it out! Well, I may just round the numbers to some digits as proposed So, thank you again for this fast response!! Have a nice day :-) Andrea On Tue, Sep 11, 2012 at 4:48 PM, PIKAL Petr <petr.pikal at precheza.cz> wrote:> Hi > >> -----Original Message----- >> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- >> project.org] On Behalf Of Andrea Gonnermann >> Sent: Tuesday, September 11, 2012 4:24 PM >> To: r-help at r-project.org >> Subject: [R] R calculates small numbers, where the result should be >> zero >> >> Hi all, >> >> my problem is that R does wrong calculations, when the result should be >> zero. >> I have the following code: >> >> RD_Var1 <- ((0.1*(1-0.1))/100)+((0.2*(1-0.2))/100) >> RD_Var2 <- ((0.1*(1-0.1))/100)+((0.2*(1-0.2))/100) >> RD_1 <- 0.1-0.2 >> RD_1 >> RD_2 <- 0.1-0.2 >> FEM_Eff_RD <- >> ((1/RD_Var1)*RD_1+(1/RD_Var2)*RD_2)/(1/RD_Var1+1/RD_Var2) >> Q_RD <- 1/RD_Var1*((RD_1-FEM_Eff_RD)^2)+1/RD_Var2*((RD_2- >> FEM_Eff_RD)^2) >> Q_RD >> My result is: >> 1.540744e-31 >> When I put the calculated numbers directly into the last formula, I get >> zero, which is the correct answer: >> Q_RD <- 1/0.0025*((-0.1--0.1)^2)+1/0.0025*((-0.1--0.1)^2) >> >> Why does R calculate the other number instead of zero? > > Because you are probably using computer. Some fractional decimal numbers cannot be expressed as binary finite precision numbers. You can round your result to get 0. BTW it is covered in FAQ 7.31 (I believe) > > Regards > Petr > >> >> Thank you very much for your help! >> >> ______________________________________________ >> 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.