2

So I have a code that adds two integers and prints the result:

Console.WriteLine("enter number: ");
int intTemp = Convert.ToInt32(Console.ReadLine());
long sum = intTemp + 5;
Console.WriteLine($"sum is : {sum}");

But if in the console I will put the maximum value for the int type, I won't get an exception, but the result is wrong, even if I am saving the result in a long variable. Here is the output:

enter number:
2147483647
sum is : -2147483644

But if the sum variable is a long, why I am getting the wrong result?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
  • 6
    Because `intTemp + 5` is still an `int`, and *then* it is assigned to a `long`. You can use `intTemp + 5L`. There should be a duplicate but I couldn't find it. – harold Mar 03 '20 at 13:03
  • The add operator doesn't know (nor should it) *where* you intend to store it's result, it's type or size, etc. – Damien_The_Unbeliever Mar 03 '20 at 13:04
  • If you want to see it crash and burn, try `long sum = checked(intTemp + 5);`. The result of adding two `int`s is still an `int`, even if you ultimately assign it to a `long`. – Jeroen Mostert Mar 03 '20 at 13:04
  • @Damien_The_Unbeliever so you're saying that everywhere I am adding two numbers, in order to be 100% sure that I'm getting the right result, I need to cast one to long (assuming that I save the result in a long variable)? – Buda Gavril Mar 03 '20 at 13:06
  • 1
    Or `long intTemp = long.Parse(Console.ReadLine());` – Dmitry Bychenko Mar 03 '20 at 13:08

3 Answers3

2

The result is not of type long. It is of type int and afterwards it is converted to a long in order to assign it to a variable of type long.

That is needed to do, it is the following:

long sum = (long)intTemp + 5;

or

long sum = intTemp + (long)5;

Doing either of the above, since the one operand is of type (long), after conversion, the other would be converted also to long, in order the two values to can be added and the result would be stored to the sum variable.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • so you're saying that everywhere I am adding two numbers, in order to be 100% sure that I'm getting the right result, I need to cast one to long (assuming that I save the result in a long variable)? – Buda Gavril Mar 03 '20 at 13:21
  • Essentially that I try to say is that when you expect the result of the addition of two int to be a number that cannot be represented by an int and it can be represented by a long, to convert one or another operand to long and an addition between two long to be performed instead the addition between two int. – Christos Mar 03 '20 at 14:17
1

You have to cast the int "intTemp" to a long before, because the sum only gets cast to a long after the calculation is complete

etaxi341
  • 96
  • 7
0

The key is like already mentioned that you need to convert one of the values to long to be able to retain the correct value as otherwise the result value is already corrupted before it is assigned to long. I would like to suggest that you can use MaxValue in these numeric types to make the calculation memory friendly if that is where you will use it for calculations. int takes 32 bits and long takes 64 bits. If the result of the calculation is still an int then you can save 32 bits of storage till you really need it. In your example you could do

if (int.MaxValue - 5) < intTemp ) // it means the value will go above int range if add 5
{
  // Make conversion to target type before the operation
}else{ 
  // the value will still be in int range
}

You can use the appropriate storage type for the result then. It can become quite memory efficient if you are storing large number of results and then using them for further calculations. Hope it helps.