[R] Odp: precision issue?

Petr PIKAL petr.pikal at precheza.cz
Thu Mar 4 11:50:56 CET 2010


Hi

r-help-bounces at r-project.org napsal dne 04.03.2010 10:36:43:

> 
> Hi R Gurus,
> 
> I am trying to figure out what is going on here.
> 
> > a <- 68.08
> > b <- a-1.55
> > a-b
> [1] 1.55
> > a-b == 1.55
> [1] FALSE
> > round(a-b,2) == 1.55
> [1] TRUE
> > round(a-b,15) == 1.55
> [1] FALSE
> 
> Why should (a - b) == 1.55 fail when in fact b has been defined to be a 
- 1.
> 55?  Is this a precision issue? How do i correct this?

In real world those definitions of b are the same but not in computer 
world. See FAQ 7.31

Use either rounding or all.equal.

> all.equal(a-b, 1.55)
[1] TRUE

To all, this is quite common question and it is documented in FAQs. 
However there is no such issue in Excel or some other spreadsheet 
programs, therefore maybe a confusion from novices. 

I wonder if there could be some type of global option which will get rid 
of these users mistakes or misunderstandings by setting some threshold 
option for equality testing by use "==".

Regards
Petr




> 
> Alex
> 
> _________________________________________________________________
> Hotmail: Free, trusted and rich email service.
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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.



More information about the R-help mailing list