[R] problem with comparisons for vectors

Joshua Wiley jwiley.psych at gmail.com
Mon Jul 12 08:08:44 CEST 2010


This is also mentioned in FAQ 7.31

http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f

Also if you search the R-help archives for 'precision' you can find a
lot of threads discussing the issue in further depth.

On Sun, Jul 11, 2010 at 9:02 PM, Wu Gong <wg2f at mtmail.mtsu.edu> wrote:
>
> I don't know the real reason, but help("==") gives some clues.
>
> For numerical and complex values, remember == and != do not allow for the
> finite representation of fractions, nor for rounding error. Using all.equal
> with identical is almost always preferable. See the examples.
>
> x1 <- 0.5 - 0.3
> x2 <- 0.3 - 0.1
> x1 == x2                           # FALSE on most machines
> identical(all.equal(x1, x2), TRUE) # TRUE everywhere
>
>
> -----
> A R learner.
> --
> View this message in context: http://r.789695.n4.nabble.com/problem-with-comparisons-for-vectors-tp2285557p2285685.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list