473,769 Members | 6,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

integer overflow

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

Nov 14 '05 #1
25 2722
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.


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?

Nov 14 '05 #2
thanks for that. I will try it out and let you know how it went
Ashutosh
"CBFalconer " <cb********@yah oo.com> wrote in message
news:40******** *******@yahoo.c om...
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.


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?

Nov 14 '05 #3
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.


If overflow is happening with your data type you could detect it but
preventing it means switching to a different data type. For an integer
data type (long long) is your best bet. If you are only working with
unsigned numbers then (unsigned long long) is your best bet.

You might still get overflow. If this is the case you can try using double
as your data type. Another option is to search the web for "big number C
library". There are libraries you can use for numbers bigger than unsigned
long long.

If you go with the big number library it will probably be slower than
using long long. You might me able to create your own limited big number
library. If you are just counting then you only need addition and some way
of printing the number.

--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@ whitehouse.gov
Nov 14 '05 #4
In <40************ ***********@fre enews.iinet.net .au> "Ashutosh Iddya" <as************ @news.edu.au> writes:
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.


I have a hard time imagining you're going to reach the limits of the
double solution within a reasonable period of time, even if all you
do is counting. Try the following program and see how long it takes
to terminate normally.

#include <stdio.h>

int main()
{
double d = 0;
while (d + 1 != d) d++;
printf("%.16f\n ", d);
return 0;
}

Assumming that the loop takes one CPU cycle per iteration and that the CPU
is running at 4.5 GHz, this proggie should run for about 1e6 seconds, if
using IEEE-754 doubles.

BTW, how about "optimising " the while loop like this?

while (d != ++d) ;

;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5
Darrell Grainger wrote:
On Fri, 16 Apr 2004, 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.
If overflow is happening with your data type you could detect it but
preventing it means switching to a different data type. For an integer
data type (long long) is your best bet. If you are only working with
unsigned numbers then (unsigned long long) is your best bet.

You might still get overflow.


C99's long long int is /at least/ 64 bits wide.

If the OP's counter were incremented 100 times every cycle on a 10 GHz
processor, it would take 213 days for the counter to overflow.
If this is the case you can try using double as your data type.


Bad advice.

An IEEE 754 double will only provide 53 bits of precision.

See DBL_MANT_DIG and FLT_RADIX in float.h

For my information, are there implementations where double is wider
than 64 bits?

Nov 14 '05 #6
In <Pi************ *************** ***@drj.pf> da*****@NOMORES PAMcs.utoronto. ca.com (Darrell Grainger) writes:
On Fri, 16 Apr 2004, 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.


If overflow is happening with your data type you could detect it but
preventing it means switching to a different data type. For an integer
data type (long long) is your best bet. If you are only working with
unsigned numbers then (unsigned long long) is your best bet.


Unless your C compiler, invoked in conforming mode, tells you that
"long long" is a syntax error ;-)

Are the latest Microsoft C compilers supporting long long?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7
Grumble wrote:
[...]
For my information, are there implementations where double is wider
than 64 bits?


VAX H-format floating point had/has 128 bits; I don't recall
how they're divided up between exponent and fraction. Also,
when I was working with VAXen the C implementations were not
able to use H-format. (For the curious: `float' used the
32-bit F-format, and `double' used either D-format or G-format,
both 64 bits, at your option -- and if you accidentally mixed
G's and D's ... "The horror! The horror!")

--
Er*********@sun .com
Nov 14 '05 #8
Ashutosh Iddya wrote:

thanks for that. I will try it out and let you know how it went


Kindly DO NOT toppost. See sig.

--
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?
Nov 14 '05 #9
"Ashutosh Iddya" <as************ @news.edu.au> writes:
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.


Using double is probably the wrong solution. If you repeatedly add 1
to a double variable, you'll never get an overflow, just a loss of
precision; eventually, (d + 1.0 == d).

The type "long long" is guaranteed to be at least 64 bits, which
should be more than enough for whatever it is you're counting. This
type is new in C99, but many C90 compilers support it as an extension.
(Since the C90 standard doesn't specify "long long", it's
theoretically possible that a C90 compiler could provide a "long long"
type that's smaller than 64 bits, but I don't think anyone has ever
done that.)

If 32 bits is enough, use long (or unsigned long).

If 32 bits isn't enough, and you don't care about portability to
systems that don't support long long, use long long (or
unsigned long long).

Be aware that a C90 implementation that supports long long as an
extension may not necessarily support everything that goes with it,
particularly the printf formats.

If you can't use long long, you might try using a pair of unsigned
longs (untested code follows):

unsigned long count_hi = 0;
unsigned long count_lo = 0;
...
for (...) {
count_lo ++;
if (count_lo == 0) count_hi ++;
}

Note the use of unsigned types. Overflow is well-defined for unsigned
types; it wraps around. Overflow for signed types causes undefined
behavior; wraparound is common, but not guaranteed.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4342
by: John Black | last post by:
Hi, If I have many integer calculation in my code, what's the best way to detect integer overflow? unsigned int i1 = 0xFFFFFF00, i2 = 0xFFFF; then in statement unsigned int i3 = i1 + i2; there is overflow and the result is not what I want. If such sum calculation scatters around my code, I wonder what's the best way to catch it?
2
648
by: REH | last post by:
If the is_modulo field of the numeric_limits class is true for signed integer types, can I assume that overflow for such types is defined behavior? If so, is the behavior the same regardless of implementation? Also, if the range of an integer type is not symmetrical around zero (i.e., 2's comp.), is it safe to assume that the extra value(s) is one the negative side? Thanks,
8
377
by: Ashutosh Iddya | last post by:
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.
25
6263
by: junky_fellow | last post by:
Is there any way by which the overflow during addition of two integers may be detected ? eg. suppose we have three unsigned integers, a ,b, c. we are doing a check like if ((a +b) > c) do something;
9
8635
by: Chris Botha | last post by:
Hi, I have an UInt32 and want to stick the value into an Integer and get an Overflow exception, BUT using C# the same value can be casted into an int and the value is as expected. The Hex value is FFFFFFDB, which should be -37. Thanks.
40
2810
by: Robert Seacord | last post by:
The CERT/CC has released a beta version of a secure integer library for the C Programming Language. The library is available for download from the CERT/CC Secure Coding Initiative web page at: http://www.cert.org/secure-coding/ The purpose of this library is to provide a collection of utility functions that can assist software developers in writing C programs that are free from common integer problems such as integer overflow, integer...
13
3225
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I didn't found it yet. help please thank you Freaker85
42
7034
by: thomas.mertes | last post by:
Is it possible to use some C or compiler extension to catch integer overflow? The situation is as follows: I use C as target language for compiled Seed7 programs. For integer computions the C type 'long' is used. That way native C speed can be reached. Now I want to experiment with raising a Seed7 exception (which is emulated with setjmp(), longjmp() in C) for integer
6
5000
by: Chris Becke | last post by:
I *know* my CPU has opcodes that can do this - when adding (or subtracting) there is a carry flag that can be invoked to make the result essentially 1 bit longer than the data size used in calculations. When multiplying two numbers, the CPU automatically returns a double width result. c & c++ give programmers these bloody ridiculous integer types of undetermined size and no really optimal way to even build a variable width number library...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10050
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9999
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.