Skip to content

A strange arithmetic error in R (maybe a bug)

3 messages · T. Zhang, Adrian Dusa, Berend Hasselman

#
Hello,

Today I happened to notice a strange error in R. If you type (2.01-0.06)==1.95, output from R is ?FALSE?, which is wrong. But if you type (1.01-0.06)==0.95, output is ?TRUE?, which is correct. I tested this in two systems: R 3.3.2 in my iMac and R 3.2.0 on my school?s Linux server. Both gave same outputs. As shown in the following:
[1] 1.95
[1] FALSE
[1] 0.95
[1] TRUE
[1] FALSE
[1] TRUE

Similar errors could be found with simple alterations of the above inputs, such as:
[1] 0.121
[1] FALSE
[1] 0.121
[1] TRUE

Could any of you test whether you have same outputs as mine? And does anyone know what is wrong with these? My guess is that R has a bug in processing double numbers. Thanks!

TZ
#
There is no bug, of course, this is a common floating point arithmetic
misunderstanding.
[1] 2.0099999999999997868

Please search for "What every scientist should know about floating-point
arithmetic" and you'll hopefully understand what the "bug" is.

Hth,
Adrian
On Sat, Feb 18, 2017 at 6:18 AM, T. Zhang <tz05 at me.com> wrote:

            

  
    
#
Not a bug. See the R FAQ section 7.31.

Berend Hasselman