处理溢出,下溢等

编程入门 行业动态 更新时间:2024-10-11 15:23:48
本文介绍了处理溢出,下溢等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我写了一个小函数,可以(我认为)处理 溢出/下溢同时添加整数并除以零问题。 可以有一个如果我做得对,请告诉我?因为我们 知道在添加浮动时总是存在一些错误余量 $ b $是否可以安全地将它扩展到双精度浮点数b点数?我应该为我的 项目实现哪些其他功能? #include< stdio.h> #include < limits.h> int add_chk(int a,int b) { if(b 0) { if(a INT_MAX - b) { fprintf(stderr,"溢出错误\ n"); 返回(INT_MAX); } } if(b < 0) { if(a< INT_MIN - b) { fprintf(stderr ,下溢错误\ n); 返回(INT_MIN); } } 返回(a + b); } int div_chk(int a,int b) { if(b == 0) { fprintf(stderr,除以零的尝试); 返回(INT_MAX); } 返回(a / b); } int main(无效) { int i; int x,y; clrscr(); printf(输入两个数字\ n); if (scanf("%d%d",& x,& y)!= 2) { fprintf(stderr,"用户输入错误) \ n"); 返回(1); } if((x< INT_MAX&& x INT_MIN)&& (y< INT_MAX&>> 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 ,输入的值超出范围\ n;) 返回(1); } return(0); }

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

推荐答案

pereges写道: pereges wrote: 我已经编写了一个小函数,可以(我认为)处理 溢出/下溢同时添加整数并除以零问题。 有人可以告诉我是否我做得对吗?因为我们 知道在添加浮动时总是存在一些错误余量 $ b $是否可以安全地将它扩展到双精度浮点数b点数?我应该为我的 项目实现哪些其他功能? [...] int div_chk(int a,int b) { if(b == 0) { fprintf(stderr,"除以零attempts \ n"); return(INT_MAX); 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);

我不相信INT_MAX是返回 -1/0或0/0的最佳值。 />

I''m not convinced INT_MAX is the best value to return for -1/0 or for 0/0.

} return(a / b); } return (a/b);

INT_MIN / -1可能会造成麻烦,或者至少会引起一些人的担忧。你的测试程序(由于我不清楚的原因) 可以防止这种情况发生,但是如果你想写一些通用库你可能不会依赖它在 输入被消毒。 至于这一切是否是个好主意 - 好吧,我想你 需要考虑用例对于你的图书馆:什么类型 的程序会使用它,为什么以及 程序会有什么样的行为?换句话说,我担心你可能已经在没有先制定战略的情况下制定战术决策。 - Er ********* @ sun

pereges写道: 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,溢出错误\ n); 返回(INT_MAX); } } if(b< 0) #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)

如果我将使用else。

I''d use else if.

{ if(a< INT_MIN - b) { fprintf(stderr,Underflow error\ n); { if (a < INT_MIN - b) { fprintf(stderr, "Underflow error\n");

通常认为这不是下溢。下溢是浮点数缺乏 精度,例如: 10e50 + 10e-50通常下溢。

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); } } 返回(a + b); } return (INT_MIN); } } return (a + b); }

打印到stderr会结合你的功能。这不一定是坏bb,但是我更喜欢将errno设置为ERANGE并返回0.让 来电者确定溢出的内容。 ....

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(); int main(void) { int i; int x, y; clrscr();

当您发布到clc时请忽略非标准功能。

Please elide non-standard functions when you post to clc.

printf(" Enter two) number \\\"); if(scanf("%d%d",& x,& y)!= 2) { fprintf(stderr,用户输入错误\ n); return(1); 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&>> INT_MIN)) } if ((x < INT_MAX && x INT_MIN) && ( y < INT_MAX && y > INT_MIN))

由于x和y是整数,在什么时间点它们的值将在int范围之外为?

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); } 其他 { fprintf(stderr,输入的值超出范围\ n); return(1 ); } 返回(0); } { 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

-- Peter

" Peter Nilsson" < ai *** @ acay.auha scritto nel messaggio news:31 ************************ ********** @ v26g2000 prm.googlegroups ... "Peter Nilsson" <ai***@acay.auha scritto nel messaggio news:31**********************************@v26g2000 prm.googlegroups... pereges写道: pereges wrote: > printf(输入两个数字\ n); if(scanf("%d%d",& x,& y)!= 2) { fprintf(stderr,用户输入错误\ n); return(1); > 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.

出于好奇,你知道一个系统有这些宏定义 有不同的价值吗? #define EXIT_SUCCESS 0 #define EXIT_FAILURE 1 据我所知,只有0和EXIT_SUCCESS,EXIT_FAILURE由定义 C标准,但想知道这种限制是否纯粹是哲学的......

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

更多推荐

处理溢出,下溢等

本文发布于:2023-10-24 20:51:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!