1

Unexpectedly for the command:

seq(-0.30,0.60,0.10)

i receive the answer:

 [1] -3.000000e-01 -2.000000e-01 -1.000000e-01  5.551115e-17  1.000000e-01  2.000000e-01  3.000000e-01  4.000000e-01  5.000000e-01
[10]  6.000000e-01

Note the non zero value in 4th element, whereas for the command:

seq(-0.40,0.60,0.10)

The return result is (as expected):

 [1] -0.4 -0.3 -0.2 -0.1  0.0  0.1  0.2  0.3  0.4  0.5  0.6

I suppose this is a round problem. The issue here is that I am using the seq command for creating a legend and therefore my legend becomes badly formatted. Apart from the obvious replacement of the value or the construction of a c() command, did anyone else get this error? Is this a bug?

I am using Rstudio Version 1.0.136 and R version 3.3.2 (2016-10-31).

Jaap
  • 81,064
  • 34
  • 182
  • 193
Pedro Nogueira
  • 153
  • 1
  • 9

1 Answers1

3

Yeah, that's just precision and floating point numbers...

It's usually easier to do:

seq(-3,6) / 10

which gives:

-0.3 -0.2 -0.1  0.0  0.1  0.2  0.3  0.4  0.5  0.6
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • 3
    Or just `-3:6/10` – d.b Mar 06 '17 at 16:51
  • Instead of "*easier*" I would say "*safer*" as `seq(-3,6)` returns an integer vector. I would also argue it's safer to do `10L` instead of `10` in order to make sure we are not dividing by a float. – David Arenburg Mar 06 '17 at 18:47