[R] wrong answer for simple expressions

Petr Pikal petr.pikal at precheza.cz
Fri Nov 12 09:50:57 CET 2004


Hi

On 11 Nov 2004 at 12:24, 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.  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?

Floating point arithmetic is imprecise.

Cheers
Petr

> 
> 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

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list