Hi all,
i have the following function to solve a linear system of equations for a 3x3 matrix:
Vector2d Matrix3x3::solve2d()
{
Vector2d v;
if (mComponents[1] != 0)
{
v.mY = (mComponents[6] - mComponents[7] * mComponents[0] / mComponents[1])
/ (mComponents[3] - mComponents[4] * mComponents[0] / mComponents[1]);
v.mX = (mComponents[7] - v.mY * mComponents[4]) / mComponents[1];
}
else if (mComponents[2] != 0)
{
v.mY = (mComponents[6] - mComponents[8] * mComponents[0] / mComponents[2])
/ (mComponents[3] - mComponents[5] * mComponents[0] / mComponents[2]);
v.mX = (mComponents[8] - v.mY * mComponents[5]) / mComponents[2];
}
else
{
v.mY = (mComponents[7] - mComponents[6] * mComponents[1] / mComponents[0])
/ (mComponents[4] - mComponents[3] * mComponents[1] / mComponents[0]);
v.mX = (mComponents[6] - v.mY * mComponents[3]) / mComponents[0];
}
return v;
}
In most cases this works fine, but for some values eg.
137.66466268987 0.0046204004420716 137.66466268987
-56.938009696394 -0.0019109944413589 -56.938009696394
0.0050000000000000 -148.97481767635 0.00000000000000
i get an -1.#IND for y instead of the correct value -3.35e-5. This is because my code steps into the first branch instead of the second which would produce the correct result. I know that this is a matter of precision so i have to determine which branch would produce the least error rate.
Does anybody know a "smart" way for this?
eloso