I wrote this code to overload the unary operator- on a matrix class:
const RegMatrix RegMatrix::operator-()const{
RegMatrix result(numRow,numCol);
int i,j;
for(i=0;i<numRow;++i)
for(j=0;j<numCol;++j){
result.setElement(i,j,(-_matrix[i][j]));
}
return result;
}
When i ran my program with debugger in visual studio, it showed me that when the operation is done on a double equals zero, it inserts the result matrix the number -0.00000. Is it some weird VS-display feature, or is it something i should handle carefully?
Well for doubles actually have different values for '0.0' and '-0.0' I think it makes perfect sense....
What different result did you expect?
using double (IEEE754), there is defined positive and negative zero.
As ereOn said, you've got a negative zero:
For more information see Signed Zero wiki page.
-0 and 0 are the same thing, and it is nothing to worry about. Floating point numbers have the capability to have both a positive and negative 0, for math reasons. But -0 is interpreted the same way as 0 in C/C++ arithmetic.