[R] Logical inconsistency

Bernardo Rangel Tura tura at centroin.com.br
Sat Dec 6 11:00:48 CET 2008


On Fri, 2008-12-05 at 14:18 +0100, Wacek Kusnierczyk wrote:
> Berwin A Turlach wrote:
> > Dear Emma,
> >
> > On Fri, 5 Dec 2008 04:23:53 -0800 (PST)
> > emma jane <emma_me_jane at yahoo.com> wrote:
> >  
> >   
> >> Please could someone kindly explain the following inconsistencies
> >> I've discovered__when performing logical calculations in R:
> >>
> >> 8.8 - 7.8 > 1
> >>     
> >>> TRUE
> >>>       
> >> 8.3 - 7.3 > 1
> >>     
> >>> TRUE
> >>>       
> >
> > Gladly:  FAQ 7.31
> > http://cran.at.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f
> >
> >   
> 
> well, this answer the question only partially.  this explains why a
> system with finite precision arithmetic, such as r, will fail to be
> logically correct in certain cases.  it does not explain why r, a
> language said to isolate a user from the underlying implementational
> choices, would have to fail this way. 
> 
> there is, in principle, no problem in having a high-level language
> perform the computation in a logically consistent way.  for example, bc
> is an "arbitrary precision calculator language", and has no problem with
> examples as the above:
> 
> bc <<< "8.8 - 7.8 > 1"
> # 0, meaning 'no'
> 
> bc <<< "8.3 - 7.3 > 1"
> # 0, meaning 'no'
> 
> bc <<< "8.8 - 7.8 == 1"
> # 1, meaning 'yes'
> 
> 
> the fact that r (and many others, including matlab and sage, perhaps not
> mathematica) does not perform logically here is a consequence of its
> implementation of floating point arithmetic. 
> 
> the faq you were pointed to, and its referring to the goldberg's
> article, show that r does not successfully isolate a user from details
> of the lower-level implementation.
> 
> vQ

Well, first of all for 8.-7.3 is not equal to 1 [for computers]

> 8.3-7.3-1
[1] 8.881784e-16

But if you use only one digit precision 

> round(8.3-7.3,1)-1
[1] 0
> round(8.3-7.3,1)-1>0
[1] FALSE
> round(8.3-7.3,1)==1
[1] TRUE


So the problem is the code write and no the software

-- 
Bernardo Rangel Tura, M.D,MPH,Ph.D
National Institute of Cardiology
Brazil



More information about the R-help mailing list