Ok, I think det
is the one that's misleading here.
The "true" determinant is zero if the product of the eigenvalues of $X^TX$ is zero, which happens iff one of the individual eigenvalues is zero. Given computer arithmetic, the determinant will be computed as zero if one of the individual computed eigenvalues is exactly zero or if enough of them are very small that the computed product underflows. It takes a lot to underflow double precision, so we're talking really really small. .Machine$double.eps^20
doesn't underflow.
The matrix is truly uninvertible iff one of the individual eigenvalues is zero. Given computer arithmetic, the inverse will be detected as numerically singular if the estimated condition number, the ratio of the largest and smallest eigenvalues, is too large. The default threshold is the reciprocal of the condition number being smaller than machine epsilon, which is only $2^{-52}\approx 2\times 10^{-16}$. So it's a lot easier to get solve
to give up on a matrix that to get det
to underflow to zero.
@John's answer gives a matrix of rank 2 that has a non-zero computed determinant, because the non-zero eigenvalues are big and presumably the zero ones didn't exactly evaluate to zero. Your example isn't like that because it would have full rank at infinite precision, but it's presumably similar. The smallest eigenvalue is not zero, but it's less than machine epsilon times the largest eigenvalue.
As a final note, while solve
and det
just use Lapack, as all sensible people do, functions like lm
and glm
don't -- and they have a much stricter tolerance for singular matrices, because typically a double-precision design matrix that someone hasn't deliberately set up as a numerical analysis exercise is either actually singular or has a reciprocal condition number much larger than machine epsilon. And if it does fall in the gap, the user probably needs to know. The tolerance (in qr(,LAPACK=FALSE)
) is $10^{-7}$. So, the numerical rank as computed by qr
can be zero when solve
still works, and that's deliberate and for good reasons. (I mean, on top of the fact that you're probably using qr
on $X$ rather than $X^TX$)