teck.lim at imperial.ac.uk
2006-May-29 16:22 UTC
[Rd] Numerical error in R (win32) (PR#8909)
Hi I had observed the following problem in R (also C, Matlab, and Python). sprintf('%1.2g\n', 3.15) give 3.1 instead of 3.2 whereas an input of 3.75 gives 3.8. Java's System.out.printf is ok though.> round(3.75,1)[1] 3.8> round(3.15,1)[1] 3.1 Similar outcome with sprintf in R. However, the right answer should be 3.2 Regards Teckpor [[alternative HTML version deleted]]
On 5/29/2006 12:22 PM, teck.lim at imperial.ac.uk wrote:> Hi > I had observed the following problem in R (also C, Matlab, and Python). > sprintf('%1.2g\n', 3.15) > give 3.1 instead of 3.2 whereas an input of 3.75 gives 3.8. > Java's System.out.printf is ok though. > >> round(3.75,1) > [1] 3.8 >> round(3.15,1) > [1] 3.1 > > Similar outcome with sprintf in R. > > > However, the right answer should be 3.2This is not a bug. There is no way to represent 3.15 exactly in double precision, so it is hard to predict whether it will round up or down. Apparently on the machine you were using it is represented as a number slightly less than 3.15, which rounds down. Duncan Murdoch
ligges at statistik.uni-dortmund.de
2006-May-29 16:44 UTC
[Rd] Numerical error in R (win32) (PR#8909)
teck.lim at imperial.ac.uk wrote:> Hi > I had observed the following problem in R (also C, Matlab, and Python). > sprintf('%1.2g\n', 3.15) > give 3.1 instead of 3.2 whereas an input of 3.75 gives 3.8. > Java's System.out.printf is ok though. > > >>round(3.75,1) > > [1] 3.8 > >>round(3.15,1) > > [1] 3.1 > > Similar outcome with sprintf in R. > > > However, the right answer should be 3.2This is not a bug. Please read the R FAQ "Why doesn't R think these numbers are equal?" and after that note that *numerically* the following inaqualities are TRUE: (3.2 - 3.15) > 0.05 (3.15 - 3.1) < 0.05 Uwe Ligges> Regards > Teckpor > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Did you check the Details section of the help page for round()? Peter Ehlers teck.lim at imperial.ac.uk wrote:> Hi > I had observed the following problem in R (also C, Matlab, and Python). > sprintf('%1.2g\n', 3.15) > give 3.1 instead of 3.2 whereas an input of 3.75 gives 3.8. > Java's System.out.printf is ok though. > > >>round(3.75,1) > > [1] 3.8 > >>round(3.15,1) > > [1] 3.1 > > Similar outcome with sprintf in R. > > > However, the right answer should be 3.2 > > Regards > Teckpor > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
teck.lim at imperial.ac.uk writes:> Hi > I had observed the following problem in R (also C, Matlab, and Python). > sprintf('%1.2g\n', 3.15) > give 3.1 instead of 3.2 whereas an input of 3.75 gives 3.8. > Java's System.out.printf is ok though. > > > round(3.75,1) > [1] 3.8 > > round(3.15,1) > [1] 3.1 > > Similar outcome with sprintf in R. > > > However, the right answer should be 3.2According to what? Remember that we're dealing with finite precision binary arithmetic here:> (3.15 - 3.1)<.05[1] TRUE> abs(3.15 - 3.2)>.05[1] TRUE See also FAQ 7.31.> Regards > Teckpor >-- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
teck.lim at imperial.ac.uk
2006-May-30 12:43 UTC
[Rd] Numerical error in R (win32) (PR#8909)
Hi Thanks for the quick reply. However, I am not satisfied, as> round(3.15000000, 1)[1] 3.1> round(3.75000000, 1)[1] 3.8 I think the problem is really more of an error in the rounding off algorithm than finite precision. Thanks Teckpor -----Original Message----- From: Uwe Ligges [mailto:ligges at statistik.uni-dortmund.de] Sent: Monday, May 29, 2006 17:45 To: teck.lim at imperial.ac.uk Cc: R-bugs at biostat.ku.dk Subject: Re: [Rd] Numerical error in R (win32) (PR#8909) teck.lim at imperial.ac.uk wrote:> Hi > I had observed the following problem in R (also C, Matlab, andPython).> sprintf('%1.2g\n', 3.15) > give 3.1 instead of 3.2 whereas an input of 3.75 gives 3.8. > Java's System.out.printf is ok though. > > >>round(3.75,1) > > [1] 3.8 > >>round(3.15,1) > > [1] 3.1 > > Similar outcome with sprintf in R. > > > However, the right answer should be 3.2This is not a bug. Please read the R FAQ "Why doesn't R think these numbers are equal?" and after that note that *numerically* the following inaqualities are TRUE: (3.2 - 3.15) > 0.05 (3.15 - 3.1) < 0.05 Uwe Ligges> Regards > Teckpor > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
p.dalgaard at biostat.ku.dk
2006-May-30 13:05 UTC
[Rd] Numerical error in R (win32) (PR#8909)
"ltp" <teck.lim at imperial.ac.uk> writes:> Hi > > Thanks for the quick reply. However, I am not satisfied, as > > round(3.15000000, 1) > [1] 3.1 > > round(3.75000000, 1) > [1] 3.8 > > I think the problem is really more of an error in the rounding off > algorithm than finite precision.It isn't, and adding zeros doesn't change anything. The issue is that 3.15 has a nonterminating binary expansion, just like 1/7 has a nonterminating decimal one. Please do read the references that have already been provided to you. 3.15 (dec) == 11.0010011001100110011... (bin) 3.75 (dec) == 11.11 (bin)> Thanks > Teckpor > > -----Original Message----- > From: pd at pubhealth.ku.dk [mailto:pd at pubhealth.ku.dk] On Behalf Of Peter > Dalgaard > Sent: Monday, May 29, 2006 17:49 > To: teck.lim at imperial.ac.uk > Cc: r-devel at stat.math.ethz.ch; R-bugs at biostat.ku.dk > Subject: Re: [Rd] Numerical error in R (win32) (PR#8909) > > teck.lim at imperial.ac.uk writes: > > > Hi > > I had observed the following problem in R (also C, Matlab, and > Python). > > sprintf('%1.2g\n', 3.15) > > give 3.1 instead of 3.2 whereas an input of 3.75 gives 3.8. > > Java's System.out.printf is ok though. > > > > > round(3.75,1) > > [1] 3.8 > > > round(3.15,1) > > [1] 3.1 > > > > Similar outcome with sprintf in R. > > > > > > However, the right answer should be 3.2 > > According to what? Remember that we're dealing with finite precision binary > arithmetic here: > > > (3.15 - 3.1)<.05 > [1] TRUE > > abs(3.15 - 3.2)>.05 > [1] TRUE > > See also FAQ 7.31. > > > Regards > > Teckpor > > > > -- > O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B > c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K > (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 > ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 > >-- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907