Are you rewriting == and! = IsTRUE (all.equal ())?

A previous post made me post this question. It would seem best to reassign == to isTRUE(all.equal()) (and != To !isTRUE(all.equal()) . I wonder if others do this in practice? I just realized that I use == and != to make numerical equality in all my code base. My first reaction was that I need to make a full scrub and convert to all.equal . But in fact, every time I use == and != , I I want to check equality (regardless of the data type). Actually, I’m not sure that these operations will check except equality. I’m sure that there is no concept here. Maybe someone Enlighten me? The only argument I see against this approach is that in some cases two non-identical numbers seem to be the same due to the admission of all.equal , but we are told that two numbers that are actually identical can not pass identical() because of how they are stored in memory. Actually, what point is not for default of all.equal ?

+7
source share
1 answer

As @joran mentioned, you will encounter floating point issues with == and != In any other language. One of the important aspects of them in R is part of the vectorization.

It would be much better to define a new almostEqual , fuzzyEqual or similar function. Unfortunately, there is no such basic function. all.equal not very efficient, as it processes all kinds of objects and returns a string describing the difference when you just want TRUE or FALSE .

Here is an example of such a function. It is vectorized as == .

 almostEqual <- function(x, y, tolerance=1e-8) { diff <- abs(x - y) mag <- pmax( abs(x), abs(y) ) ifelse( mag > tolerance, diff/mag <= tolerance, diff <= tolerance) } almostEqual(1, c(1+1e-8, 1+2e-8)) # [1] TRUE FALSE 

... it is about 2 times faster than all.equal for scalar values ​​and much faster with vectors.

 x <- 1 y <- 1+1e-8 system.time(for(i in 1:1e4) almostEqual(x, y)) # 0.44 seconds system.time(for(i in 1:1e4) all.equal(x, y)) # 0.93 seconds 
+7
source

All Articles