I wrote this function:
static bool colorIsEmpty(const Color col)
{
return (col[0] == 0 && col[1] == 0 && col[2] == 0 );
}
where Color is simply a float[3];
the function doesn’t work if col[3] are all 0;
but this works:
if(col[0] == col[1] == col[2] == 0) {
//gets called
}
however gcc gives me warning:
cColorTest.c:212:5: warning: suggest parentheses around
comparison in operand of ‘==’ [-Wparentheses]
so it would be nice if that function works,why it doesn’t work?
I mean the function always return false,
The condition
is parenthesised
so if
col[0] == col[1], it checks whethercol[2] != 1, and ifcol[0] != col[1], it checks whethercol[2] != 0. I strongly believe that is not what you want.checks whether all three array elements are 0, and if it doesn’t give the expected results, your array elements are not what you expect them to be.
Depending on your application, you might want an approximate test instead of strict equality. Due to the nature of floating point numbers, it is not uncommon that different computations that would mathematically have the same result leads to different results in floating point arithmetic/calculus. Usually the difference is small (but it can also become huge, for example due to loss of significance/catastrophic cancellation), so often floating point values are compared with a tolerance.
Perhaps replacing the
col[i] == 0with an approximate zero test,fabsf(col[i]) < epsilonis the right strategy for your purposes (appropriate values ofepsilondepend on the use case).