[R] wrong answer for simple expressions

Thomas Lumley tlumley at u.washington.edu
Thu Nov 11 19:33:07 CET 2004


On Thu, 11 Nov 2004, Drew Hoysak wrote:

> I am experiencing strange (to me) output when trying to do simple
> calculations.  Expressions that should equal zero yield non-zero
> values.

No. There is no reason why these expressions should yield zero values. 
Remember that computers work in base 2, and that 0.1 has an infinitely 
recurring binary expansion in base 2.  You should expect that 0.1 computed 
two different ways should differ in the last few bits. You have managed to 
get zero to 52 bits accuracy, which is not bad when you consider that the 
machine only works to 54 bits.

 	-thomas

> Examples:
>
>> a <- 4.1-3.1
>> b <- 5.1-4.1
>> a-b
> [1] -4.440892e-16
>
>
>> (4.1-3.1)-(5.1-4.1)
> [1] -4.440892e-16
>
>
> When this last expression is expanded, I get the right answer:
>
>> 4.1-3.1-5.1+4.1
> [1] 0
>
>
> I am using the binary packaged version R-2.0.0-0.fdr.1.fc2.i386.rpm for
> Linux Fedora Core 2.  I had the same problem with version 1.9.0-0
>
> Can anyone tell me what is going on?
>
> Thanks,
>
>
> Drew Hoysak
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle




More information about the R-help mailing list