當前位置: 首頁>>代碼示例>>C#>>正文


C# Complex結構體代碼示例

本文整理匯總了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);
   }
}
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:33,代碼來源:Complex

輸出:

(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);
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:4,代碼來源:Complex

輸出:

(-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);
   }
}
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:22,代碼來源:Complex

輸出:

(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;
      }                        
   }
}
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:50,代碼來源:Complex

示例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));
   }
}
開發者ID:.NET開發者,項目名稱:System.Numerics,代碼行數:15,代碼來源:Complex

輸出:

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


注:本文中的System.Numerics.Complex結構體示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。