本文整理汇总了C#中System.Numerics.Complex结构体的典型用法代码示例。如果您正苦于以下问题:C# Complex结构体的具体用法?C# Complex怎么用?C# Complex使用的例子?那么恭喜您, 这里精选的结构体代码示例或许可以为您提供帮助。
Complex结构体属于System.Numerics命名空间,在下文中一共展示了Complex结构体的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Numerics;
public class Example
{
public static void Main()
{
// Create a complex number by calling its class constructor.
Complex c1 = new Complex(12, 6);
Console.WriteLine(c1);
// Assign a Double to a complex number.
Complex c2 = 3.14;
Console.WriteLine(c2);
// Cast a Decimal to a complex number.
Complex c3 = (Complex) 12.3m;
Console.WriteLine(c3);
// Assign the return value of a method to a Complex variable.
Complex c4 = Complex.Pow(Complex.One, -1);
Console.WriteLine(c4);
// Assign the value returned by an operator to a Complex variable.
Complex c5 = Complex.One + Complex.One;
Console.WriteLine(c5);
// Instantiate a complex number from its polar coordinates.
Complex c6 = Complex.FromPolarCoordinates(10, .524);
Console.WriteLine(c6);
}
}
输出:
(12, 6) (3.14, 0) (12.3, 0) (1, 0) (2, 0) (8.65824721882145, 5.00347430269914)
示例2: Complex
Complex value = new Complex(Double.MinValue/2, Double.MinValue/2);
Complex value2 = Complex.Exp(Complex.Log(value));
Console.WriteLine("{0} \n{1} \nEqual: {2}", value, value2,
value == value2);
输出:
(-8.98846567431158E+307, -8.98846567431158E+307) (-8.98846567431161E+307, -8.98846567431161E+307) Equal: False
示例3: Main
//引入命名空间
using System;
using System.Numerics;
public class Example
{
public static void Main()
{
Complex c1 = new Complex(Double.MaxValue / 2, Double.MaxValue /2);
Complex c2 = c1 / Complex.Zero;
Console.WriteLine(c2.ToString());
c2 = c2 * new Complex(1.5, 1.5);
Console.WriteLine(c2.ToString());
Console.WriteLine();
Complex c3 = c1 * new Complex(2.5, 3.5);
Console.WriteLine(c3.ToString());
c3 = c3 + new Complex(Double.MinValue / 2, Double.MaxValue / 2);
Console.WriteLine(c3);
}
}
输出:
(NaN, NaN) (NaN, NaN) (NaN, Infinity) (NaN, Infinity)
示例4: GetFormat
//引入命名空间
using System;
using System.Numerics;
public class ComplexFormatter :IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}
public string Format(string format, object arg,
IFormatProvider provider)
{
if (arg is Complex)
{
Complex c1 = (Complex) arg;
// Check if the format string has a precision specifier.
int precision;
string fmtString = String.Empty;
if (format.Length > 1) {
try {
precision = Int32.Parse(format.Substring(1));
}
catch (FormatException) {
precision = 0;
}
fmtString = "N" + precision.ToString();
}
if (format.Substring(0, 1).Equals("I", StringComparison.OrdinalIgnoreCase))
return c1.Real.ToString(fmtString) + " + " + c1.Imaginary.ToString(fmtString) + "i";
else if (format.Substring(0, 1).Equals("J", StringComparison.OrdinalIgnoreCase))
return c1.Real.ToString(fmtString) + " + " + c1.Imaginary.ToString(fmtString) + "j";
else
return c1.ToString(format, provider);
}
else
{
if (arg is IFormattable)
return ((IFormattable) arg).ToString(format, provider);
else if (arg != null)
return arg.ToString();
else
return String.Empty;
}
}
}
示例5: Main
public class Example
{
public static void Main()
{
Complex c1 = new Complex(12.1, 15.4);
Console.WriteLine("Formatting with ToString(): " +
c1.ToString());
Console.WriteLine("Formatting with ToString(format): " +
c1.ToString("N2"));
Console.WriteLine("Custom formatting with I0: " +
String.Format(new ComplexFormatter(), "{0:I0}", c1));
Console.WriteLine("Custom formatting with J3: " +
String.Format(new ComplexFormatter(), "{0:J3}", c1));
}
}
输出:
Formatting with ToString(): (12.1, 15.4) Formatting with ToString(format): (12.10, 15.40) Custom formatting with I0: 12 + 15i Custom formatting with J3: 12.100 + 15.400j