C/C++
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Web Development Archives FAQs C/C++

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Web Development Archives Sponsor:
  #1  
Old July 2nd, 2008, 06:49 AM
pereges
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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, " 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);
}

Reply With Quote
  #2  
Old July 2nd, 2008, 05:49 PM
Peter Nilsson
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
handling overflow, underflow etc

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, " 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

Reply With Quote
  #3  
Old July 4th, 2008, 09:19 AM
pereges
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
handling overflow, underflow etc

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.

Reply With Quote
  #4  
Old July 4th, 2008, 09:19 AM
santosh
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
handling overflow, underflow etc

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?


Reply With Quote
Reply

Viewing: Web Development Archives FAQs C/C++ > handling overflow, underflow etc


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway