(I use R 2.9.1 with win XP) If I run this code: seq(-0.1,.9, by = .05)[seq(-0.1,.9, by = .05) <= 0.5] I get this output: [1] -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 Why is 0.50 not in the results ? (It seems that it gives a slightly bigger number then 0.5 but I don't understand why it does that) Where as if I try: seq(-0.1,.9, by = .05)[seq(-0.1,.9, by = .05) <= 0.4] and get: [1] -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 Then 0.40 WILL be in the results. Thanks,Tal -- ---------------------------------------------- My contact information: Tal Galili Phone number: 972-50-3373767 FaceBook: Tal Galili My Blogs: http://www.r-statistics.com/ http://www.talgalili.com http://www.biostatistics.co.il [[alternative HTML version deleted]]
Tal Galili wrote:> (I use R 2.9.1 with win XP) > > If I run this code: > seq(-0.1,.9, by = .05)[seq(-0.1,.9, by = .05) <= 0.5] > I get this output: > [1] -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 > Why is 0.50 not in the results ? > (It seems that it gives a slightly bigger number then 0.5 but I don't > understand why it does that) > > > Where as if I try: > seq(-0.1,.9, by = .05)[seq(-0.1,.9, by = .05) <= 0.4] > and get: > [1] -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 > Then 0.40 WILL be in the results. >Neither a bug nor a feature, just the way things are. None of -0.1, 0.9 or 0.05 are numbers that can be represented exactly in R, so it's not too surprising that when you add a dozen copies of the last one to the first one, you don't get exactly 0.5 (which, being a power of 2, is exactly representable. See FAQ 7.31. Duncan Murdoch
General floating point arithmetic issue here: See FAQ 7.31 http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Tal Galili Sent: Monday, August 10, 2009 4:14 PM To: r-help at r-project.org Subject: [R] Bug in "seq" (or a "feature") ? (I use R 2.9.1 with win XP) If I run this code: seq(-0.1,.9, by = .05)[seq(-0.1,.9, by = .05) <= 0.5] I get this output: [1] -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 Why is 0.50 not in the results ? (It seems that it gives a slightly bigger number then 0.5 but I don't understand why it does that) Where as if I try: seq(-0.1,.9, by = .05)[seq(-0.1,.9, by = .05) <= 0.4] and get: [1] -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 Then 0.40 WILL be in the results. Thanks,Tal -- ---------------------------------------------- My contact information: Tal Galili Phone number: 972-50-3373767 FaceBook: Tal Galili My Blogs: http://www.r-statistics.com/ http://www.talgalili.com http://www.biostatistics.co.il [[alternative HTML version deleted]] ______________________________________________ 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.
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On > Behalf Of Tal Galili > Sent: Monday, August 10, 2009 2:14 PM > To: r-help at r-project.org > Subject: [R] Bug in "seq" (or a "feature") ? > > (I use R 2.9.1 with win XP) > > If I run this code: > seq(-0.1,.9, by = .05)[seq(-0.1,.9, by = .05) <= 0.5] > I get this output: > [1] -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 > Why is 0.50 not in the results ? > (It seems that it gives a slightly bigger number then 0.5 but I don't > understand why it does that) > > > Where as if I try: > seq(-0.1,.9, by = .05)[seq(-0.1,.9, by = .05) <= 0.4] > and get: > [1] -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 > Then 0.40 WILL be in the results. > > Thanks,Tal > >This is another question for which the answer is FAQ 7.31. Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204