Skip to content

problem with comparisons for vectors

2 messages · Wu Gong, Joshua Wiley

#
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.
#
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: