0

Suppose you declared following function:

compound <- function(x,i,t) {
  x*(1+i)^t
}

What are the fundamentals of following results:

compare(compound(100, 0.1, 2),121) => 'Equal'

and

identical(compound(100, 0.1, 2),121) => FALSE

In the package testthat expect_identical() checks the second condition and returns a failure in that case, although the value is 121. What is a better alternative to verify the above function compound()?

camille
  • 16,432
  • 18
  • 38
  • 60
taddoo
  • 1
  • 3
    Try `compound(100, 0.1, 2) - 121`. This is why `identical` retuns `FALSE`. `identical` also wants *everything* (say, the `names` attribute) else to be identical. The best alternative is `isTRUE(all.equal(compound(100, 0.1, 2), 121))`. – Rui Barradas Jul 03 '19 at 18:59
  • Thank you! I know how to proceed, but could you explain your *everything* further? – taddoo Jul 03 '19 at 19:20
  • `identical` needs exact matches to return `TRUE`.I have given an example, a frequent `attributes`. But there are others. Try `identical(as.numeric(1:10), 1:10)`. The former is of class `"numeric"`, the latter of class `"integer"` and this is enough for `identical` to return `FALSE`. – Rui Barradas Jul 03 '19 at 19:37
  • Thanks a lot @Rui Barradas. I have found another explaination which I guess could also be relevant for my topic. See: https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal – taddoo Jul 03 '19 at 20:50
  • Yes, I was about to close the question as a duplicate of that one. The error is so frequent it's a R FAQ many R users know by heart, FAQ 7.31. – Rui Barradas Jul 03 '19 at 20:52

0 Answers0