By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,581 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

handling overflow, underflow etc

P: n/a
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);
}
Jul 2 '08 #1
Share this Question
Share on Google+
11 Replies


P: n/a
pereges wrote:
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 ?
[...]
int div_chk(int a, int b)
{
if (b == 0)
{
fprintf(stderr, "Division by zero attempted\n");
return (INT_MAX);
I'm not convinced INT_MAX is the best value to return for
-1/0 or for 0/0.
}

return (a/b);
INT_MIN/-1 is likely to make trouble, or at least raise
a few eyebrows. Your test program (for reasons unclear to me)
prevents this case from arising, but if you're trying to write
a general-purpose library you should probably not rely on the
inputs being "sanitized."

As to whether all this is a good idea -- well, I think you
need to consider the "use cases" for your library: What kinds
of programs will use it, and why, and what behavior would the
programs expect? To put it another way, I fear you may already
be making tactical decisions without first settling on strategy.

--
Er*********@sun.com
Jul 2 '08 #2

P: n/a
pereges wrote:
#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?
{
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);
}
--
Peter
Jul 2 '08 #3

P: n/a

"Peter Nilsson" <ai***@acay.com.auha scritto nel messaggio
news:31**********************************@v26g2000 prm.googlegroups.com...
pereges wrote:
> 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.
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....

Jul 4 '08 #4

P: n/a
Lorenzo Villari wrote:
"Peter Nilsson" <ai***@acay.com.auha scritto nel messaggio
news:31**********************************@v26g2000 prm.googlegroups.com...
>pereges wrote:
>> 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.

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....
OpenVMS. (But it's been more than fifteen years since
I used it, and things may have changed.)

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jul 4 '08 #5

P: n/a
Eric Sosman wrote:
Lorenzo Villari wrote:
>"Peter Nilsson" <ai***@acay.com.auha scritto nel messaggio
news:31**********************************@v26g200 0prm.googlegroups.com...
>>pereges wrote:
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.

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....
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.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jul 4 '08 #6

P: n/a
Lorenzo Villari wrote:
>
.... 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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Jul 4 '08 #7

P: n/a
jacob navia wrote:
Eric Sosman wrote:
>Lorenzo Villari wrote:
>>"Peter Nilsson" <ai***@acay.com.auha scritto nel messaggio
news:31**********************************@v26g2000 prm.googlegroups.com...
>>>pereges wrote:
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.

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....

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.
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.

Jul 4 '08 #8

P: n/a
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.
Jul 4 '08 #9

P: n/a
pereges wrote:
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?

Jul 4 '08 #10

P: n/a
jacob navia wrote:
Eric Sosman wrote:
>Lorenzo Villari wrote:
>>>
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
[...]

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.
VMS' condition codes have been discussed here before,
but for those who weren't paying attention:

http://h71000.www7.hp.com/doc/82fina...73pro_022.html
http://blog.isti.cnr.it/cgiplus-bin/...L~exit%2C_exit

At least two generations of hacks have been perpetrated in
attempts to produce usable condition codes from C programs.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jul 4 '08 #11

P: n/a
On Fri, 4 Jul 2008 14:30:27 +0200, "Lorenzo Villari" <vl****@alice.it>
wrote:
>
"Peter Nilsson" <ai***@acay.com.auha scritto nel messaggio
news:31**********************************@v26g200 0prm.googlegroups.com...
>pereges wrote:
>> 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.

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
Jul 4 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.