2

I am trying to multiply two numbers in my application but in some case it result wrong value

var result = 0;
var firstNumber = 654165;
var secondNumber = 6541;
result = firstNumber * secondNumber;

it result -16074031 which is wrong

can you help me to found where is the error?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Ayman
  • 117
  • 11
  • 2
    The problem is your variable type `int`. You need to change it `int64` or `long` which is the same – DerApe Sep 18 '13 at 10:24
  • This is considering as `integer` this case, you need `long` instead `var`. Please look [Format for 'short', 'long', and 'int' literal in C#?](http://stackoverflow.com/questions/5820721/format-for-short-long-and-int-literal-in-c) – huMpty duMpty Sep 18 '13 at 10:24

6 Answers6

5

Try this:

checked
{
    var result = 0;
    var firstNumber = 654165;
    var secondNumber = 6541;
    result = firstNumber * secondNumber;
}

this way you can see the problem System.OverflowException. To solve simply use long

unchecked
{
    var result = 0L;
    var firstNumber = 654165L;
    var secondNumber = 6541L;
    result = firstNumber * secondNumber;
}

Take a look at checked and unchecked keywords.

Alessandro D'Andria
  • 8,663
  • 2
  • 36
  • 32
4

Your implicitly-typed variables are all being created as int, but the result of your calculation will only fit in a long, so you are seeing an overflow error.

If you simply declare result as a long instead, you will still see the wrong result, because the operation is performed on two ints and not converted to long until the assignment, by which time the overflow has already occurred.

So instead, you need to either declare one (or both) of your numbers as long, or cast one (or both!) to long during the calculation. You can also omit the redundant assigning of 0 to `result:

int firstNumber = 654165;
int secondNumber = 6541;
long result = (long)firstNumber * secondNumber;

or:

long firstNumber = 654165;
int secondNumber = 6541;
long result = firstNumber * secondNumber;
Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
1

You are getting an overflow.

Use long instead of var for the three declarations and it will gives you the right result.

long result = 0;
long firstNumber = 654165;
long secondNumber = 6541;
result = firstNumber * secondNumber;

Using var the compiler assign the type Int32 to your variables.

The result is 4,278,893,265 which is greater than Int32.MaxValue (which is 2,147,483,647)

Tobia Zambon
  • 7,479
  • 3
  • 37
  • 69
1

You have a overflow. You should be using a type larger than 32bit. (long for example, double for decimals)

RvdK
  • 19,580
  • 4
  • 64
  • 107
0

use long in spite of using var for all declarations , then you will get correct output

Deadlock
  • 330
  • 1
  • 3
  • 21
0

The result should be of type long

You can do this by changing to.

var result = 0L;
var firstNumber = 654165L;
var secondNumber = 6541L;
result = firstNumber * secondNumber;

The multiplication is creating a number bigger than an int and is overflowing.

tigerswithguitars
  • 2,497
  • 1
  • 31
  • 53