0

I can't think of what I am doing wrong here. If I try:

seq(from=0.55, to=0.55, by=0.05)
[1] 0.55

But then if I try to use variables it gives me an error. Not sure what is different here but have a hunch it is some subtlety of R that I missed. Here is the same code using variables:

corr.from <- 0.55
corr.to <- 0.6
corr.increment <- 0.05
seq(from=corr.from, to=corr.to - corr.increment, by=corr.increment)
Error during wrapup: wrong sign in 'by' argument

What am I doing wrong?

Denis
  • 11,796
  • 16
  • 88
  • 150
  • 1
    The problem is that `corr.to - corr.increment` is not the same as `0.55`. See `corr.to - corr.increment == 0.55` returns `FALSE`. It's actually less than 0.55 so you can't have a positive `by=` value. `corr.to - corr.increment < 0.55 == TRUE` – MrFlick Aug 22 '19 at 19:53
  • Why is `corr.to - corr.increment == 0.55` FALSE? Is there some floating point arithmetic happening here? – Denis Aug 22 '19 at 19:54
  • 1
    That's explained in the duplicate. Computers aren't very good at adding decimal numbers. If you need numbers to match exactly, you need to be extra careful. – MrFlick Aug 22 '19 at 19:55
  • Isn't there a decimal type? – Denis Aug 22 '19 at 19:57
  • I read the duplicate. This is the floating point arithmetic that is present in every language. This is what happens when you use double/float in most .NET languages. But in .NET languages they also have a `decimal` type that you can use to avoid this mess. It is used when you are doing precise calculation like money, for example. Wondering if R has a similar type... – Denis Aug 22 '19 at 20:00
  • Then I supposed this duplicate is more appropriate: [Is there a datatype “Decimal” in R?](https://stackoverflow.com/questions/16960167/is-there-a-datatype-decimal-in-r) – MrFlick Aug 22 '19 at 20:02
  • @MrFlick - awesome! Yes :-). Is there anything not asked on SO yet? – Denis Aug 22 '19 at 20:03

0 Answers0