Reputation: 45
I am creating a C# project which takes two user inputs of complex numbers and completes a mathematical operation on them. The problem I am running into is how to parse i as the square root of -1.
double operandA = 0;
double operandB = 0;
double result = 0;
string textA, textB;
string error = "The value you entered is invalid, try again";
private void plus_Btn_Click(object sender, EventArgs e)
{
result = operandA + operandB;
}
private void Subtract_btn_Click(object sender, EventArgs e)
{
result = operandA - operandB;
}
private void mult_Btn_Click(object sender, EventArgs e)
{
result = operandA * operandB;
}
private void divide_Btn_Click(object sender, EventArgs e)
{
result = operandA / operandB;
}
private void textBox2_Leave(object sender, EventArgs e)
{
textB = textBox2.Text;
if (double.TryParse(textB, out operandB))
operandB = double.Parse(textB);
else
{
MessageBox.Show(error);
textBox2.Select();
}
}
private void textBox1_Leave(object sender, EventArgs e)
{
textA = textBox1.Text;
if (double.TryParse(textA, out operandA))
operandA = double.Parse(textA);
else
{
MessageBox.Show(error);
textBox1.Select();
}
}
It works fine on regular numbers, decimals, and negative numbers, but I can't figure out how to do the value of i that I need. Can anyone help? It has been suggested that I use System.Numeric.Complex, but whenever I try to do this using "Using System.Numerics.Complex" or simply "Using System.Numerics", it says this type/namespace does not exist inside 'System'.
Upvotes: 1
Views: 1376
Reputation: 249536
If you want to support operations on complex numbers, you should consider using System.Numerics.Complex
Complex c = Complex.Sqrt(-1);
Console.WriteLine(c + 1);
For documentation on this type see here
Upvotes: 7
Reputation: 382
You can use Complex in System.Numerics
https://msdn.microsoft.com/en-us/library/system.numerics.complex(v=vs.110).aspx
Upvotes: 2