[Rd] Inaccuracy in seq() (PR#9189)

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Aug 30 13:52:12 CEST 2006


See the FAQ Q7.31: 0.07 is not exactly representable on your computer.

0.060 + 2*0.005 != 0.070 in computer arithmetic:

> options(digits=18)
> 0.060
[1] 0.06
> 0.070
[1] 0.070000000000000007
> 0.060+2*0.005
[1] 0.06999999999999999
> 0.06+2*0.005 -0.07
[1] -1.3877787807814457e-17
> .Machine$double.eps * 0.07
[1] 1.5543122344752193e-17

On Wed, 30 Aug 2006, kellymj1 at cf.ac.uk wrote:

> Full_Name: Mark James Kelly
> Version: 2.3.1
> OS: Windows
> Submission from: (NULL) (193.63.127.224)
> 
> 
> Sequence produces slightly inaccurate results. 

sequence() is a different function in R.

> #This (supposedely) creates a sequence 0.060,0.065,0.070.....0.30
> thingnor=(seq(0.06,0.30,by=0.005))
> 
> #This does the same but rounds to three decimals
> thingrou=round(seq(0.06,0.30,by=0.005),3)
> 
> #This is another vector, the same length as the two above, with 24 zeroes, then
> 0.070, then 24 zeroes
> otherthing=c(rep(0,24),0.070,rep(0,24))
> 
> 
> #This fails to select the entries equal to 0.070 
> otherthing[otherthing==thingnor[3]]
> 
> #This gives the correct answer
> otherthing[otherthing==thingrou[3]]
> 
> 
> #This compares the two sequences (which should be identical)
> (thingnor-thingrou)

Not according to FAQ Q7.31 and help("==").

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list