I thought this was going to be super easy but it has resulted in the need for more checks than i would like.
I have this code so far:
if (funcType == "Mul")
return (val1 * val2) <= int.MaxValue && (val1 * val2) >= int.MinValue;
if (funcType == "Add")
return (val1 + val2) <= int.MaxValue && (val1 + val2) >= int.MinValue;
if (funcType == "Sub")
return (val1 - val2) <= int.MaxValue && (val1 - val2) >= int.MinValue;
if (funcType == "Div")
return (val1 / val2) <= int.MaxValue && (val1 / val2) >= int.MinValue;
I need to check if when the calculations result in a number within the range of the int value type.
For example
int val1 = 1777777774;
int val2 = 477778777;
When added together the result is -2039410745 (a valid int)
But this is not the correct answer (so to speak) and so the check is not working as desired.
I suppose i could declare val1 and val2 as longs to prevent the negative results and then do the comparison.
Would this be safe (i’d still be worried about the result hitting limits when doing multiplication), is there a better way that you could think of?
Many thanks,
Kohan.
Convert one of the arguments to a
longfirst, then the entire calculation will use 64-bit integers.There’s no danger of overflow so long as your original arguments remain typed as
int:long.MaxValuehas more than twice the magnitude ofint.MaxValue * int.MaxValue, similarly withlong.MinValueandint.MinValue * int.MaxValue.