[R] Surprising behavior using seq()

Duncan Murdoch murdoch.duncan at gmail.com
Mon Nov 15 15:46:28 CET 2010


On 15/11/2010 9:24 AM, Vadim Patsalo wrote:
> Patrick and Bert,
>
> Thank you both for you replies to my question. I see how my naïve expectations fail to floating point arithmetic. However, I still believe there is an underlying problem.
>
> It seems to me that when asked,
>
> >  c(7.7, 7.8, 7.9) %in% seq(4, 8, by=0.1)
> >  [1]  TRUE FALSE  TRUE
>
> R should return TRUE in all instances. %in% is testing set membership... in that way, shouldn't it be using all.equal() (instead of the implicit '=='), as Patrick suggests the R inferno?

No, because 7.8 is not in the set.  Some number quite close to it is 
there, but no 7.8.  This is true for both meanings of "7.8":

   - the number 78/10
   - R's representation of that number

Neither one is in the set.  What you have there is R's representation of 
4 plus 38 times R's representation of 0.1.  (R can represent 4 and 38 
exactly, but not 0.1, 7.8, pi, or most other numbers.)

Duncan Murdoch

> Is there a convenient way to test set membership using all.equal()? In particular, can you do it (conveniently) when the lengths of the numeric lists are different?
>
> Thanks again for your reply!
> Vadim
>
> On Nov 13, 2010, at 5:46 AM, Patrick Burns wrote:
>
> >  See Circle 1 of 'The R Inferno'.
>
> ______________________________________________
> 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.



More information about the R-help mailing list