0

I realize I'm posting this on April Fool's Day and it's a bit absurd, but this isn't a joke. I've tested this on Microsoft R Open 3.3.2 for Mac, 3.3.1 for Windows, and vanilla R 3.3.2 on CentOS 6 all with the same results.

Back story: while debugging today, I ran into a situation where a logical equals test failed when I didn't think it should have. The simplest version of the test is below, where 3.8 + 0.6 doesn't seem to equal 4.4, but other combinations do.

I ran these commands in a clean environment:

> (3.8 + 0.6) 
[1] 4.4
> (3.8 + 0.6) == 4.4
[1] FALSE
> (3.6 + 0.8) == 4.4
[1] TRUE
> (3.6 + 0.6 + 0.2) == 4.4
[1] TRUE
> (3.7 + 0.6) == 4.3
[1] TRUE
> (3.9 + 0.6) == 4.5
[1] TRUE
> (3.8 + 0.6) == 4.4
[1] FALSE
> (2.2 + 2.2) == 4.4
[1] TRUE
> (2.3 + 2.1) == 4.4
[1] TRUE

What in the world is going on with the FALSE results?

Joe C.
  • 1
  • 1
  • Base 10 != base 2. The results can be counter-intuitive at first. This is a heavily-duplicated question, popping up in various forms maybe several times a week. – John Coleman Apr 01 '17 at 22:15
  • It's a machine precision issue, though I'm not sure why it manifests itself in that particular way. But run `options(digits=22)`, then in the console type `4.4` then `3.8` then `0.6`. Then try `4.4 - (3.8 + 0.6)` then `4.4 - 3.8 - 0.6` then `4.4 - (3.2 + 1.2)`, etc. – eipi10 Apr 01 '17 at 22:24

0 Answers0