0

I found a interesting bug in R and replicated it on different machines in different versions of R.

If I take the 2nd modulo of 14 (14 %% 2), I expect 0 as result. This works fine:

> 14 %% 2
[0] 0

If I take the 2nd modulo of (100 * 0.14), which is 14 obviously, I get 1.776357e-15 as a result.

> (100 * 0.14) %% 2
[0] 1.776357e-15

I investigated a bit and found, that this only happens for (14 * n²):

> (100 * 0.14) %% 2
[0] 1.776357e-15
> (100 * 0.28) %% 2
[0] 3.552714e-15
> (100 * 0.54) %% 2
[0] 7.105427e-15

Does someone has an explanation for this behaviour?

HFPSY
  • 31
  • 6
  • 2
    Not a bug, rather a feature of computers. Caused by floating point imprecision and applies to many things as exemplified in the link above. See also this: https://stackoverflow.com/questions/71678599/why-does-not-floor-always-return-the-outcome-of-a-multiplication Weird, eh? – Mikko May 05 '22 at 11:47
  • 1
    As an addition, you state "(100 * 0.14), which is 14 obviously", well not that obvious ;) Try `14 == 100*0.14 # [1] FALSE` so then we learn that it has nothing to do with the modulo function but the fact that those numbers are not the same. Why? See the already given links. – Merijn van Tilborg May 05 '22 at 11:53

0 Answers0