In this case, sum is an int and I am intentionally setting one[] and [two] to int.MaxValue.
The result for sum is 1.
How can I tell if the result of a math function is going to overflow here, prior to attempting it?
sum += one[i] * two[i];
In this case, sum is an int and I am intentionally setting one[] and [two] to int.MaxValue.
The result for sum is 1.
How can I tell if the result of a math function is going to overflow here, prior to attempting it?
sum += one[i] * two[i];
By 'overflow', did you mean it will exceed int.MaxValue?
If so, try to use checked block.
As @AlexD mentioned in comments, it won't perform a prior-check; however, it will raise an exception in case of overflow.
Quote and code sample are taken from MSDN:
The checked keyword is used to explicitly enable overflow checking for integral-type arithmetic operations and conversions.
// If the previous sum is attempted in a checked environment, an
// OverflowException error is raised.
// Checked expression.
Console.WriteLine(checked(2147483647 + ten));
// Checked block.
checked
{
int i3 = 2147483647 + ten;
Console.WriteLine(i3);
}