|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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); } |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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? |
![]() |
| Viewing: Web Development Archives > FAQs > C/C++ > handling overflow, underflow etc |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|