Dear All, Is this right? > floor((5.05-floor(5))*100) [1] 4 I would expect 5, or am I wrong? Thanks and regards, W --------------------------------- [[alternative HTML version deleted]]
G'day Werner,>>>>> "WB" == Werner Bier <aliscla at yahoo.com> writes:>> floor((5.05-floor(5))*100) WB> [1] 4 WB> I would expect 5, or am I wrong? You are wrong. :) Consider:> (5.05-floor(5))*100[1] 5> (5.05-floor(5))*100 - 5[1] -1.776357e-14 and read FAQ 7.31 Cheers, Berwin ========================== Full address ===========================Berwin A Turlach Tel.: +61 (8) 6488 3338 (secr) School of Mathematics and Statistics +61 (8) 6488 3383 (self) The University of Western Australia FAX : +61 (8) 6488 1028 35 Stirling Highway Crawley WA 6009 e-mail: berwin at maths.uwa.edu.au Australia http://www.maths.uwa.edu.au/~berwin
I believe this is a FAQ. Examine:> format((5.05-floor(5))*100, nsmall=16)[1] "4.9999999999999822"> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Werner Bier > Sent: Tuesday, November 29, 2005 3:35 PM > To: r-help at stat.math.ethz.ch > Subject: [R] floor() > > > Dear All, > > Is this right? > > > floor((5.05-floor(5))*100) > [1] 4 > > I would expect 5, or am I wrong? > > Thanks and regards, > W > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
On 29-Nov-05 Werner Bier wrote:> Dear All, > > Is this right? > > > floor((5.05-floor(5))*100) > [1] 4 > > I would expect 5, or am I wrong? > > Thanks and regards, > WIt may seem reasonable to expect it, but in the case of R (and most other computer languages) you would be wrong. The reason: print((5.05-floor(5))*100,digits=20) [1] 4.9999999999999822 whose floor() is 4. The underlying reason for this and all similar phenomena is the slight imprecision of floating-point arithmetic when the fractional part is not a multiple of 1/2^k for some k. Since 0.05 = 1/20 and 20 = 4*5, you have a factor 1/5 in there and the imprecision will occur. If you really *know* what you are doing in a particular context, you can guard against it by a deliberate tiny mistake, such as dtm <- 1e-13 floor((5.05-floor(5))*100 + dtm) [1] 5 but you have to be careful that you don't let this happen when it should not happen. And you have to choose your dtm with care: 1e-14 is not good enough! Though, since the trouble really arises at the (5.05-floor(5)) level, you could use, more judiciously, dtm <- 1e-15 floor((5.05-floor(5)+dtm)*100) [1] 5 and here 1e-16 won't work. Check: print((5.05-floor(5)),digits=20) [1] 0.049999999999999822 where the last "9" is the 15th digit after the ".". Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 30-Nov-05 Time: 00:09:44 ------------------------------ XFMail ------------------------------