I accidentally discovered a weird bug with either the as.integer or the det function in R base. Does anyone know what is going on here and how to prevent it?
I was computing the determinant of the following 3-by-3 matrix:
mat <- matrix(c(15, 6, 116, 10, 13, 16, 14, 23, 56), ncol = 3)
Which looks like this:
[,1] [,2] [,3]
[1,] 15 10 14
[2,] 6 13 23
[3,] 116 16 56
Two things are easy to see: all entries are integers, and each of the six sets of three-entries-no-two-of-which-lie-in-the-same-row-or-column contains at least one even integer. Hence the determinant must be an even integer.
Asking R the actual value of this determinant by typing det(mat) it returns something that looks like an even integer: 8952. But lo and behold: deep down inside R's mind it actually is either a non-integer or an odd integer, since when typing as.integer(det(mat)) we get 8951.
What is going on here? The 8951 is obviously wrong. Also, less obviously, the value 8952 is correct as can be seen with pen and paper.
So my questions are:
what is going on here?
How do I force R to give me the correct integer values when asked to compute the determinant of an integer matrix?
