views:

110

answers:

7

I've seen this code for finding a Minor of a matrix:

RegMatrix RegMatrix::Minor(const int row, const int col)const{
  //printf("minor(row=%i, col=%i), rows=%i, cols=%i\n", row, col, rows, cols);
 assert((row >= 0) && (row < numRow) && (col >= 0) && (col < numCol));

 RegMatrix result(numRow-1,numCol-1);

 // copy the content of the matrix to the minor, except the selected
    for (int r = 0; r < (numRow - (row >= numRow)); r++){
  for (int c = 0; c < (numCol - (col > numCol)); c++){
   //printf("r=%i, c=%i, value=%f, rr=%i, cc=%i \n", r, c, p[r-1][c-1], r - (r > row), c - (c > col));
   result.setElement(r - (r > row), c - (c > col),_matrix[r-1][c-1]);
  }
 }
     return result;
}

This is the first time I encounter a code line like this: r < (numRow - (row >= numRow)).

What does this mean?

A: 

(row >= numRow) is a boolean expression, which when used like this gets converted to an int, with true becoming 1 and false becoming 0

usta
+1  A: 

(row >= numRow) is a boolean expression. If operator>= has not been overloaded, it should evaluate to true if row is greater or equal to numRow, and to false otherwise. When casting this boolean to an integer for subtraction, it will become 1 if true, 0 else.

Benoit
oh i get this now.
limlim
limlim
+1  A: 

A somewhat clearer way to express it might be:

r < (row >= numRow ? numRow - 1 : numRow)

Paul R
A: 

row >= numRow will return a boolean true or false, and that is the implicitly cast into an integer 0 or 1. So what that does is basically same as:

r < (numRow - (row >= numRow ? 1 : 0))
reko_t
A: 

row >= numRow will return 1 if row is greater than or equal to numRow, otherwise 0.

Thus, the code line is equivalent to a function like this:

bool foo() { 
    if(row >= numRow)
       return r < numRow - 1;
    else
       return r < numRow;
}
teukkam
A: 

The result of using a comparison operator is either a true of false which when used as an integer is 1 or 0.

r < (numRow - (row >= numRow))

is same as

r < (numRow - 1) if (row >= numRow)

else it is r < numRow

codaddict
A: 

As others have said before, it is just a bool expression that is cast into an int before substracting (it means: substract 1 if row >= numRow).

But I would add that it is quite absurd. You have already asserted that row < numRow, so row >= numRow would violate the preconditions for your function. And the same happens for col > numCol in the following line.

Gorpik