We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
There a two unsigned integers. Lets call them red and blue. How can i put them in a if condition?
The if condition should be true, if the difference between the two (unsigned integer) variables is greater than a constant value?
I assume i should use the labs function in such way:
if (labs((signed int)red - (signed int)blue) > 22){ // doing something if condition is true :-) }
is there a better way to do?
In which library is the labs function? Thanks in advance for being patient ;-)
You put it there - why do you think it is necessary?
I don't see any reason for it and, without range checking, it could give unexpected results...
Hi Andy, thanks for your help. Your right, it is not necessary.
Bevor i had this version
if (labs((signed int)red - blue) > 22)
As i had not included math.h so i got an errormessage when i wanted to use the "abs()" function.
I know this function from good old BASIC :-)
But at that time, i missinterpreted the C errormessage in that way, that i believed, "abs()" is not a C function. So i took a look to my 19 year old "QuickC 2.0" quick reference book. I saw that there is a labs() function.
I just used the (signed int) cast to force the result to be signed. And i thought another cast in front of blue would look nice.
Yes, i understand, if the value of an unsigned int is equal or greater than the half of the possible range (the most significant bit is "1") then casting to (signed int) will give an unexpected result.