Look at this simple R code:
seq(0.3, 2, 0.01)[56]
## [1] 0.85
seq(0.3, 2, 0.01)[56] == 0.85
## [1] FALSE
Why do I get this and what's the best way to do such equality test?
Look at this simple R code:
seq(0.3, 2, 0.01)[56]
## [1] 0.85
seq(0.3, 2, 0.01)[56] == 0.85
## [1] FALSE
Why do I get this and what's the best way to do such equality test?
You need to use all.equal
as in
all.equal(seq(0.3, 2, 0.01)[56],0.85)
This occurs because of issues in representing numbers less that 1 in binary. In general, 0.85 cannot be written exactly in binary, and the approximation used by simply typing 0.85 is not identical to the approximation used by adding an approximation of 0.01 to an approximation of 0.3 55 times.
For more details see here: Why can't decimal numbers be represented exactly in binary?