# What is wrong in this math equation in C#?

I have a math equation and I want use it in my program but I got an error. The result from this equation is non number .. the math equation i:

note: n is the number in the textbox

``````((298/2)^2*ACOS(((298/2)-n)/(298/2))-((298/2)-n)*((2*(298/2)*n-n^2))^(0.5))*1213/1000
``````

in button code

``````double a = Math.Pow((298.00 / 2), 2);
double b = (Math.Acos(((298.00 / 2) - Convert.ToDouble(textBox1.Text)) / (298.00 / 2)));
double c = ((298.00 / 2) - Convert.ToDouble(textBox1.Text));
double d = (2 * (298.00 / 2) * Convert.ToDouble(textBox1.Text));
double f = Math.Pow(Convert.ToDouble(textBox1.Text), 2);
double r = ((a * b) - (c * d) )- f;
double result = Math.Pow(r, (0.5));
double h = result * 1213.00 / 1000;
textBox1.Text = Convert.ToString(h );
}
``````

If anyone knows what is wrong here, tell me please. The error is in variable r because the result from r is less that 0!

Lets rewrite given math expression in more convenient way:

``````( (298/2)^2 * ACOS(((298/2)-n)/(298/2)) - ((298/2)-n) * ( (2*(298/2)*n - n^2 ) ) ^ (0.5) ) * 1213/1000
---------   -------------------------   -----------     ------------   ---
a                  b                     c               d          f
--------------------------------
r = (d - f) ^ 0.5
------------------------------------------------------------------------------------------------------
(a * b - c * r) * 1213/1000
``````

Now it is easier to see that you have an error in formula:

``````double r = ((a * b) - (c * d) )- f;
``````

`r` must be calculated using the next formula:

``````double r = Math.Sqrt(d - f); // It is better to use Math.Sqrt(x) instead of Math.Pow(x, 0.5).
``````

Now knowing where the error is we can fix it:

``````double n = Convert.ToDouble(textBox1.Text);

double a = Math.Pow(298.00 / 2, 2);
double b = Math.Acos( (298.00 / 2 - n) / (298.00 / 2) );
double c = 298.00 / 2 - n;
double d = 2 * 298.00 / 2 * n;
double f = Math.Pow(n, 2);
double r = Math.Sqrt(d - f);
double result = a * b - c * r;
double h = result * 1213.00 / 1000;

textBox1.Text = Convert.ToString(h);
``````

But for values of `n > 298` you will still get `r = NaN` because for such values of `n` expression `d - f` is negative.