处理溢出,下溢等 [英] handling overflow, underflow etc

查看:90
本文介绍了处理溢出,下溢等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小函数,可以(我认为)处理

溢出/下溢同时添加整数并除以零问题。

可以有一个如果我做得对,请告诉我?因为我们

知道在添加浮动时总是存在一些错误余量
$ 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.com


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.com.auha scritto nel messaggio

news:31 ************************ ********** @ v26g2000 prm.googlegroups.com ...

"Peter Nilsson" <ai***@acay.com.auha scritto nel messaggio
news:31**********************************@v26g2000 prm.googlegroups.com...

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


这篇关于处理溢出,下溢等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆