I have created a class Matrix to do mathematical operation on floating point matrices (basic operations, inverse, …).
Floating point precision make it difficult to test them.
Typically, if test the inverse operation, the error can be huge if the determinant of the matrix is close to zero.
How can I do unit testing on these operations?
There are a number of options which may be suitable depending on your goals.
For each individual output element, you can set the error tolerated in that element based on the input values that contribute to it. (In the case of a compound operation, such as multiplying by a matrix and then multiplying by its inverse, you would still set the tolerance based on all values that contribute to the final value.)
You can select input matrices for testing that do not have determinants near zero.
Instead of measuring the errors in individual elements, you can compute the distance the result matrix is from the ideal result and determine whether that distance exceeds your error tolerance. (E.g., subtract the two matrices, sum the squares of the elements, and take the square root. For other notions of “distance” and errors in matrix operations, consider this page.)
Keep in mind that the purpose of testing is to find errors. This suggests that it is okay to use a very large error tolerance provided the threshold is less than the magnitude of any differences that bugs are likely to cause. That is, consider the types of bugs that may occur in your code: References to the wrong element, references outside of the array, omissions of operations, et cetera. Generally, these errors will cause very large errors in the results. As long as your tests catch these errors, they are accomplishing their purposes.
Ideally, you would compare the computed result of an operation to the ideal result, rather than inverting the operation and comparing to the original input. If the ideal result is not available, you might compare to a more precisely computed result by using extended precision. If this is not generally available, then you might compare by using sets of prepared well-behaved matrices, such as matrices that have only a few simple elements (e.g, small integers) in assorted places (and zeroes elsewhere). Each test would be repeated using different input matrices, so that combined set of test covers the necessary cases. But each test would have simple outputs with little or no rounding error expected.