Error converting double to double @ k = c

At the line stating k = c i'm encountering an issue any help would be appreciated.

#include <iostream>

using namespace std;



//Declarations of functions
void print3x3Matrix(double inpC[][3], int numRows);

//Starting main function
int main ()
{
// declaring variables for 2x2 matrices
int i, j, k, counti = 0;
double d[3][3], c[3][3], res[3][3];
double triangular, factor;
// creation of 2x2 matrices A an B
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
c[i][j] = i + (j*3);
d[i][j] = c[i][j] * 2;
}
}

// Displaying matrices A and B by calling
cout << "The 3x3 of Matrix C: " << endl;
print3x3Matrix(c, 2);
cout << "The 3x3 of Matrix D: " << endl;
print3x3Matrix(d, 2);

// Transform matrix into upper triangular
for(i = 0; i < 3 - 1; i++)
{
// Elementary Row Operation I
if(c[i][i] == 0)
{
for(k = i; k < 3; k++)
{
if(c[k][i] != 0)
{
for(j = 0; j < 3; j++)
{
triangular = c[i][j];
c[i][j] = c[k][j];
c[k][j] = triangular;
}

k = c;
}
}
counti++;
}

// Elementary Row Operation III
if(c[i][i] != 0)
{
for(k = i + 1; k < 3; k++)
{
factor = -1.0 * c[k][i] / c[i][i];
for(j = i; j < 3; j++)
{
c[k][j] = c[k][j] + (factor * c[i][j]);
}
}
}
}

// Display upper triangular matrix
printf("\nUpper triangular:\n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
cout <<" "<< c[i][j];
}
cout << endl;
}
triangular = 1.0;

// Calculate determinant
for(i = 0; i < 3; i++)
{
triangular *= c[i][i];
}
// Modify determinant
cout << "\nDeterminant:\n";
if(counti % 2 == 0)
{
cout << endl << " " << triangular <<endl <<endl;
}
else
{
cout << endl << " " << -1 * triangular << endl << endl;
}


cin.get();
cin.get();
}


void print3x3Matrix(double inpC[][3], int numRows)
{
int i, j;
for(i = 0; i < numRows; i++)
{
for(j = 0; j < 3; j++)
{
cout << inpC[i][j] << " ";
}
cout << endl;
}
}
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
Last edited on
I just realized that when I rewrote my code in haste for this small portion I forgot to replace a variable with the number 3 and instead put c in its place which is why it wasn't working.

k = 3; is what it should read!

Sorry for the post!

But thanks for the refresher on precision usage. I've only ever used it once in class so it completely slipped my mind.

On another note though, is there any advice you could give me on how to more efficiently add in the output of the determinant for the matrix D as well as the matrix C without copying all of that code and replacing c with d?
Last edited on
Topic archived. No new replies allowed.