[R] help for the first poster- a simple question

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Mar 3 18:57:54 CET 2008


On Mon, 2008-03-03 at 12:52 -0500, Xuejun Qin wrote:
> Hi, there, 
> I cannot  get accurate value  for calculation.
> for example:
> ld<-sqrt(1*0.05*0.95*0.05*0.95)
> 0.05*0.95-ld=-6.938894e-18
> 0.05*0.95-ld==0 is False.
> 
> I met this problem in my program, how can I handle it. Thanks.

Answer 1:

Read FAQ 7.31 -

http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f

Answer 2:

Use all.equal()

> ld<-sqrt(1*0.05*0.95*0.05*0.95)
> 0.05*0.95-ld
[1] -6.938894e-18
> 0.05*0.95-ld==0
[1] FALSE
> all.equal(0.05*0.95-ld, 0)
[1] TRUE

> 
> 
> xj.

HTH

G
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list