Just wondering, is it possible to compute the square roots of negative numbers in C#? For example sqrt(-1) = i.
I wrote this piece of code:
using System;
public static class sqrts
{
public static void Main()
{
string x;
double sqrtofx;
Console.Write("Enter a number: ");
x = Console.ReadLine();
sqrtofx = Math.Sqrt(Convert.ToInt32(x));
Console.WriteLine("\nSqrt({0}): {1}", x, sqrtofx);
Console.ReadKey();
}
}
If I enter 25, it gives 5. However, if I put in -5, it gives NaN instead of 5i.