[R] seq() in 0.62.4 and 0.63

Prof Brian D Ripley ripley at stats.ox.ac.uk
Sat Nov 14 09:04:34 CET 1998

```Solaris 2.6, R Version 0.63.0  (November 14, 1998)
Version 0.62.4  (October 24, 1998)

> seq(0.15, 0.70, 0.05)
[1] 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65

The much reviled (on r-help) 0.62.3 got such simple cases right (even if
0.62.1 did not).

Jim Lindsey insisted that such bugs are reported here, so I am.  And yes,
this is a real example and leads to an incorrect analysis of the dataset in
question.

The problem is the lines

if (by > 0)
while (from + n * by > to) n <- n - 1
else while (from + n * by < to) n <- n - 1

One should try to avoid exact comparisons on floating-point quantities.
Given the work above to choose a suitable tolerance, we can attach a
tolerance here, say

if (by > 0)  while (from + n * (1 - eps) * by > to) n <- n - 1
else         while (from + n * (1 - eps) * by < to) n <- n - 1

although I suspect eps is actually rather too small for safety (I would
suggest 10 x machine precision to allow for a number of lfoating point
operations).

However, I think there is a much simpler approach:

n <- as.integer(n + 0.1) # make sure n is not too small
tol <- abs(to - from) * 10 * .Machine\$double.eps
if(by > 0)	while(from + n*by > to + tol) n <- n - 1
else	while(from + n*by < to - tol) n <- n - 1

Brian

--
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```