Reputation: 398
when multiplying a set of int's and converting the result to a long I get a different answer as opposed to when I multiply a set of doubles and convert the result to a long. eg:
int a = 5;
int b = 5;
int c = 7;
int d = 6;
int ee = 6;
int f = 8;
int g = 9;
int h = 6;
int i = 6;
int j = 4;
int k = 8;
int l = 9;
int m = 5;
long x = a * b * c * d * ee * f * g * h * i * j * k * l * m;
double aa = 5;
double ab = 5;
double ac = 7;
double ad = 6;
double aee = 6;
double af = 8;
double ag = 9;
double ah = 6;
double ai = 6;
double aj = 4;
double ak = 8;
double al = 9;
double am = 5;
long y = (long)(aa * ab * ac * ad * aee * af * ag * ah * ai * aj * ak * al * am);
Can anyone tell me why this happens? Thank you
Upvotes: 5
Views: 1154
Reputation: 626
I give it a brief search and run some tests even on scripts as well and I found the following outcomes, in script format I test it as,
<script>
var a = 5;
var b = 5;
var c = 7;
var d = 6;
var ee = 6;
var f = 8;
var g = 9;
var h = 6;
var i = 6;
var j = 4;
var k = 8;
var l = 9;
var m = 5;
var x = a * b * c * d * ee * f * g * h * i * j * k * l * m;
var y = eval(a * b * c * d * ee * f * g * h * i * j * k * l * m);
document.writeln("RESULT X : " + x);
document.write("<br>");
document.writeln("RESULT Y : " + y);
</script>
and the outcome was 23514624000
for both x
and y
, but in c# as you try to multiply all integers and in result an integer is formed but we also know that int.MAX = 2147483647
which is less then thw actuall result, So in result when assigning to long x= a * b * c * d * ee * f * g * h * i * j * k * l * m;
it truncates the original values, and in other result it is not, if you want same result in both you can use following code,
int a = 5;
int b = 5;
int c = 7;
int d = 6;
int ee = 6;
int f = 8;
int g = 9;
int h = 6;
int i = 6;
int j = 4;
int k = 8;
int l = 9;
int m = 5;
long x = (long)a * b * c * d * ee * f * g * h * i * j * k * l * m;
double aa = 5;
double ab = 5;
double ac = 7;
double ad = 6;
double aee = 6;
double af = 8;
double ag = 9;
double ah = 6;
double ai = 6;
double aj = 4;
double ak = 8;
double al = 9;
double am = 5;
long y = (long)(aa * ab * ac * ad * aee * af * ag * ah * ai * aj * ak * al * am);
Console.WriteLine(x);
Console.WriteLine(y);
Console.ReadKey();
Now you will get the same result i.e 23514624000
Upvotes: 2
Reputation: 823
That's probably because the result of your multiplication is too big to hold in an Int32 - yet it can be held in a double.
To be a bit more specific look at x and y in hexadecimal:
x = 0x000000007994b000
y = 0x000000057994b000
An Int32
can only hold the lower 8 hex values. This is why x
is the wrong number. The lowest 8 hex-values are correct, but the top is cut off.
Upvotes: 5
Reputation: 186668
You've got an integer overflow, since whenever you multiply two int
you have int
:
this is interpreted as Int32
so far
a * b * c * d * ee * f * g * h * i * j * k * l * m
and only than converted into Int64
(long
). To correct the implementation, declare
all the variables initially being long
e.g.
long a = 5;
long b = 5; // actually, not necessary, since a * b will be long, but safer
long c = 7;
...
long m = 5;
long x = a * b * c * d * ee * f * g * h * i * j * k * l * m;
Upvotes: 13