I have been working on cleaning up my old linear algebra code after getting some actual programming experience (in audio...), and I have the above question about best practices: if a matrix is not invertible, what should the function Mat3 inverse( Mat3 const& a ) do? I know that MOST matrices are invertible, but I don't want things to explode on the off chance things go sideways.
I see five possibilities (I'm using the Adjugate matrix divided by the determinant to form the inverse):
- Rely on the calling context to check if a matrix is invertible. (Yeah right)
- Don't check the determinant and let the floating point exception happen. (Eww)
- Assert that the determinant isn't zero (though I haven't written assertions yet).
- Throw an exception.
- Return some matrix which isn't the inverse, such as the matrix 'a' itself or a null matrix of all zeroes, which can be checked; in the case of returning 'a', you could check it against itself for equality. (Bugs?)
I'm leaning Assertion for development, possibly backed by throwing an exception for 'production' code (who am I kidding, none of this will ever go live :P) so that it is recoverable or at least loggable.