John Harrison wrote:
I have a problem. I want to compare an integral value, n, against
three half open ranges as follows
[-A, 0) // range 1
[0, B) // range 2
[B, C} // range 3
Each range corresponds to a different outcome and if the integral
value isn't within any of the ranges, that's a fourth outcome. So
far so easy, the problem is that n is a signed quantity and A, B, C
are unsigned quantities. Apart from this obscure corner of my code
this makes perfect sense, so I don't want to change the signedness
of anything. How to I write these tests so that my code is reasonably
understandable, rather than a horrible mess of casts and compiler
warnings? One more point, of the unsigned quantity, only B is guaranteed
small
enough that it could be safely cast to a signed integer.
What about:
#include <iostream>
int main ( void ) {
unsigned long A = 30;
unsigned long B = 20;
unsigned long C = 100;
long x = 0;
while ( std::cin >> x ) {
if ( x >= 0 ) {
if ( x < B ) {
std::cout << "range 2";
} else if ( x < C ) {
std::cout << "range 3";
} else {
std::cout << "above all ranges";
}
} else {
if ( ( -x ) <= A ) {
std::cout << "range 1";
} else {
std::cout << "below all ranges";
}
}
std::cout << '\n';
}
}
This only compares positive values. As long as the unsigned type is
large enough to represent the absolute values of the signed type,
you will be fine.
I thought of something like that, but two problems.