Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why this would result in long integer overflow

Tags:

c#

integer

I checked the document that long= int64 has range more than 900,000,000,000,000

Here is my code:

int r = 99;
long test1 = r*r*r*r*r;

at runtime it gives me 919,965,907 instead of the correct 9,509,900,499.

another test

long test2 = 99*99*99*99*99;

It refuses to compile, saying integer overflow.

But if i do this

long test3 = 10100200300;

This works fine.

like image 881
colinfang Avatar asked Oct 14 '11 11:10

colinfang


People also ask

What causes an integer overflow?

An integer overflow occurs when you attempt to store inside an integer variable a value that is larger than the maximum value the variable can hold. The C standard defines this situation as undefined behavior (meaning that anything might happen).

How could an integer overflow error occur in your program?

How could an integer overflow error occur in your program? If mathematical operations increase integer values above maximum allowed value or decrease them below minimum allowed values, an integer overflow error occurs.

How is overflow caused?

Overflow occurs when the magnitude of a number exceeds the range allowed by the size of the bit field. The sum of two identically-signed numbers may very well exceed the range of the bit field of those two numbers, and so in this case overflow is a possibility.

What are possible consequences of an integer overflow?

An integer overflow can cause the value to wrap and become negative, which violates the program's assumption and may lead to unexpected behavior (for example, 8-bit integer addition of 127 + 1 results in −128, a two's complement of 128).


2 Answers

The problem is that the literal "99" is being treated as an int. If you add "L" it will treat it as a long. To fix your compilation problem:

long test2 = 99L * 99L * 99L * 99L * 99L;

And to fix the "incorrect result" caused by integer overflow:

long r = 99;
long test1 = r * r * r * r * r;

The key point is that the expression to the right of the "=" is evaluated before the assignment to long r is done.

There are other literal suffixes you might be interested in:

Type    Suffix    Example
uint    U or u    100U
long    L or l    100L
ulong   UL or ul  100UL
float   F or f    123.45F
decimal M or m    123.45M

@m.edmonson, regarding your question about why it comes out to 919965907. What's happening, is that the value is "wrapping" around int.MaxValue. You can see this with a little test program:

int i = 99; // 99
i *= 99;    // 9801
i *= 99;    // 970299
i *= 99;    // 96059601
i *= 99;    // 919965907        should be 9509900499 but comes out to 919965907
            //                      which is (9509900499 % int.MaxValue)

long k = 9509900499 % int.MaxValue;

What is meant by "wrapping around"? When you exceed int.MaxValue by 1, the value "goes back" to int.MinValue.

int j = int.MaxValue;
j++;

bool isNowMinValue = (j == int.MinValue);   // true, the value has "wrapped around"

This is a bit simplistic; if you search for "integer overflow" you will get a better explanation. It's worth understanding how integers (and other numeric types) are represented with 32 bits:

http://en.wikipedia.org/wiki/Signed_number_representations

like image 71
JohnD Avatar answered Sep 27 '22 16:09

JohnD


It's using integer multiplication :

long r = 99;
long test1 = r*r*r*r*r;
like image 38
Tim Rodham Avatar answered Sep 27 '22 17:09

Tim Rodham