0

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

Community
  • 1
  • 1
J.M.
  • 257
  • 1
  • 2
  • 13
  • Okay, so this was marked as duplicate but the linked example does not: (1) explain why this occurs _only_ for `0.10` and _not_ for any other values, and (2) offer approaches to remedy this in the function. Furthermore, implementing the following code evaluates to `TRUE`, not `FALSE` as the linked thread suggests: `isTRUE(all.equal(0.10, val.range[10]))`. If I'm missing something obvious, please let me know. Otherwise, I request removing the mark of duplicate on the question. Thanks, Michael – J.M. Oct 28 '16 at 18:09
  • I don't think you took the time to read the answer and click the other links because one answer that I found in 3 seconds specifically addresses 0.1 and answers _why_ this happens for some decimals and not others – rawr Oct 28 '16 at 18:14
  • @rawr Would you mind linking? I read the answers, the suggested links as I was posing this question, and some of the other links to the other link. Simply saying it took you 3 seconds to find an answer doesn't entail that's true for me, and in this case it certainly isn't. I did apply one of the answers in the linked thread, and got `TRUE`, so still stuck. I don't mind fishing, but teaching me to fish is a lot more helpful than saying "you're doing it wrong" from the armchair. Thanks, Michael – J.M. Oct 28 '16 at 18:20
  • you expect it to be true, why are you arguing against yourself? – rawr Oct 28 '16 at 18:22
  • @rawr I'll update the OP – J.M. Oct 28 '16 at 18:23
  • http://stackoverflow.com/a/1089026/2994949 – rawr Oct 28 '16 at 18:27
  • 1
    `format(c(.1, val.range[10]), digits = 22)` – rawr Oct 28 '16 at 18:28
  • @rawr Thanks, that makes it clear. Just got thrown for a loop since using the suggested solution of `isTRUE(all.equal(...))` returned `TRUE`. – J.M. Oct 28 '16 at 18:32

0 Answers0