0

I have the following code:

     seq(0.2, 0.4, by = 0.1) == c(0.2, 0.3, 0.4)

which yields:

     [1]  TRUE FALSE  TRUE

Could somebody please explain this result?

costebk08
  • 1,299
  • 4
  • 17
  • 42

1 Answers1

1

This is numerical error:

> seq(0.2, 0.4, by = 0.1)-c(0.2, 0.3, 0.4)
[1] 0.000000e+00 5.551115e-17 0.000000e+00

See ?identical or ?all.equal and compare:

> identical(seq(0.2, 0.4, by = 0.1),c(0.2, 0.3, 0.4))
[1] FALSE
> all.equal(seq(0.2, 0.4, by = 0.1),c(0.2, 0.3, 0.4))
[1] TRUE

Also compare:

> seq(2L,4L,by=1L)/10==c(.2,.3,.4)
[1] TRUE TRUE TRUE
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198