tags:

views:

63

answers:

0

Possible Duplicate:
How can I check if multiplying two numbers in Java will cause an overflow?

Some programming puzzle, as you all may know, asks its member to compute a very big number.

For example :

long a = 10000000000L;
long b = 10000000000L;
long c = a * b;
System.out.println(c);

Code above prints incorrect output, which is 7766279631452241920. This kind of problem drove me crazy twice. Since i am really sure that my algorithm is correct (i tested it in smaller value) but at bigger value prints incorrect output.

Using BigInteger, in my opinion, is the last resort since i believe BigInteger performs slower than native data type such as int or long.

What's the best way to check/prevent whether an arithmetic overflow happens or not? Please don't tell me to use BigInteger. I already know that