Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No overflow exception for int in C#?

Tags:

c#

overflow

I had this weird experience with problem number 10 on Project Euler (great site by the way). The assignment was to calculate the sum of all the prime numbers below two million.

I used an int for the sum, and my algorith produced an answer, but when i pasted it to verify the answer, it was wrong.

It turned out that the result was too big to fit in an int, but wouldn't this cause an overflow error or something? Instead, it just returned a value far off from the real answer.

When I changed the type to long, everything was hunky dory.

like image 597
erikric Avatar asked Jan 13 '10 12:01

erikric


People also ask

Does C have integer overflow?

In C programming language, a computation of unsigned integer values can never overflow, this means that UINT_MAX + 1 yields zero.

How do you prevent an overflow int?

You cannot prevent integer-overflow completely. If it happens, it happens. You need to be carefully at coding in the first place. But you can try to check before the assignment if an overflow can occur.

What is an overflow exception?

An OverflowException is thrown at run time under the following conditions: An arithmetic operation produces a result that is outside the range of the data type returned by the operation.

What happens when an unsigned int overflows?

"A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type."


2 Answers

C# integer operations don’t throw exceptions upon overflow by default. You can achieve that via the project settings, or by making the calculation checked:

int result = checked(largeInt + otherLargeInt); 

Now the operation will throw.

The opposite is unchecked, which makes any operation explicitly unchecked. Obviously, this only makes sense when you’ve got checked operations enabled in the project settings.

like image 113
Konrad Rudolph Avatar answered Sep 22 '22 05:09

Konrad Rudolph


In C# an OverflowException is not thrown (in VB the exception is thrown per default).

To get the excpetion you have to embed your code in a checked context:

byte value = 241; checked {     try      {         sbyte newValue = (sbyte) value;         Console.WriteLine("Converted the {0} value {1} to the {2} value {3}.",              value.GetType().Name, value,              newValue.GetType().Name, newValue);     }     catch (OverflowException)      {         Console.WriteLine("Exception: {0} > {1}.", value, SByte.MaxValue);     } }        

MSDN explains in more detail:

For the arithmetic, casting, or conversion operation to throw an OverflowException, the operation must occur in a checked context. By default, arithmetic operations and overflows in Visual Basic are checked; in C#, they are not. If the operation occurs in an unchecked context, the result is truncated by discarding any high-order bits that do not fit into the destination type.

like image 44
Dirk Vollmar Avatar answered Sep 22 '22 05:09

Dirk Vollmar