# checking for a posetive difference.

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-16-2007
kroiz
checking for a posetive difference.
hi

Code:

`if(a-b>0)`
I remember there is some catch in doing this but what is it?
... some casting I should do?
in what cases would it work and when not.
• 07-16-2007
Daved
Looks good to me as long as there is no overflow.
• 07-16-2007
robatino
You could just write it as
Code:

`if (a > b)`
• 07-16-2007
kroiz
Quote:

Originally Posted by robatino
You could just write it as
Code:

`if (a > b)`

yeah but I want to check for positivness so this is more readable.
• 07-16-2007
kroiz
Quote:

Originally Posted by Daved
Looks good to me as long as there is no overflow.

that is what I am worried about.
• 07-16-2007
kroiz
actually how do I do
Code:

`if(x<0)`
if x is unsigned.
• 07-16-2007
robatino
Quote:

Originally Posted by kroiz
yeah but I want to check for posetivness so this is more readable.

There are only two possible issues with your expression that I can think of: first, if a and b are pointers into an array, then a-b is computed using a type called ptrdiff_t which is supposed to represent the integer difference between two pointers, but is not actually guaranteed to be able to hold the result. The other is that if a and b are unsigned integral types, then I think the difference is calculated as an unsigned type first and then compared to 0 (which is bad, since a-b would always be >= 0 regardless of a and b). I just tested this with gcc and this in fact happens, for example with a = 1 and b = 2, the test a-b > 0 passes. So my expression is safer for unsigned types - it should work properly in this case.
• 07-16-2007
robatino
Quote:

Originally Posted by kroiz
actually how do I do
Code:

`if(x<0)`
if x is unsigned.

You can't. If x needs to hold negative values, it has to be signed.
• 07-16-2007
kroiz
Quote:

Originally Posted by robatino
You can't. If x needs to hold negative values, it has to be signed.

but x should not be able to hold negative values.

Code:

```unsigned int x=3; unsigned int y=4; for(unsigned int i ; i< x-y ++i)     foo();```
see the problem here? I would like the loop to run zero times.

I would like something like this:
Code:

```unsigned int x=3;  unsigned int y=4; int diff = x-y; if(diff<0)     diff=0; for(unsigned int i ; i<diff; ++i)     foo();```
another problem is that diff in the above example might not be big enough to hold the difference between x and y, since it signed and thus half the size.
• 07-16-2007
robatino
Code:

`for(unsigned int i ; i+y<x; ++i)`
Now both sides of the comparison are >=0 and it works. (BTW, you need to initialize i, I didn't since I don't know what value you want.)
• 07-16-2007
kroiz
I see, I guess I can not avoid doing a comparison of who is bigger.
I just thought it is more readable to actually compare to zero.
• 07-16-2007
robatino
You could also use
Code:

`unsigned int diff = (x > y)? x-y : 0;`
and then
Code:

`i < diff`
which would be a little more efficient since you're not doing the addition each time through the loop.
• 07-16-2007
kroiz
Thanks robatino. (-:
• 07-16-2007
Salem
> ptrdiff_t which is supposed to represent the integer difference between two pointers,
> but is not actually guaranteed to be able to hold the result.
Where did this come from?
• 07-16-2007
robatino
In the C standard,

http://c0x.coding-guidelines.com/6.5.6.html

see #1166. I'm assuming the C++ standard has the same behavior.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last