Re: [Rd] Numerical error in R (win32) (PR#8909)
Hi Thanks for the quick reply. However, I am not satisfied, as > round(3.1500, 1) [1] 3.1 > round(3.7500, 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Peter Dalgaard Sent: Monday, May 29, 2006 17:49 To: [EMAIL PROTECTED] Cc: r-devel@stat.math.ethz.ch; [EMAIL PROTECTED] Subject: Re: [Rd] Numerical error in R (win32) (PR#8909) [EMAIL PROTECTED] 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 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Numerical error in R (win32) (PR#8909)
Hi Thanks for the quick reply. However, I am not satisfied, as > round(3.1500, 1) [1] 3.1 > round(3.7500, 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: Peter Ehlers [mailto:[EMAIL PROTECTED] Sent: Monday, May 29, 2006 17:45 To: [EMAIL PROTECTED] Cc: r-devel@stat.math.ethz.ch Subject: Re: [Rd] Numerical error in R (win32) (PR#8909) Did you check the Details section of the help page for round()? Peter Ehlers [EMAIL PROTECTED] 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@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Numerical error in R (win32) (PR#8909)
Hi Thanks for the quick reply. However, I am not satisfied, as > round(3.1500, 1) [1] 3.1 > round(3.7500, 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: Duncan Murdoch [mailto:[EMAIL PROTECTED] Sent: Monday, May 29, 2006 17:41 To: [EMAIL PROTECTED] Cc: r-devel@stat.math.ethz.ch; [EMAIL PROTECTED] Subject: Re: [Rd] Numerical error in R (win32) (PR#8909) On 5/29/2006 12:22 PM, [EMAIL PROTECTED] 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 This 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 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel