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
25 2724
On Fri, 16 Apr 2004, Grumble wrote: 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.
I'd doubt the OP was using a C99 compiler. It is possible but my first
assumption would be a C89 compiler.
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.
Man, slow day and my brain shuts down. Just a month ago I proved that
overflow on a cycle count profiler for a 1 GHz processor would take over
500 years to occur. I should have realized this. 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
I was thinking more of situations when (long long) would be 32 bit. On
older compilers I remember seeing support for (long long) such that
sizeof(long) == sizeof(long long). Essentially they made it so source with
(long long) would not be considered a syntax error but still only
supported 32 bit integers.
For my information, are there implementations where double is wider than 64 bits?
Not that I have seen.
--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@ whitehouse.gov
"Grumble" <in*****@kma.eu .org> a écrit dans le message de
news:c5******** **@news-rocq.inria.fr.. . Darrell Grainger wrote:
For my information, are there implementations where double is wider than 64 bits?
There is the standard long double for that.
In lcc-win32 long double is 80 bits.
Then, you have the qfloat with 350 bits if you
really want big precision.
After that, the bignums is the only way, or
(much better) to look at the algorithm and
see why it is overflowing :-) http://www.cs.virginia.edu/~lcc-win32
Grumble <in*****@kma.eu .org> writes:
[...] For my information, are there implementations where double is wider than 64 bits?
The standard allows it, but I doubt that any implementations actually
do so. If you have a floating-point type wider than 64 bits, you're
more likely to call it "long double" (which has been valid at least
since the C89/C90 standard).
--
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" da*****@NOMORES PAMcs.utoronto. ca.com (Darrell Grainger) writes:
[...] I was thinking more of situations when (long long) would be 32 bit. On older compilers I remember seeing support for (long long) such that sizeof(long) == sizeof(long long). Essentially they made it so source with (long long) would not be considered a syntax error but still only supported 32 bit integers.
Ick!
Can you remember which compiler did this? Did it really implement
"long long" as a distinct type, or did it just allow the "long"
keyword to be repeated with no further effect (so "long", "long long",
and "long long long" would all be equivalent)?
--
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"
"Ashutosh Iddya" <as************ @news.edu.au> wrote in message
news:40******** *************** @freenews.iinet .net.au... 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.
unsigned long counter[2] = { 0 };
for (;;)
{
if (++counter[0] && ++counter[1])
puts("64+ bit counter overflowed!");
}
This is trivially extendable to as much precision as you want. But if you need a counter
bigger than a minimum of 64-bits, then I'd love to know what machine you're using and
where I can pick one up!
--
Peter
On Sat, 17 Apr 2004, Peter Nilsson wrote: "Ashutosh Iddya" <as************ @news.edu.au> 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. unsigned long counter[2] = { 0 };
for (;;) { if (++counter[0] && ++counter[1])
ITYM if (!(++counter[0] || ++counter[1]))
You want to make sure they both *don't* go back to zero at once.
puts("64+ bit counter overflowed!"); }
-Arthur
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote in message
news:Pi******** *************** ************@un ix48.andrew.cmu .edu... On Sat, 17 Apr 2004, Peter Nilsson wrote: "Ashutosh Iddya" <as************ @news.edu.au> 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. unsigned long counter[2] = { 0 };
for (;;) { if (++counter[0] && ++counter[1])
ITYM if (!(++counter[0] || ++counter[1]))
Or...
if (++counter[0] == 0 && ++count[1] == 0) You want to make sure they both *don't* go back to zero at once.
Yup, my bad. Thanks.
--
Peter
In <Pi************ *************** ***@drj.pf> da*****@NOMORES PAMcs.utoronto. ca.com (Darrell Grainger) writes: I was thinking more of situations when (long long) would be 32 bit.
No such thing. With 32-bit int and long, no point in adding a 32-bit
long long.
On older compilers I remember seeing support for (long long) such that sizeof(long) == sizeof(long long).
True, but those were compilers with 64-bit long's (e.g. the Digital Unix
compilers as well as gcc on any 64-bit Linux flavour).
Essentially they made it so source with (long long) would not be considered a syntax error but still only supported 32 bit integers.
A brain dead idea: the code will compile but silently generate wrong
results, because people using long long *really* expect more than 32 bits
(otherwise they would be using long in the first place).
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
In <40************ ***@sun.com> Eric Sosman <Er*********@su n.com> writes: 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.
So, you're providing a non-example. long double would have been the
right type for the VAX H_FLOAT type, but I don't know if any VAX C
compiler actually supported it. VAX FORTRAN did support it as REAL*16.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
In <ln************ @nuthaus.mib.or g> Keith Thompson <ks***@mib.or g> writes: "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).
No loss of precision until d + 1.0 == d, and the point where this happens
can be detected. On typical implementation, this should be around 2 ** 53
which should be enough for most applications needing to count something.
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.
Not that many out of the Unix world. They typically support a 64-bit
integer type, but give it a name in the implementation name space. Even
on 64-bit hardware...
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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?
|
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,
|
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.
|
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;
|
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.
| |
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...
|
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
|
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
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |