473,408 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 ?

#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
11 3164
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
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

"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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Viktor Lundström | last post by:
Hi! I recently decided to write a streambuf which handles streamed IO to a device (ie. a socket). Now, in an effort to move away from C-style IO error handling (ie. if(read(..) == -1) ...), I...
7
by: Oplec | last post by:
Hello, How can underflow and overflow of the basic arithmetic types be tested for using standard C++? For instance, if two long doubles are multiplied together, test whether or not the result...
4
by: aling | last post by:
Given: signed a, b; How to judge overflow of the sum of these two operands? Just use one *if* statement. Is this statement right? if ((a>0 && b>0 && sum<=0) || (a<0 && b<0 && sum>=0)) //...
5
by: juergen perlinger | last post by:
Hello out there. sometimes I need to have proper control of the floating point arithmetic of the C(and C++) runtime system, and using the f.p. exception handling of the C99 standard is quite...
2
by: Paul Emmons | last post by:
Can anyone suggest example code, or how to proceed, in adding or subtracting two long long integers (I'm working in gcc with Linux, where a long long is 64 bits) and determining whether an overflow...
5
by: Ian Pilcher | last post by:
I'm trying to figure out if an increment to a variable of an integer type, followed by a decrement, (or vice versa) is guaranteed to restore the variable to its initial value, even if the first...
4
by: Tom | last post by:
I have a VB.NET framework 1.1 application that I am installing on my user's workstation. It works fine on EVERY machine except for one - on this one machine it generates a 'Overflow or underflow in...
4
by: Raymond | last post by:
Source: http://moryton.blogspot.com/2007/08/detecting-overflowunderflow-when.html Example from source: char unsigned augend (255); char unsigned const addend (255); char unsigned const sum...
2
by: jou00jou | last post by:
Hi, I have trouble using sscanf and fgets to check for overflow. I will post the assignment specification so I could help whoever would kindly like to offer his/her help. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.