[R] seq

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Fri Sep 3 14:59:59 CEST 2004


On 03-Sep-04 Henric Nilsson wrote:
> Hi everyone,
> 
> I've tried the below on R 1.9.1 and the 2004-08-30 builds of R 1.9.1 
> Patched and R 2.0.0 on Windows 2000, and the results are consistent.
> 
>  > seq(0.5, 0, by = -0.1)
> [1] 0.5 0.4 0.3 0.2 0.1 0.0
> 
>  > seq(0.7, 0, by = -0.1)
> [1]  7.000000e-01  6.000000e-01  5.000000e-01  4.000000e-01 
> 3.000000e-01 
> 2.000000e-01  1.000000e-01 -1.110223e-16
> 
> Is this really the intended behaviour? I ended up using
> 
>  > seq(0.7, 0, length = 8)
> [1] 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.0
> 
> which does what I want.

Hi Henric,
The replies you have already received explain what is going on.
However, what you (and we all) need is a procedure which can
guarantee that what you expect is what you get, For instance,
a test that the 8th element of the "seq" was "==0" would give
the result FALSE.

What I'm in the habit of doing for this sort of thing is on the
lines of

  0.1*seq(5,0, by = -1)
  [1] 0.5 0.4 0.3 0.2 0.1 0.0

or even simply

  0.1*(5:0)
  [1] 0.5 0.4 0.3 0.2 0.1 0.0

The difference is that here the sequence element is working with
integers, which are (provided within bounds) handled exactly in
a digitial computer. Floating point numbers with a fractional
part generally are not, as already explained by others.

Note however the result of

  0.1*(5:0) == 0.3)
  [1] FALSE FALSE FALSE FALSE FALSE FALSE

as opposed to "==0.5", "==0.4", "==0.2", "==0.1" or "==0"!

I hope this helps,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 03-Sep-04                                       Time: 13:59:59
------------------------------ XFMail ------------------------------




More information about the R-help mailing list