Can someone please explain these results to me? I assume it has something to do with how the value of -1 is stored in the vector "dev"...
As mentioned in the comment below:
Can someone please explain these results to me? I assume it has something to do with how the value of -1 is stored in the vector "dev"...
As mentioned in the comment below:
(edited to add Best Answer)
As pointed out by Ben Bolker in the comments, there is a classic answer already, which covers my points and more besides:
Why are these numbers not equal?
all.equal is your friend. When comparing floating point numbers, use it instead of ==. e.g. all.equal(dev[j], -1)
This is floating-point arithmetic; floating-point numbers are rarely exactly the same, due to rounding errors. This isn't an R thing; it's a computer thing.
In this case, the modulo operator is doing exactly what the documentation says: it's returning the remainder after division. In this case, the remainder is noise, which is expected.
You can compare with
all.equal(dev[j] %% 1, 0)
Floating-point traps are covered quite nicely in chapter 1 of the R Inferno: https://www.burns-stat.com/pages/Tutor/R_inferno.pdf
Whenever floating point operations are done, even simple ones, you should assume that there will be numerical error. If by chance there is no error, regard that as a happy accident, not your due. You can use the
all.equalfunction instead of==to test equality of floating point numbers.