On 29/10/2021 4:34 a.m., PIKAL Petr wrote:
Hi

One has to be careful when using fractions in seq step.

Although it works for 0.5
(seq(0,10, .5) - round(seq(0,10,.5),2))==0
  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
TRUE
[16] TRUE TRUE TRUE TRUE TRUE TRUE

in case of 0.3 (or others) it does not always result in expected values (see
FAQ 7.31 for explanation)

(seq(0,10, .3) - round(seq(0,10,.3),2))==0
  [1]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE
[13] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE
[25] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE


Petr is right, it's unsafe to use fractional values for the step. 0.5 works because it has a power of 2 in the denominator and so does the start value, but it's easy to make mistakes when you rely on that (e.g. changing the step size from 0.5 to 0.3 would break things).

A better idea is to modify a sequence of integers. For example, to get 1.5 to 3.5 by 0.5, you can do (3:7)*0.5, and for 0 to 3 by 0.3, use (0:10)*0.3.

Duncan Murdoch

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to