So I had some code that would return -0, so I used a checker similar to this:
if(result == 0) result = 0;
However, after reading a bit from an assembly language tutorial, I found out that -0 is different by one bit. Does that mean that the following will work? And be way more efficient?
result ^= -0;
I can't test this on my code that produces -0 because there's a whole lot of introduced bugs I still need to fix. Plus I don't really know how to write another piece of code that would result in -0. Anyway, I just need to know if this alternative would be a great optimizer.
If I'm not mistaken, ^= is the compound assignment operator for XOR?
The check I have currently works fine. I just want to know if the alternative will make the program more efficient. The code that produces the -0 is really slow with large amounts of data, but I'm going to optimize it after I finish fixing the bugs.
^ and ^= are much slower than checking for equality and assigning.
-0 and 0 are identical unless you're dealing with floating point types, but it shouldn't matter anyway since you're smart enough not to use direct == comparison.
Why do you think -0 is a problem? For integers, there is no -0, it is the same as 0.
Here's more of the code (result is actually double).
1 2 3 4 5 6 7 8 9 10 11 12 13
//Perform Cross Product
vector<double> toreturn;
unsigned column;
int sign;
//Create outside for loop according to number of components
for(column=0, sign = 1; column < vectorlist.begin()->size(); column++, sign *= -1){
//Explicity check for a result of 0 and push back 0 to avoid the -0 architect
//Declare temporary variable to avoid calling Determinant twice and avoid overhead cost
double coefficient = ( catalyst.Splice(column) ).Determinant(componentnumber-1);
if(coefficient == 0) toreturn.push_back(0);
else toreturn.push_back(sign*coefficient);
}
return toreturn;
When I first tested it out (the code worked back then and without the 0 checker) with input that should have resulted in <0,0,0>, I always got <0,-0,0>.