handling overflow, underflow etc

P

pereges

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 ?

#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);
}
 
P

Peter Nilsson

pereges said:
#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)

I'd use else if.
{
if (a < INT_MIN - b)
{
fprintf(stderr, "Underflow error\n");

This is not normally thought of as underflow. Underflow is a lack of
precision in floating point, e.g. 10e50 + 10e-50 typically underflows.
return (INT_MIN);
}
}

return (a + b);
}

Printing to stderr couples your function. That's not necessarily
bad, but I prefer to set errno to ERANGE and return 0. Let the
caller decide what to with the overflow.

....
int main(void)
{
int i;
int x, y;

clrscr();

Please elide non-standard functions when you post to clc.
printf("Enter two numbers\n");
if (scanf("%d %d", &x, &y) != 2)
{
fprintf(stderr, "Error in user input\n");
return (1);

Please use portable return values.
}

if ((x < INT_MAX && x > INT_MIN) && ( y < INT_MAX && y >
INT_MIN))

Since x and y are ints, at what point in time will their values be
outside the range of an int?
 
C

CBFalconer

Lorenzo said:
.... snip ...

Just out of curiosity, do you know a system which has these macros
defined with different values?

#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

I understand that only 0 and EXIT_SUCCESS, EXIT_FAILURE are
defined by the C standard but wondered if that restriction was
purely philosophical....

Yes. No.
 
J

jacob navia

Do not confuse pedantry with philosophy.
OpenVMS. (But it's been more than fifteen years since
I used it, and things may have changed.)

Yeah... Most of the regulars answer stuff like this when they
are asked for a concrete example.
 
S

santosh

jacob said:
Do not confuse pedantry with philosophy.


Yeah... Most of the regulars answer stuff like this when they
are asked for a concrete example.

EXIT_FAILURE is perfectly appropriate to indicate generic failure. I
would consider other values only when I need to return more than just
success or failure. This is not necessary in small programs of the like
that are most often posted here in c.l.c.

Gratuitously using 1 when EXIT_FAILURE will do is merely restricting
portability for no good reason.
 
P

pereges

In C99 it seems there are some very good methods to handle such math
errors. My pelles C compiler lists a few of them but then again
portability is the problem.
 
S

santosh

pereges said:
In C99 it seems there are some very good methods to handle such math
errors.

Such as?
My pelles C compiler lists a few of them but then again
portability is the problem.

How many systems do you need your program to work under? Have you
checked whether they have C99 conformant implementations or compiler
that implement the subset of C99 that interests you?
 
B

Barry Schwarz

Just out of curiosity, do you know a system which has these macros defined
with different values?

#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

I understand that only 0 and EXIT_SUCCESS, EXIT_FAILURE are defined by the
C standard but wondered if that restriction was purely philosophical....

The IBM mainframe compiler defines EXIT_FAILURE as 8 in keeping with
traditional "return code" conventions which are paraphrased:
0 success
4 minor problems or warnings (output probably useful)
8 non-trivial problems or errors (output probably unusable)
12 serious problems (output probably suppressed or truncated)
16 complete failure


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top