kabilan Mohanasundaram
kabilan Mohanasundaram

Reputation: 399

Issue in Adding large numbers in C# with int datatype

I am trying to add five large numbers in C# with int dataType. But the result was not correct. why .net is forcing me cast to long and perform the add to get the correct result.

using System;       
public class Program
{
    public static void Main()
    {
        int i1= 256741038,i2=  623958417,i3=  467905213 ,i4= 714532089,i5=938071625;
        long l1= 256741038,l2=  623958417,l3=  467905213 ,l4= 714532089,l5=938071625; 
        long result1=i2+i3+i4+i5;
        long result2=l2+l3+l4+l5;
        long result3=(long)l2+(long)l3+(long)l4+(long)l5;
        Console.WriteLine(result1);
        Console.WriteLine(result2);
        Console.WriteLine(result3);
        }
}

Upvotes: 1

Views: 1611

Answers (4)

Tearth
Tearth

Reputation: 306

You can use checked keyword to see that long result1=i2+i3+i4+i5; is generating System.OverflowException: 'Arithmetic operation resulted in an overflow.', as other answers say. Valid result of your operation is 2,744,467,344, but max of the int (which is just Int32) is 2,147,483,647. Long type (aliasa for Int64) can contain up to 9,223,372,036,854,775,807 which is enough to add your numbers without overflow.

public static void Main()
{
    checked
    {
        int i1 = 256741038, i2 = 623958417, i3 = 467905213, i4 = 714532089, i5 = 938071625;
        long l1 = 256741038, l2 = 623958417, l3 = 467905213, l4 = 714532089, l5 = 938071625;
        long result1 = i2 + i3 + i4 + i5;
        long result2 = l2 + l3 + l4 + l5;
        long result3 = (long)l2 + (long)l3 + (long)l4 + (long)l5;
        Console.WriteLine(result1);
        Console.WriteLine(result2);
        Console.WriteLine(result3);
        Console.Read();
    }
}

Upvotes: 1

Luaan
Luaan

Reputation: 63732

With a few exceptions, type resolution in C# doesn't care about the type you're assigning to. All the compiler is considering in your case is

i2 + i3 + i4 + i5

Since each of the operands is of type int, the result is also of type int (or, more accurately, it uses the addition operator that takes two ints as arguments and returns int), even though you later assign it to a long local.

By casting one of the operands to long, the int, int -> int operator no longer fits, and the long, long -> long is chosen. The same is true of float, for example - as soon as one of the operands is float, the result is float - otherwise, a / b gives you integer division (operator int, int -> int).

In general, you need to make sure the data type you're working with is big enough to hold the result and any intermediate values. If you expect a result that doesn't fit into an int, don't use int. By default, C# doesn't check for overflows - you can override it like this:

var result = checked(i2 + i3 + i4 + i5);

This way, instead of silently wrapping around to the wrong int value, you get an exception.

Upvotes: 2

canton7
canton7

Reputation: 42235

int has a maximum value of 2,147,483,647. If you try and make an int that's larger than this (for example, by adding two smaller ints), it will wrap around to being a very large negative number. This is what is happening in result1.

long is much bigger, and has a maximum value of 9,223,372,036,854,775,807. This is large enough to hold the values that you're trying to represent.

Upvotes: 0

TomTom
TomTom

Reputation: 62093

Because of performance C# (not dotnet) and ANY other langauge that I know use the LARGEST common denominator to do operations. This is COMMON, between them. not the result, which would have to be guessed.

SO, adding 2 int numbers is an int operation that may overflow. Adding an int to long is a long operation. Adding two long is a long operation.

Simple like that. If you know you will overflow, then declare one of the types as longer.

Now, you CAN cast - you COULD also just declare them as long, you know.

C# short/long/int literal format?

shows you all the suffices.

  • 1111 is an int
  • 1111UL is an unsigned long.

So, there is no need to cast if you tell the compiler the correct data type to use in the literal.

Upvotes: 2

Related Questions