[R] seq(0.05,0.95,by=0.002) and logical error

Uwe Ligges ligges at statistik.uni-dortmund.de
Sun Dec 10 11:10:35 CET 2000



On Sat, 9 Dec 2000, [iso-8859-1] Álvaro A. Novo wrote:

> Regardless of which version -- 1.1.1 or 1.2.0 (2000-11-27) -- with a fresh 
> "directory" (i.e. no .RData), I am getting an extremely weird result. 
> 
> R : Copyright 2000, The R Development Core Team
> Version 1.2.0 Under development (unstable) (2000-11-27)
> 
> > jj _ seq(0.05,0.95,by=0.002)
> > sum(jj==0.75) ## WRONG ANSWER
> [1] 0
> > 0.05 + 350*.002 ## Double check that 0.75 is in jj
> [1] 0.75
> > sum(jj==0.5)
> [1] 1
> > sum(jj==0.562)
> [1] 1
> > sum(jj==0.564) ## 0.564 seems to be the cutoff point
> [1] 0
> 
> Version 1.1.1  (August 15, 2000)
> 
> > jj _ seq(0.05,0.95,by=0.002)
> > sum(jj==0.75)  ## WRONG ANSWER
> [1] 0
> > sum(jj==0.562)
> [1] 1
> > sum(jj==0.564)
> [1] 0
> 
> An additional note, for a different sequence, 0.75 is "recognized"...
> 
> > jj _ seq(0,0.95,by=0.002)
> > sum(jj==0.75) 
> [1] 1
> 
> 
> Thank you for your attention,
> 
> Alvaro Novo
> Linux 2.2.17 - SuSE 6.4


That's a computational problem:
  
  jj <- seq(0.05, 0.95, by=0.002)
  options("digits" = 22)
  jj[351]  # result:  [1] 0.75000000000000011

So sum(jj==0.75) gives the *correct* answer. 

Uwe Ligges 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list