Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > integer overflow

Reply
Thread Tools

integer overflow

 
 
Ashutosh Iddya
Guest
Posts: n/a
 
      04-16-2004
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





 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      04-16-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 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?

 
Reply With Quote
 
 
 
 
Ashutosh Iddya
Guest
Posts: n/a
 
      04-16-2004
thanks for that. I will try it out and let you know how it went
Ashutosh
"CBFalconer" <> wrote in message
news:...
> 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?
>



 
Reply With Quote
 
Darrell Grainger
Guest
Posts: n/a
 
      04-16-2004
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
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      04-16-2004
In <407fba3a$0$16582$ > "Ashutosh Iddya" <> 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:
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      04-16-2004
In <> (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:
 
Reply With Quote
 
Grumble
Guest
Posts: n/a
 
      04-16-2004
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?

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-16-2004
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!")

--

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      04-16-2004
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?


 
Reply With Quote
 
Darrell Grainger
Guest
Posts: n/a
 
      04-16-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
integer or long overflow... deancoo C++ 11 03-05-2005 11:13 PM
Integer overflow in expression Vivek Mohan C Programming 7 05-06-2004 07:13 AM
hhow to detect overflow in integer calculation John Black C++ 1 04-15-2004 05:28 AM
unsigned integer overflow behaviour bartek C++ 3 02-06-2004 09:47 PM
Integer overflow Enrico 'Trippo' Porreca C Programming 9 08-24-2003 10:24 AM



Advertisments