Reputation: 241
I searched and found topics relating to this issue but in all of them here the reason was overflow (the result was too big for an int type,right?). but in this case the result is less than the max value for int and still produces a negative number from 2 positives. and I wonder why.
heres the code:
int s = (50*49*48*47*46*45);
out.println(s);
OUTPUT:
-1443597888
now when i try 449 instead of 49:
int s = (50*449*48*47*46*45);
out.println(s);
OUTPUT:
1760488896
so why does that happen? I generally want to calculate (50*49*48*47*46*45)/(1*2*3*4*5*6) but it keeps giving negative result.
Thanks for the help.
Oh Yeah I am using Java, a very early beginner so I have little clue of whats going on. thanks.
Upvotes: 0
Views: 143
Reputation: 17576
As others have pointed out, the problem is that computing 50*49*48 ...
is too big to fit in a Java int
. You could switch to a bigger fixed-precision type, namely long
, but then you'll run into the problem for some other, larger, arguments. My advice is to use Java's BigInteger
type to store the result.
Upvotes: 0
Reputation: 1446
You didn't specified the language which you use, neither the number of bits for int representation. But is seems that it IS an overflow problem.
From your second example:
50*449*48*47*46*45
is 104839704000
which is binary:
01100001101000111011101110110111000000
but the result which you get is 1760488896
which is binary:
00000001101000111011101110110111000000
First bits (the 0110...
at the beginning) were cut off.
Seems that you have 32bit for int representation. Which is to small to represent 11441304000
- the result of 50*49*48*47*46*45
.
You get wrong result in both cases. Why it's negative only at first example?
The binary representation of 11441304000
is
010 10101001111101000111000111000000
But only 32 (bolded) bits fits into your integer variable. The first of them is 1
, which is translated to negative decimal value.
Upvotes: 2
Reputation: 3821
The "int" type of your programming language of choice probably uses 32-bit signed integer representation. That means that a fixed number of bits is used to represent a number, so the range of numbers that can be stored is limited. Most usually, the range is -2147483648 to 2147483647 (or written differently, -2^31 to 2^31-1), best consult documentation for your programming language for confirmation.
So, an int can only be in that limited range, but the actual result of your calculation (50*49*48*47*46*45) would be 11441304000, which is well outside that range, so one of the multiplications causes an overflow.
Different programming languages and platforms handle that in different ways, but one usual result is that the number simply wraps around, so that 2147483647+1 will result in -2147483648. Other programming languages will catch this and generate an error. Others again will automatically switch to a larger representation, so you don't get an overflow at all but can work with arbitrarily large numbers.
The language you are using appears to belong to the first group which silently overflows, so you will simply get a wrong result.
Upvotes: 2