Hi, I recently spent quite a bit of time trouble shooting a function that I had written only to discover that the problem I was having was with the comparison operator. I assumed that the following would return TRUE:> testMean <- 82.8 + 0.1 > testMean[1] 82.9> testMean == 82.9[1] FALSE Apparently this has to do with deciml places. Look:> newTest <- 82.0 > newTest[1] 82> newTest == 82[1] TRUE> newTest == 82.0[1] TRUE>What does signif() do to my object called "testMean" so that the comparison now evaluates to TRUE?> signif(testMean, 3) == 82.9[1] TRUE Version info:> R.Version()$platform [1] "i386-pc-mingw32" $arch [1] "i386" $os [1] "mingw32" $system [1] "i386, mingw32" $status [1] "" $major [1] "2" $minor [1] "1.0" $year [1] "2005" $month [1] "04" $day [1] "18" $language [1] "R">
Nick Drew wrote:> Hi, I recently spent quite a bit of time trouble > shooting a function that I had written only to > discover that the problem I was having was with the > comparison operator. I assumed that the following > would return TRUE: >This is a very common error. In R 2.1.0, it's FAQ 7.31 Why doesn't R think these numbers are equal? Duncan Murdoch> >>testMean <- 82.8 + 0.1 >>testMean > > [1] 82.9 > >>testMean == 82.9 > > [1] FALSE > > > Apparently this has to do with deciml places. Look: > > >>newTest <- 82.0 >>newTest > > [1] 82 > >>newTest == 82 > > [1] TRUE > >>newTest == 82.0 > > [1] TRUE > > > What does signif() do to my object called "testMean" > so that the comparison now evaluates to TRUE? > > >>signif(testMean, 3) == 82.9 > > [1] TRUE > > > Version info: > > >>R.Version() > > $platform > [1] "i386-pc-mingw32" > > $arch > [1] "i386" > > $os > [1] "mingw32" > > $system > [1] "i386, mingw32" > > $status > [1] "" > > $major > [1] "2" > > $minor > [1] "1.0" > > $year > [1] "2005" > > $month > [1] "04" > > $day > [1] "18" > > $language > [1] "R" > > > > ______________________________________________ > 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 22-May-05 Nick Drew wrote:> Hi, I recently spent quite a bit of time trouble > shooting a function that I had written only to > discover that the problem I was having was with the > comparison operator. I assumed that the following > would return TRUE: > >> testMean <- 82.8 + 0.1 >> testMean > [1] 82.9 >> testMean == 82.9 > [1] FALSE > > > Apparently this has to do with deciml places. Look: > >> newTest <- 82.0 >> newTest > [1] 82 >> newTest == 82 > [1] TRUE >> newTest == 82.0 > [1] TRUE >> > > What does signif() do to my object called "testMean" > so that the comparison now evaluates to TRUE? > >> signif(testMean, 3) == 82.9 > [1] TRUEWhile not an exact explanation, the following strongly hints at the underlying answer: > print(82.9,digits=20) [1] 82.9 > print(82.8,digits=20) [1] 82.8 > print(0.1,digits=20) [1] 0.1 > print(82.8+0.1,digits=20) [1] 82.89999999999999 > (82.8+0.1)-82.9 [1] -1.421085e-14 So decimal arithmetic in a binary computer is not as exact as decimal arithmetic done with pencil and paper. The underlying reason (in part) for the above apparent anomalies is that when 82.8 is added to 0.1, the smaller of these numbers (in its binary "floating point" representation) is shifted along until corresponding digits line up (the shift being accounted for in the "exponent"). In performing the shift, binary digits are dropped from the bottom end. Since "0.1" in binary in fact has an infinite number of significant decimal places, inaccuracy inevitably creeps in! A close neighbour of the above calculations, such that there are no discrepancies in the binary representations, illustrates the above explanation: > print((82.0 + 7/8),digits=20) [1] 82.875 > print((82.0 + 7/8)+1/16,digits=20) [1] 82.9375 > print(82.9375,digits=20) [1] 82.9375 > ((82.0 + 7/8)+1/16)-82.9375 [1] 0 No discrepancies here! The reason is that the fractional parts of these numbers all have exact finite (and short) binary representations. Compare: > ((82.0 + 7/8)+1/16)==82.9375 [1] TRUE > ((82.0 + 8/10)+1/10)==82.9 [1] FALSE Hoping this helps, Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 22-May-05 Time: 09:58:00 ------------------------------ XFMail ------------------------------
On Sat, 21 May 2005, Nick Drew wrote:> > What does signif() do to my object called "testMean" > so that the comparison now evaluates to TRUE? > >> signif(testMean, 3) == 82.9 > [1] TRUE >It rounds to 3 significant digits. An even more reliable approach is round(testMean*10) == 829 since 829 is an integer and exactly representable. -thomas
Maybe Matching Threads
- coercing a list to a data frame, lists in foreloops
- [Fwd: Re: How to apply functions over rows of multiple matrices]
- $new cannot be accessed when running from Rscript and methods package is not loaded
- How to apply functions over rows of multiple matrices
- MP3 encoding of Monitor files