I am working on a program and I am using a quadratic formula. This is the code that is executed when the button is clicked to solve the problem,
private void button1_Click(object sender, EventArgs e)
{
double aVal, bVal, cVal, xVal1, xVal2;
xVal1 = 0;
xVal2 = 0;
aVal = Convert.ToDouble(a.Text);
bVal = Convert.ToDouble(b.Text);
cVal = Convert.ToDouble(c.Text);
xVal1 = (bVal + Math.Sqrt(Math.Pow(bVal, 2) - (4 * aVal * cVal))) / 2 * aVal; //Positive Calculation
xVal2 = (bVal - Math.Sqrt(Math.Pow(bVal, 2) - (4 * aVal * cVal))) / 2 * aVal; //Negative Calculation
xPos.Text = Convert.ToString(xVal1);
xNeg.Text = Convert.ToString(xVal2);
}
After some debugging, I find that I narrowed down the problem to lines 9 and 10, where the actual math takes place. However, I'm not quite sure what happened. As you can see, numbers are doubled and initialized, so they are not zero or truncated. When I run the program and enter the values b and c, say 6, 4 and 3, the labels xPos and xNeg, which are responsible for outputting negative and positive values, simply display NaN. Should I use a shortcut for this kind of thing?
source
share