[Rd] Proposal: more accurate seq(from, to, length=n)

Suharto Anggono Suharto Anggono @uh@rto_@nggono @ending from y@hoo@com
Fri Sep 7 23:34:42 CEST 2018


In R,
seq(0, 1, 0.1)
gives the same result as
(0:10)*0.1.
It is not the same as
c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1) ,
as 0.1 is not represented exactly. I am fine with it.

In R,
seq(0, 1, length=11)
gives the same result as
seq(0, 1, 0.1).
However, for 
seq(0, 1, length=11),
it is more accurate to return
c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1) .
It can be obtained by
(0:10)/10.

When 'from', 'to', and 'length.out' are specified and length.out > 2, I propose for function 'seq.default' in R to use something like
from + ((0:(length.out - 1))/(length.out - 1)) * (to - from)
instead of something like
from + (0:(length.out - 1)) * ((to - from)/(length.out - 1)) .



More information about the R-devel mailing list