整数溢出 [英] integer overflow

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

问题描述




我在我的程序中执行特定操作的整数计数。

在足够大的值之后发生溢出。目前我已经通过将其声明为双倍来解决问题,即使它有

限制。有没有一种方法可以防止这种溢出或某些从中恢复的方法。在这方面的任何帮助将不胜感激。


感谢你。


Ashutosh

Hi ,

I am performing an integer count of a particular operation in my program.
After a sufficiently large value an overflow occurs. At the moment I have
gone around the problem by declaring it as a double, even that has its
limits. Is there a method of preventing this overflow or some method of
recovering from it. Any help in this regard would be greatly appreciated.

Thanking you.

Ashutosh

推荐答案

Ashutosh Iddya写道:
Ashutosh Iddya wrote:

我在我的
程序中执行特定操作的整数计数。在足够大的值之后发生溢出。在我解决这个问题的那一刻,我宣布它是一个双重的,即使这有其局限性。有没有一种方法可以防止这种溢出或某些从中恢复的方法。在这方面的任何帮助将不胜感激。

I am performing an integer count of a particular operation in my
program. After a sufficiently large value an overflow occurs. At
the moment I have gone around the problem by declaring it as a
double, even that has its limits. Is there a method of preventing
this overflow or some method of recovering from it. Any help in
this regard would be greatly appreciated.




如果长时间不做,那么你可以使用long long(在C99或gnu gcc上)

系统)。否则试试:


if(n< INT_MAX)n ++;

else {

溢出++;

n = 0;

}


并且不要忘记#include< limits.h>


使用无符号类型会更简单,然后:


if(!(++ n))overflow ++;


-

答:因为它会破坏人们通常阅读文本的顺序。

问:为什么顶级发布这么糟糕的事情?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



If long won''t do, then you can use long long (on C99 or gnu gcc
systems). Otherwise try:

if (n < INT_MAX) n++;
else {
overflows++;
n = 0;
}

and don''t forget to #include <limits.h>

It would be simpler to use unsigned types, and then:

if (!(++n)) overflow++;

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


谢谢你。我会尝试一下,让你知道它是怎么回事

Ashutosh

" CBFalconer" < CB ******** @ yahoo.com>在消息中写道

新闻:40 *************** @ yahoo.com ...
thanks for that. I will try it out and let you know how it went
Ashutosh
"CBFalconer" <cb********@yahoo.com> wrote in message
news:40***************@yahoo.com...
Ashutosh Iddya写道:
Ashutosh Iddya wrote:

我在我的
程序中执行特定操作的整数计数。在足够大的值之后发生溢出。在我解决这个问题的那一刻,我宣布它是一个双重的,即使这有其局限性。有没有一种方法可以防止这种溢出或某些从中恢复的方法。在这方面的任何帮助将不胜感激。

I am performing an integer count of a particular operation in my
program. After a sufficiently large value an overflow occurs. At
the moment I have gone around the problem by declaring it as a
double, even that has its limits. Is there a method of preventing
this overflow or some method of recovering from it. Any help in
this regard would be greatly appreciated.



如果长期不做,那么你可以使用多长(在C99或gnu gcc
系统)。否则试试:

if(n< INT_MAX)n ++;
else {
溢出++;
n = 0;
}
<并且不要忘记#include< limits.h>

使用无符号类型会更简单,然后:

if(!( ++ n))溢出++;

- 答:A:因为它会破坏人们通常阅读文本的顺序。
问:为什么顶级发布这么糟糕的事情?答:最热门的帖子。
问:usenet和电子邮件中最烦人的事情是什么?



If long won''t do, then you can use long long (on C99 or gnu gcc
systems). Otherwise try:

if (n < INT_MAX) n++;
else {
overflows++;
n = 0;
}

and don''t forget to #include <limits.h>

It would be simpler to use unsigned types, and then:

if (!(++n)) overflow++;

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?



2004年4月16日星期五,Ashutosh Iddya写道:
On Fri, 16 Apr 2004, Ashutosh Iddya wrote:


我正在我的程序中执行特定操作的整数计数。
在足够大的值之后发生溢出。目前我已经通过将其声明为双重来解决问题,即使它有其限制。有没有一种方法可以防止这种溢出或某些方法从中恢复。在这方面的任何帮助将不胜感激。
Hi ,

I am performing an integer count of a particular operation in my program.
After a sufficiently large value an overflow occurs. At the moment I have
gone around the problem by declaring it as a double, even that has its
limits. Is there a method of preventing this overflow or some method of
recovering from it. Any help in this regard would be greatly appreciated.




如果您的数据类型发生溢出,您可以检测到它但是

防止它意味着切换到不同的数据类型。对于整数

数据类型(long long)是最好的选择。如果你只使用

无符号数,那么(unsigned long long)是你最好的选择。


你可能仍然会溢出。如果是这种情况,您可以尝试使用双

作为您的数据类型。另一种选择是在网上搜索大号C

库。有些库你可以使用大于未签名的数字

很长。


如果你使用大号库它可能会慢于

使用long long。你可能我可以创建你自己的有限大号

库。如果你只算数,那么你只需要添加和某种方式

打印数字。


-

发送e邮件:darrell at cs dot toronto dot edu

不要发电子邮件给 vi ************ @ whitehouse.gov


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

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