-3

I am trying to calculate the delta of a number using a formula of bhaskara and delta but it is giving NaN in the message box

//Variables declaration
int a = 800, b = 500, c = 350;
double delta, a1, a2;

//Formulas
delta = (b*b) - (4*a*c);
a1 = (-b + Math.Sqrt(delta)) / (2 * a);
a2 = (-b - Math.Sqrt(delta)) / (2 * a);

//Output
MessageBox.Show(a1.ToString());
MessageBox.Show(a2.ToString());

enter image description here

ilg
  • 21
  • 3
  • 2
    Because it can't take square root of negative number. BTW, regarding the title, NaN = "Not a Number". – Eugene Sh. Jul 02 '19 at 19:46
  • a negative will return NaN, from the docs https://learn.microsoft.com/en-us/dotnet/api/system.math.sqrt?view=netframework-4.8 – David Yenglin Jul 02 '19 at 19:48

2 Answers2

2

The value of delta is -870000.
That's not a value you can take the square root of (and get a real number anyway).

From the Math.Sqrt docs:

enter image description here

Broots Waymb
  • 4,713
  • 3
  • 28
  • 51
1

As Broots said Math.sqrt of negative number will return NaN. There is a Complex struct that should do what you need:

Complex c = Complex.Sqrt(-25); // has a value of approximately 0 + 5i
Shivam
  • 3,514
  • 2
  • 13
  • 27