-
warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if((newsum-oldsum < 10) && ((newsum-oldsum) > -10))
-
@jsulm but the summation result possible to comes negative.
to avoid warning what i need to write.
i.e.
unsigned int x,y;x=2000;
y=3000;if( (y-x) > (-10))
.
.
.@Qt-embedded-developer If I'm not mistaken the result type of "newsum-oldsum" will be unsigned if both are unsigned. Try this:
unsigned a = 1; unsigned b = 2; qDebug() << a - b; qDebug() << signed(a - b);
You will be surprised :-)
You need either to cast the result to signed or cast at least one of both variables to signed. -
warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if((newsum-oldsum < 10) && ((newsum-oldsum) > -10))
@Qt-embedded-developer Well, I guess newsum and oldsum are unsigned? And you compare it with -10, which is signed...
-
@Qt-embedded-developer Well, I guess newsum and oldsum are unsigned? And you compare it with -10, which is signed...
@jsulm but the summation result possible to comes negative.
to avoid warning what i need to write.
i.e.
unsigned int x,y;x=2000;
y=3000;if( (y-x) > (-10))
.
.
. -
warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if((newsum-oldsum < 10) && ((newsum-oldsum) > -10))
-
@jsulm but the summation result possible to comes negative.
to avoid warning what i need to write.
i.e.
unsigned int x,y;x=2000;
y=3000;if( (y-x) > (-10))
.
.
.@Qt-embedded-developer said in why below logic give warning ?:
to avoid warning what i need to write.
for example:
if(qAbs(int(newsum)-oldsum) < 10) { } or if((qMax(newsum, oldsum) - qMin(newsum, oldsum) ) < 10) { }
-
@jsulm but the summation result possible to comes negative.
to avoid warning what i need to write.
i.e.
unsigned int x,y;x=2000;
y=3000;if( (y-x) > (-10))
.
.
.@Qt-embedded-developer If I'm not mistaken the result type of "newsum-oldsum" will be unsigned if both are unsigned. Try this:
unsigned a = 1; unsigned b = 2; qDebug() << a - b; qDebug() << signed(a - b);
You will be surprised :-)
You need either to cast the result to signed or cast at least one of both variables to signed. -
The problem would be avoided by thinking about the safety of the operation. storing the difference of the values in a temporary is more readable than the original expression, and has the desirable side effect of making the type of the result explicit. Furthermore, putting the lower bound conditional after the upper bound conditional is goofy.
int diff = (x - y);
if ((diff > -10) && ((diff < 10)) { true; }I suppose you could implement the conditional expression as a lambda if you're into such things, but IMHO that too would make it less readable.