First, I will preface this question by saying that I know it is a bad idea to test equality of doubles using ==
and that i should instead use isTRUE(all.equal())
, but I've encountered some strange behaviour that is not the result of rounding:
The setup:
> a <- c(0.2, 0.4, 0.6)
> b <- a - 0.3
[1] -0.1 0.1 0.3
> c <- abs(b)
[1] 0.1 0.1 0.3
Now onto the strange behaviour:
which(a == 0.4) # This works fine.
which(b == 0.1) # integer(0) - no good.
which(b == 0.3) # But this works fine?
which(c == 0.3) # Also fine
which(c == 0.1) # integer(0) - no good.
c[1] == c[2] # FALSE
I'm at a loss as to why it works fine in some cases but not others?