0

I have an XTS filled with decimal values, and I've run into a strange problem.

Steps to recreate

Create the XTS

t <- xts(c(1.9,3.8,5.7,7.6,9.5,11.4,13.3,15.2,17.1,19,21,23), order.by=seq(as.Date("2018-01-01"), as.Date("2018-01-12"), "days"))
colnames(expected) <- "Values"

> t
           Values
2018-01-01    1.9
2018-01-02    3.8
2018-01-03    5.7
2018-01-04    7.6
2018-01-05    9.5
2018-01-06   11.4
2018-01-07   13.3
2018-01-08   15.2
2018-01-09   17.1
2018-01-10   19.0
2018-01-11   21.0
2018-01-12   23.0

Compare it to a value

> (1.9 * 3) == t
           Values
2018-01-01  FALSE
2018-01-02  FALSE
2018-01-03  FALSE
2018-01-04  FALSE
2018-01-05  FALSE
2018-01-06  FALSE
2018-01-07  FALSE
2018-01-08  FALSE
2018-01-09  FALSE
2018-01-10  FALSE
2018-01-11  FALSE
2018-01-12  FALSE

I'd expect t["2018-01-03"] to equal (1.9 * 3) which equals 5.7. But it does not.

There shouldn't be any rounding error here since 1.9 * 3 is exactly 5.7. So I am not sure what is going on here.

Is this a quirk of XTS with decimal values? I can compare integer values just fine using this method. How do I compare these properly?

  • Oh sorry. Just try in your console `1.9*3==5.7` and you will see that is `FALSE` – Onyambu Mar 07 '18 at 00:36
  • @Onyambu, yes. But that doesn't answer my question. –  Mar 07 '18 at 00:40
  • That is commonly referred to as [FAQ 7.31](https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f). – Dirk Eddelbuettel Mar 07 '18 at 00:43
  • @DirkEddelbuettel, I figured thats the case just like every other language. Thanks for the link to the FAQ. So to make sure the XTSes are truly equal I will need to first compare their indexes, and then use `all.equal` with their `coredata`? –  Mar 07 '18 at 00:45
  • Yes, or a similar "epsilon"-based comparison. – Dirk Eddelbuettel Mar 07 '18 at 00:54

0 Answers0