checking for a posetive difference.

This is a discussion on checking for a posetive difference. within the C++ Programming forums, part of the General Programming Boards category; hi Code: if(a-b>0) I remember there is some catch in doing this but what is it? ... some casting I ...

  1. #1
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116

    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.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Looks good to me as long as there is no overflow.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You could just write it as
    Code:
    if (a > b)

  4. #4
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Quote Originally Posted by robatino View Post
    You could just write it as
    Code:
    if (a > b)
    yeah but I want to check for positivness so this is more readable.
    Last edited by kroiz; 07-17-2007 at 10:09 PM.

  5. #5
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Quote Originally Posted by Daved View Post
    Looks good to me as long as there is no overflow.
    that is what I am worried about.

  6. #6
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    actually how do I do
    Code:
    if(x<0)
    if x is unsigned.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by kroiz View Post
    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.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by kroiz View Post
    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.

  9. #9
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Quote Originally Posted by robatino View Post
    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.
    Last edited by kroiz; 07-16-2007 at 10:52 PM.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.)

  11. #11
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    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.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.
    Last edited by robatino; 07-16-2007 at 11:10 PM.

  13. #13
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Thanks robatino. (-:

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,663
    > 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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Overflow and range checking for mul/div
    By Elysia in forum C++ Programming
    Replies: 28
    Last Post: 06-06-2008, 02:09 PM
  3. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  5. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21