Consider the code: x <- seq(0,1,0.2) y <- seq(0,1,0.01) cbind(match(y,x),y) which, surprisingly, doesn't show a match at 0.6! (It gives correct matches at 0, 0.2, 0.4, 0.8 and 1, though) In addition, x[4]==y[61] yields FALSE. (but x[5]==y[81], the one for 0.8, yields TRUE) Is this a consequence of machine error or something else? Could this be overcome? (It works correctly when integers are used in the sequences as well as in many other circumstances) Thank you, Bernhard
On 4/18/07, Bernhard Klingenberg <Bernhard.Klingenberg at williams.edu> wrote:> Consider the code: > > x <- seq(0,1,0.2) > y <- seq(0,1,0.01) > cbind(match(y,x),y) > > which, surprisingly, doesn't show a match at 0.6! (It gives correct > matches at 0, 0.2, 0.4, 0.8 and 1, though) > > In addition, > > x[4]==y[61] > > yields FALSE. (but x[5]==y[81], the one for 0.8, yields TRUE) > > Is this a consequence of machine error or something else? > Could this be overcome? (It works correctly when integers are used in > the sequences as well as in many other circumstances)See the R FAQ - question 7.31 It's a basic property of floating point arithmetic.
>Is this a consequence of machine error or something else? >Could this be overcome? (It works correctly when integers are used in >the sequences as well as in many other circumstances)The usual solution for testing a==b with floating-point round-off error is abs(a-b)<tol where tol depends on the magnitude of a,b and the likely error. Match is a bit trickier but you could round at some interval representing "close enough" numbers to force almost equal numbers to the same representation, e.g.> X1=seq(0,1,len=11) > X2=seq(0,1,len=101) > match(X1,X2)[1] 1 11 21 NA 41 51 NA 71 81 91 101> match(round(X1,2),round(X2,2))[1] 1 11 21 31 41 51 61 71 81 91 101 In the following case, X2 does not round "exactly" but 7-digit accuracy seems fine.> X2=seq(0,1,len=31) > match(X1,X2)[1] 1 4 7 NA 13 16 NA NA 25 28 31> match(round(X1,7),round(X2,7))[1] 1 4 7 10 13 16 19 22 25 28 31