comparison between the signed and unsigned integer

Hello forum

I am having the warning from the GCC compiler related to the issue. I have defined the size of a domain as follows:

 
glm::uint64 T;


Since it is a domain size, it is strongly assumed that the value will always be positive.

Then we have the query window size within the domain as follows and it is also assumed that the query window size is positive.

1
2
glm::uint64 p;
glm::uint64 q;



At last we have the coordinate denoting the lower left corner of the query window as follows:

1
2
glm::int64 x;
glm::int64 y;


The coordinate value could be negative . I get the warning when I do some comparison as follows:

1
2
    if(x < T/2 && y < T/2 && (x + q) >= T/2 && (y + p) >= T/2 )
        return WindowQueryType::type0;



The warning is as follows:
1
2
3
/home/QuadSplittingWindowQuery/QuadSplitter.cpp:122: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(x < T/2 && y < T/2 && (x + q) >= T/2 && (y + p) >= T/2 )
                     ^



I am seeking some hints to get around this issue. I believe that the variable declaration is quite reasonable and they may need not to be altered.


Thanks
You need to cast either x and y to unsigned int or T to signed integer.
If you go the cast route, be careful.

If you cast a negative number to unsigned you'll probably get a very large result.

If casting the unsigned type and that unsigned value is larger than what can be accommodated in the signed type you may invoke undefined behavior.

What is the consequence of ignoring this warning ?

Casting seems not to be the optimal way to address the issue.

As mentioned before, the data types cannot be compromised.


Thanks
The best way to handle this "problem" would be to use variables of the same type.

If for example your "domain size" is small enough be held in your signed type, you could use a signed type instead of the unsigned type. You could do checks to insure the variable is never assigned a negative value.

Is the error you reported the complete warning/error listing? Are there any other errors or warnings?
You may do this: const glm::int64 t_2 = T / 2;
Topic archived. No new replies allowed.