I am building a unit convertor program that uses the MathFP library.
Typically unit conversion occurs in the formula of:
U1 (unit1) * K (constant) = U2 (unit2)
I want to be able to detect when the an int has over/underflowed?
How can I detect when this has occured and gracefully handle the problem. Ideally I would be looking for a generic solution, as I would want to handle overflow with primitives of type long:
The only idea I have is:
int largeOne = bigNum;
int largeTwo = anotherbigNum;
//complete math operation
long l = largeOne * largeTwo;
if(l > Integer.MAX_SIZE){
System.out.println("Overflow");
//handle error
}
Should I be using a different primitive data type for these conversions, such as double?
Thanks in advance for your help
You could check if the result is negative, or positive (depending on largeOne and largeTwo signs) but it would be a guess work.I found another way, you could manually multiply your two numbers with an algorithm.
Resources :
On the same topic :