0

I have this code to create a matrix:

TTCm <- seq(from=-4, to=0, by=0.1)
Speedm <- seq(from=15, to=25, by=0.5)
matrix2 <- matrix(nrow = 41, ncol = 21)
dimnames(matrix2) = list(TTCm, Speedm)

However (depending on the sequence) dimnames might give weird values. In the above particular case TTCm -0.1 is -0.0999999999999996. Does anyone know what the problem here is? Thanks

1 Answers1

0

That's because computer uses finite precision arithmetic. it's well known as "floating point problem". so the 40th value of TTcm you can see on R console is a approximate value. you can check it by using dplyr::near() on R console. Try the code below.

TTCm <- seq(from=-4, to=0, by=0.1) TTCM[40] == -0.1 dplyr::near(TTCm[40], -0.1)

if you don't understand completely, Try the code below. Maybe you can totally understand.

sqrt(2) ^ 2 == 2 1/10 * 10 == 1 near(sqrt(2)^2, 2) near(1/10*10, 1)

you need to be careful when handling floating point.

BBang
  • 16
  • 2