In my code, I need to saturate a variable uint32_t var
within a range [MIN, MAX]
.
MIN
and MAX
are defined as macros, so I can change these values easily inside a header file.
Header:
#define MIN (0)
#define MAX (1000)
Code:
if(var > MAX){
var = MAX;
}else if(var < MIN){
var = MIN;
}
When I now use compiler flag -Wtype-limits
, I get a warning that the second check var < MIN
is always false. This warning suggests that I can remove the second comparison.
This makes sense as long as I define #define MIN (0)
but, when changing this to let's say #define MIN (10)
, then the second comparison is mandatory.
So my question: How can I tell the compiler, that MIN
can be any value greater or equal zero?