I have always wondered about the following situation.
Let L_1 > L_2
be unsigned values (in this case size_t
). Also A
is of the same type.
Will this test work:
if( A > (L_1 - L_2) )
{
// do stuff
}
or is it equivalent to
if( 0 > (L_1 - L_2) - A )
{
// do stuff
}
or
if( 0 < A - (L_1 - L_2) )
{
// do stuff
}
which simplifies to
if(!(A - (L_1 - L_2) ))
{
// do stuff
}
Since everything is unsigned
. The last thing will "do stuff" as soon as A != L_1 - L2
, which is not what I want.
We know there are plenty of values for whichA > (L_1 - L_2)
is true.
For example, with L_1 == L_2
and A == UINT_MAX
.
But 0 > (L_1 - L_2) - A
is always false. As L1
, L2
and A
are unsigned operands (L_1 - L_2) - A
is an unsigned expression. The value of an unsigned expression is always >= 0
.