Cory Rieth
2010-Nov-28 21:20 UTC
[R] unexpected behavior using round to 2 digits on randomly generated numbers
Hello! I stumbled upon something odd that took a while to track down, and I wanted to run it by here to see if I should submit a bug report. For randomly generated numbers (from a variety of distributions) rounding them to specifically 2 digits and then multiplying them by 100 produces strange results on about 8% of cases. The problematic numbers display as I would have expected, but do not logically match the as.integer counterpart (additionally they will not be used correctly by functions such as rep()). I realize there are easy workarounds, but I wouldn't have expected this result, and it only occurs rounding to 2 decimals, i.e. changing digits to 3 and multiplying by 1000 after rounding gives the expected result. x<-runif(100) #generate some random numbers y<-round(x,digits=2)*100 #round them all to two decimals, then multiply them all by 100. I expected the results to all be integers sum(y!=as.integer(y)) #but on about 8% of the numbers they do not match the integer version x[which(y!=as.integer(y))] # a list of the problem numbers from the original distribution. They seem to be more common but not exclusive to .54 to .57 y[which(y!=as.integer(y))] #the numbers still display as would be expected, i.e. they are integers as.integer(y[which(y!=as.integer(y))]) # and sometimes display as the same number they are not logically identical to Thanks, and sorry if I came across something that is known, or it is meant to behave this way, I couldn't find anything. Cory Rieth R.version() output: platform x86_64-apple-darwin9.8.0 arch x86_64 os darwin9.8.0 system x86_64, darwin9.8.0 status major 2 minor 12.0 year 2010 month 10 day 15 svn rev 53317 language R version.string R version 2.12.0 (2010-10-15)
David Winsemius
2010-Nov-28 21:43 UTC
[R] unexpected behavior using round to 2 digits on randomly generated numbers
On Nov 28, 2010, at 4:20 PM, Cory Rieth wrote:> Hello! > > I stumbled upon something odd that took a while to track down, and I > wanted to run it by here to see if I should submit a bug report. For > randomly generated numbers (from a variety of distributions) > rounding them to specifically 2 digits and then multiplying them by > 100 produces strange results on about 8% of cases. The problematic > numbers display as I would have expected, but do not logically match > the as.integer counterpart (additionally they will not be used > correctly by functions such as rep()). I realize there are easy > workarounds, but I wouldn't have expected this result, and it only > occurs rounding to 2 decimals, i.e. changing digits to 3 and > multiplying by 1000 after rounding gives the expected result. > > x<-runif(100) #generate some random numbers > y<-round(x,digits=2)*100 #round them all to two decimals, then > multiply them all by 100. I expected the results to all be integers > sum(y!=as.integer(y)) #but on about 8% of the numbers they do not > match the integer version > x[which(y!=as.integer(y))] # a list of the problem numbers from the > original distribution. They seem to be more common but not exclusive > to .54 to .57 > y[which(y!=as.integer(y))] #the numbers still display as would be > expected, i.e. they are integers > as.integer(y[which(y!=as.integer(y))]) # and sometimes display as > the same number they are not logically identical to > > Thanks, and sorry if I came across something that is known, or it is > meant to behave this way, I couldn't find anything.It's one of the FAQ and probably the most F-ly of the FAQ's. #21 or #31 if I remember (vaguely) ... the one about why seq(0.1, 1, by=0.1) == (1:11)/10 returns 2 FALSE's. -- David.> Cory Rieth > > R.version() output: > platform x86_64-apple-darwin9.8.0 > arch x86_64 > os darwin9.8.0 > system x86_64, darwin9.8.0 > status > major 2 > minor 12.0 > year 2010 > month 10 > day 15 > svn rev 53317 > language R > version.string R version 2.12.0 (2010-10-15) > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Duncan Murdoch
2010-Nov-28 21:45 UTC
[R] unexpected behavior using round to 2 digits on randomly generated numbers
On 28/11/2010 4:20 PM, Cory Rieth wrote:> Hello! > > I stumbled upon something odd that took a while to track down, and I wanted to run it by here to see if I should submit a bug report. For randomly generated numbers (from a variety of distributions) rounding them to specifically 2 digits and then multiplying them by 100 produces strange results on about 8% of cases. The problematic numbers display as I would have expected, but do not logically match the as.integer counterpart (additionally they will not be used correctly by functions such as rep()). I realize there are easy workarounds, but I wouldn't have expected this result, and it only occurs rounding to 2 decimals, i.e. changing digits to 3 and multiplying by 1000 after rounding gives the expected result.This is presumably another version of FAQ 7.31. Very few numbers of the form n/100 are exactly representable in floating point, and the rounding error sometimes shows up when you multiply by 100. A simpler version of this is the following: > y <- 1:99 > y[(y/100)*100 != y] [1] 7 14 28 29 55 56 57 58 Duncan Murdoch> > x<-runif(100) #generate some random numbers > y<-round(x,digits=2)*100 #round them all to two decimals, then multiply them all by 100. I expected the results to all be integers > sum(y!=as.integer(y)) #but on about 8% of the numbers they do not match the integer version > x[which(y!=as.integer(y))] # a list of the problem numbers from the original distribution. They seem to be more common but not exclusive to .54 to .57 > y[which(y!=as.integer(y))] #the numbers still display as would be expected, i.e. they are integers > as.integer(y[which(y!=as.integer(y))]) # and sometimes display as the same number they are not logically identical to > > Thanks, and sorry if I came across something that is known, or it is meant to behave this way, I couldn't find anything. > > Cory Rieth > > R.version() output: > platform x86_64-apple-darwin9.8.0 > arch x86_64 > os darwin9.8.0 > system x86_64, darwin9.8.0 > status > major 2 > minor 12.0 > year 2010 > month 10 > day 15 > svn rev 53317 > language R > version.string R version 2.12.0 (2010-10-15) > ______________________________________________ > 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.
Peter Ehlers
2010-Nov-28 21:47 UTC
[R] unexpected behavior using round to 2 digits on randomly generated numbers
On 2010-11-28 13:20, Cory Rieth wrote:> Hello! > > I stumbled upon something odd that took a while to track down, and I wanted to run it by here to see if I should submit a bug report. For randomly generated numbers (from a variety of distributions) rounding them to specifically 2 digits and then multiplying them by 100 produces strange results on about 8% of cases. The problematic numbers display as I would have expected, but do not logically match the as.integer counterpart (additionally they will not be used correctly by functions such as rep()). I realize there are easy workarounds, but I wouldn't have expected this result, and it only occurs rounding to 2 decimals, i.e. changing digits to 3 and multiplying by 1000 after rounding gives the expected result. > > x<-runif(100) #generate some random numbers > y<-round(x,digits=2)*100 #round them all to two decimals, then multiply them all by 100. I expected the results to all be integers > sum(y!=as.integer(y)) #but on about 8% of the numbers they do not match the integer version > x[which(y!=as.integer(y))] # a list of the problem numbers from the original distribution. They seem to be more common but not exclusive to .54 to .57 > y[which(y!=as.integer(y))] #the numbers still display as would be expected, i.e. they are integers > as.integer(y[which(y!=as.integer(y))]) # and sometimes display as the same number they are not logically identical to > > Thanks, and sorry if I came across something that is known, or it is meant to behave this way, I couldn't find anything. >Hmm, how hard have you looked? I doubt that I'll be the first to remind you to check the FAQ. Peter Ehlers> Cory Rieth > > R.version() output: > platform x86_64-apple-darwin9.8.0 > arch x86_64 > os darwin9.8.0 > system x86_64, darwin9.8.0 > status > major 2 > minor 12.0 > year 2010 > month 10 > day 15 > svn rev 53317 > language R > version.string R version 2.12.0 (2010-10-15) > ______________________________________________ > 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.
Jeff Newmiller
2010-Nov-28 21:53 UTC
[R] unexpected behavior using round to 2 digits on randomly generated numbers
FAQ 7.31 http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f You find it odd that the result is not an integer, while I find the fact that your calculation actually works reliably with any number of digits surprising, and you should avoid assuming such behavior will continue in the future. Multiply by the appropriate range and THEN take the integer part. Patient: Doctor, it hurts when I ram my head into the wall! Doctor: Don't do that! Cory Rieth wrote:> Hello! > > I stumbled upon something odd that took a while to track down, and I wanted to run it by here to see if I should submit a bug report. For randomly generated numbers (from a variety of distributions) rounding them to specifically 2 digits and then multiplying them by 100 produces strange results on about 8% of cases. The problematic numbers display as I would have expected, but do not logically match the as.integer counterpart (additionally they will not be used correctly by functions such as rep()). I realize there are easy workarounds, but I wouldn't have expected this result, and it only occurs rounding to 2 decimals, i.e. changing digits to 3 and multiplying by 1000 after rounding gives the expected result. > > x<-runif(100) #generate some random numbers > y<-round(x,digits=2)*100 #round them all to two decimals, then multiply them all by 100. I expected the results to all be integers > sum(y!=as.integer(y)) #but on about 8% of the numbers they do not match the integer version > x[which(y!=as.integer(y))] # a list of the problem numbers from the original distribution. They seem to be more common but not exclusive to .54 to .57 > y[which(y!=as.integer(y))] #the numbers still display as would be expected, i.e. they are integers > as.integer(y[which(y!=as.integer(y))]) # and sometimes display as the same number they are not logically identical to > > Thanks, and sorry if I came across something that is known, or it is meant to behave this way, I couldn't find anything. > > Cory Rieth > > R.version() output: > platform x86_64-apple-darwin9.8.0 > arch x86_64 > os darwin9.8.0 > system x86_64, darwin9.8.0 > status > major 2 > minor 12.0 > year 2010 > month 10 > day 15 > svn rev 53317 > language R > version.string R version 2.12.0 (2010-10-15) > ______________________________________________ > 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. >
Petr Savicky
2010-Nov-29 15:17 UTC
[R] unexpected behavior using round to 2 digits on randomly generated numbers
On Sun, Nov 28, 2010 at 01:53:05PM -0800, Jeff Newmiller wrote:> FAQ 7.31 > > http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003fAdditional information concerning rounding errors of double precision and suggestions for R code, which avoids them in some situations, may be found in the first section of http://rwiki.sciviews.org/doku.php?id=misc:r_accuracy and in http://rwiki.sciviews.org/doku.php?id=misc:r_accuracy:decimal_numbers Petr Savicky.