[R] A strange arithmetic error in R (maybe a bug)

Berend Hasselman
Sat, 18 Feb 2017 20:35:03 -0000

> On 18 Feb 2017, at 05:18, T. Zhang <tz05 at me.com> wrote:
>
> 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:
>
>> 2.01-0.06
> [1] 1.95
>> (2.01-0.06)==1.95  # should be TRUE; output is wrong
> [1] FALSE
>> 1.01-0.06
> [1] 0.95
>> (1.01-0.06)==0.95  # should be TRUE; output is correct
> [1] TRUE
>> (2.01-0.06)>1.95  # should be FALSE; output is correct
> [1] FALSE
>> (2.01-0.06)<1.95  # should be FALSE; output is wrong
> [1] TRUE
>
> Similar errors could be found with simple alterations of the above inputs,
such as:
>> 5.533-5.412
> [1] 0.121
>> (5.533-5.412)==0.121  # should be TRUE; output is wrong
> [1] FALSE
>> 2.533-2.412
> [1] 0.121
>> (2.533-2.412)==0.121  # should be TRUE; output is correct
> [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!
>

Not a bug. See the R FAQ section 7.31.

Berend Hasselman

> TZ
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.