I have read Why are these numbers not equal? and it's helpful, but doesn't address the question here. I am not asking why two numbers are different, I'm asking why every number in the range 0.01
to 0.99
is not different, except for 0.10
. The accepted solution in the linked thread (below) still doesn't differentiate between the two values in question (it says they're identical), so even if that was my question, it doesn't explain why the values are actually different, nor does it solve the problem here.
Working on a function which accepts a numeric value as an argument. The valid range for this numeric value is from 0.01
to 0.99
. For some reason, 0.10
is being returned as not in the valid range (FALSE
), even though it is?
val.range = seq(from = 0.01, to = 0.99, by = 0.01)
This works fine, and values in the data.frame
are all TRUE
:
x = data.frame()
for (i in seq(0.01, 0.99, by = 0.01)){
x = rbind(x, i %in% val.range)
}
However, this returns FALSE
- even though 0.10
clearly is in val.range
:
0.10 %in% val.range
This seems to only be an issue with 0.10
- trying other values within the range seem to work fine and return as TRUE
. Any thoughts as to what's going on here?
Edit:
After reading Why are these numbers not equal?, I tried the following accepted solution:
isTRUE(all.equal(0.10, val.range[10]))
Which evaluates to TRUE
. But if it's true that 0.10
is equal to the 0.10
in val.range[10]
, then my results above should both be TRUE
, but they're not.
Solution:
Since the linked thread does not answer the question, @rawr was kind enough to provide code in the comments that peels back the differences:
format(c(0.10, val.range[10]), digits = 22)
The solution I applied rounded both the values in val.range
and the numeric value:
val.range = round(seq(from = 0.01, to = 0.99, by = 0.01), digits = 2)
temp = round(0.10, digits = 2)
temp %in% val.range
Which then evaluates to TRUE
.
Michael