Dear R users, I ran into the strange situation where a number does not seem to equal its value. Try this: a <- seq(0.05,0.7,0.05)[3] b <- 0.15 a == b Should this not be TRUE? a-b yields a very small number (and not 0) so this most probably is a numerical error, but why does seq create such numbers? Thanks a lot Jannis
Hi. Check R FAQ, section 7.31 Why doesn't R think these numbers are equal? http://cran.r-project.org/doc/FAQ/R-FAQ.html all.equal(a,b) Andrija On Mon, May 20, 2013 at 2:36 PM, Jannis <bt_jannis@yahoo.de> wrote:> Dear R users, > > > > I ran into the strange situation where a number does not seem to equal its > value. Try this: > > > > a <- seq(0.05,0.7,0.05)[3] > > b <- 0.15 > > a == b > > > Should this not be TRUE? a-b yields a very small number (and not 0) so > this most probably is a numerical error, but why does seq create such > numbers? > > > Thanks a lot > Jannis > > ______________________________**________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide http://www.R-project.org/** > posting-guide.html <http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Hello, That's FAQ 7.31. Note that the increment, 0.05, is not a power of 2 so the third value of the sequence is not exactly equal to 0.15. Hope this helps, Rui Barradas Em 20-05-2013 13:36, Jannis escreveu:> Dear R users, > > > > I ran into the strange situation where a number does not seem to equal > its value. Try this: > > > > a <- seq(0.05,0.7,0.05)[3] > > b <- 0.15 > > a == b > > > Should this not be TRUE? a-b yields a very small number (and not 0) so > this most probably is a numerical error, but why does seq create such > numbers? > > > Thanks a lot > Jannis > > ______________________________________________ > 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.
Jannis, Not strange. Try with numbers that can be exactly represented in binary (that's what computers use), e.g., a<-seq(5,70,by=5) b<-15 a==b or fractions, a<-seq(1/32,14/32,by=1/32) b<-3/32 a==b Most decimal fractions cannot be represented exactly although a bit of magic will display them as if they are. A little digging into the r-help archives will find further explanation. Clint Clint Bowman INTERNET: clint at ecy.wa.gov Air Quality Modeler INTERNET: clint at math.utah.edu Department of Ecology VOICE: (360) 407-6815 PO Box 47600 FAX: (360) 407-7534 Olympia, WA 98504-7600 USPS: PO Box 47600, Olympia, WA 98504-7600 Parcels: 300 Desmond Drive, Lacey, WA 98503-1274 On Mon, 20 May 2013, Jannis wrote:> Dear R users, > > > > I ran into the strange situation where a number does not seem to equal its > value. Try this: > > > > a <- seq(0.05,0.7,0.05)[3] > > b <- 0.15 > > a == b > > > Should this not be TRUE? a-b yields a very small number (and not 0) so this > most probably is a numerical error, but why does seq create such numbers? > > > Thanks a lot > Jannis > > ______________________________________________ > 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. >