giraffehere - 2 years ago 98
R Question

# Odd Behavior With Sequences

I'm experiencing some quite unusual results off a simple condition and really need someone else to run this on their machine for a sanity check. Here's the code:

``````mySEQ <- seq(0.1, 1.0, by = 0.1)
for (s in c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)) { print((s + .1) %in% mySEQ) }
mySEQ[3] == 0.3
``````

Which results in:

``````> mySEQ <- seq(0.1, 1.0, by = 0.1)
> for (s in c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)) { print(s %in% mySEQ) }
[1] TRUE
[1] TRUE
[1] FALSE
[1] TRUE
[1] TRUE
[1] TRUE
[1] FALSE
[1] TRUE
[1] TRUE
[1] TRUE
> mySEQ[3] == 0.3
[1] FALSE
> mySEQ[3]
[1] 0.3
``````

I am absolutely mystified as to why they don't all print out TRUE. It's as if there's something wrong with 0.3 and 0.7. I updated R and all my packages, but just need someone else to run this to check if it's just me. It appears to be something up with the
`seq`
function. Thank you.

To answer the question in your example, use `all.equal` to check the equality of floating point numbers rather than `==`.