Reputation: 5958
in R, I want to check if a number is a multiple of another. this works well for integers
> 124%%1
[1] 0
> 124%%2
[1] 0
But for some mysterious reason doesn't work well with decimal numbers, any idea why 1.05%%0.05
is not 0?
> 0.05%%0.05
[1] 0
> 1.05%%0.05
[1] 0.05
Upvotes: 2
Views: 94
Reputation: 263352
If you expected a 0 result then perhaps you would be satisfied with the results of multiplying both arguments by 100 (making these integer or at least close to them. )
(100*1.05)%%(100*0.05)
[1] 0
See @akrun's answer for "why".
Upvotes: 2
Reputation: 887193
According to ?"%%"
%% and x %/% y can be used for non-integer y, e.g. 1 %/% 0.2, but the results are subject to representation error and so may be platform-dependent. Because the IEC 60559 representation of 0.2 is a binary fraction slightly larger than 0.2, the answer to 1 %/% 0.2 should be 4 but most platforms give 5.
Upvotes: 6