First,please always use code tags, edit your post, select all the code, then press the <> button on the right under format:
With this part, and similar lines with != :
if(c[i][i] == 0)
The array c is of type double.
This will almost certainly evaluate to false. Floating Point numbers (FP), including floats & doubles are stored as binary fractions & cannot represent every real number. Consider this:
1 2 3 4 5 6 7 8 9 10 11 12
|
float a = 0.1; // a== 0.0999997
float b = 10 * a; // b == 0.9999997
if (b == 1.0) // false
float c = b - 1.0; // c == -0.0000003
if (c == 0.0) //false
float MyPrecison = 0.001;
if (std::abs(c) < MyPrecison ) // true
|
Changing the type to double does not help.
To fix this, either cast the value to int, then compare (this may not suit your application) or; calculate the absolute value of the number and compare to some suitable precision value such as 0.001 say, or; use an epsilon value & a precision.
Google on how to use this:
numeric_limits<double>::epsilon( )
For doubles, this is somewhere around 1e-16, and for floats it is about 1e-7
Epsilon numbers need to be scaled up to suit the number range you are using. There will be a point where the scaled epsilon will be bigger than your arbitrary precision - you should check for this.
With a user precision of 0.001, For floats this will happen at about 1e+4 (10,000) and for doubles at about 1e+13.
So you can see why doubles are a recommended type, however graphics & other libraries often use floats for performance reasons.
HTH