handling overflow, underflow etc

This is a discussion on handling overflow, underflow etc within the C Programming forums, part of the General Programming Boards category; Hi, I have written a small function that can (I think) deal with overflow/underflow while adding integers and divide by ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    87

    handling overflow, underflow etc

    Hi, I have written a small function that can (I think) deal with
    overflow/underflow while adding integers and divide by zero problem.
    Can some one please tell me if I have done it correctly ? Would it be
    safe to extend it to double precision floating point numbers since we
    know that there is always some margin for error while adding floating
    point numbers ? What other functions should I look to implement for my
    project which needs heavy numerical computation ?

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int add_chk(int a, int b)
    {
        if (b > 0)
        {
            if (a > INT_MAX - b)
            {
                fprintf(stderr, "Overflow error\n");
                return (INT_MAX);
            }
        }
    
        if (b < 0)
        {
            if (a < INT_MIN - b)
            {
                fprintf(stderr, "Underflow error\n");
                return (INT_MIN);
            }
        }
    
        return (a + b);
    
    }
    
    int div_chk(int a, int b)
    {
        if (b == 0)
        {
            fprintf(stderr, "Division by zero attempted\n");
            return (INT_MAX);
        }
    
        return (a/b);
    
    }
    
    int main(void)
    {
        int i;
        int x, y;
    
        clrscr();
        printf("Enter two numbers\n");
        if (scanf("%d %d", &x, &y) != 2)
        {
          fprintf(stderr, "Error in user input\n");
          return (1);
        }
    
        if  ((x < INT_MAX && x > INT_MIN) && ( y < INT_MAX && y > INT_MIN))
        {
            i = add_chk(x, y);
            printf("Sum: %d\n", i);
            i = div_chk(x, y);
            printf("Div: %d\n", i);
        }
        else
        {
          fprintf(stderr, "Values entered are out of range\n");
          return (1);
        }
        return (0); 
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Other than some flawed logic it's fine.
    Code:
    if (scanf("&#37;d %d", &x, &y) != 2)
    /* ... */
    (x < INT_MAX && x > INT_MIN)
    If they enter a value less than INT_MIN or greater than INT_MAX overflow would have already occured. Unless when printf() scans it counts the # of digits (I don't know if it does). Even then you can still overflow...

    Even if it could work you'd want less than or equal to INT_MAX and greater than or equal to INT_MIN. To be in range it should be, x [INT_MIN, INT_MAX] not x (INT_MIN, INT_MAX) (ie it should be inclusive).
    Last edited by zacs7; 07-02-2008 at 06:10 AM.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Quote Originally Posted by zacs7 View Post
    Other than some flawed logic it's fine.
    Code:
    if (scanf("%d %d", &x, &y) != 2)
    /* ... */
    (x < INT_MAX && x > INT_MIN)
    If they enter a value less than INT_MIN or greater than INT_MAX overflow would have already occured. Unless when printf() scans it counts the # of digits (I don't know if it does). Even then you can still overflow...

    Even if it could work you'd want less than or equal to INT_MAX and greater than or equal to INT_MIN. To be in range it should be, x [INT_MIN, INT_MAX] not x (INT_MIN, INT_MAX) (ie it's should be inclusive).
    I think using fgets with strtol can help me in doing that. It is true that scanf and i guess fscanf doesn't check for overflows. Btw with <= there is a problem with floats.

    What other checks can i include ? overflow/underflow for multiplication and division may be. I really don't know wabout others though like nan, inf, loss of precision, invalid number etc
    Last edited by broli86; 07-02-2008 at 06:12 AM.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    If you're going that far, perhaps try parse the numbers yourself (that way it's even easier to find overflow) -- but only if strtol() doesn't do that. As it can be rather challenging!

    As soon as you step into NaN, Infinity, etc then you're really taking it another step. It depends how far you want to go with this? I certainly don't do such tests, instead I rely on the context... example:

    Code:
    int do_something_important(int a, int b)
    {
        if(b == 0)
            b = 1;
    
        return (a / b);
    }
    I'd rather a value that makes less sense than one that produces an error (well in this case).

    Perhaps the best method is try and avoid situations where it would occur.
    Last edited by zacs7; 07-02-2008 at 06:27 AM.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    if a larger type is available (eg 64-bit in your case, via "long long int" or "__int64") you can always cast it to the bigger type first and do the addition there and check the result.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by broli86 View Post
    Code:
        clrscr();
    You do know that clrscr() isn't a standard C/C++ function right?

    What about mult_chk() and subt_chk()?

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Quote Originally Posted by cpjust View Post
    You do know that clrscr() isn't a standard C/C++ function right?

    What about mult_chk() and subt_chk()?
    I don't think subt_chk is needed because add_chk can take care of it. I have to implement mul_chk and make div_chk more elaborate as to how it should deal with :

    INT_MIN/ -1 , 0/0 inf/0 etc etc

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    0/0 is Not a number (or it is, rather confusing )

    Ie, how many times do you have to subtract 0 from 0 to get to 0? 1 is correct, as is 2 as is 3, even 58974

    Perhaps take after the IEEE floating point standard "n / 0 is positive infinity when n is positive, negative infinity when n is negative, and NaN when n = 0". Maybe:

    Code:
    enum divideError_e
    {
        DE_NONE = 0,
        DE_NAN,
        DE_DIVIDE_BY_ZERO
        /* ... etc */
    };
    
    enum divideError_e divide_check(int a, int b)
    {
        if(a == 0 && b == 0)
            return DE_NAN;
    
        if(b == 0)
            return DE_DIVIDE_BY_ZERO;
    
        /* etc */
    
        return DE_NONE;
    }
    
    /* ... */
    
    int a = 10, b = 20, result = 0;
    
    if(divide_check(a, b) == DE_NONE)
        result = a / b;

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Quote Originally Posted by zacs7 View Post
    0/0 is Not a number (or it is, rather confusing )

    Ie, how many times do you have to subtract 0 from 0 to get to 0? 1 is correct, as is 2 as is 3, even 58974

    Perhaps take after the IEEE floating point standard "n / 0 is positive infinity when n is positive, negative infinity when n is negative, and NaN when n = 0". Maybe:

    Code:
    enum divideError_e
    {
        DE_NONE = 0,
        DE_NAN,
        DE_DIVIDE_BY_ZERO
        /* ... etc */
    };
    
    enum divideError_e divide_check(int a, int b)
    {
        if(a == 0 && b == 0)
            return DE_NAN;
    
        if(b == 0)
            return DE_DIVIDE_BY_ZERO;
    
        /* etc */
    
        return DE_NONE;
    }
    
    /* ... */
    
    int a = 10, b = 20, result = 0;
    
    if(divide_check(a, b) == DE_NONE)
        result = a / b;
    Thanks for that idea. I also have to take care of division overflow i.e. a/b > DBL_MAX. I'm not sure if loss of precission will be an issue since I'm using double data type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 11:01 PM
  2. event handling is serialized in MS Visual Studio C++ 2005 ??
    By mynickmynick in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2008, 05:47 AM
  3. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 04:20 PM
  4. Signed Char Overflow
    By coder8137 in forum C Programming
    Replies: 5
    Last Post: 11-17-2006, 08:25 AM
  5. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM

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