NMath是一個適用於全部.NET語言,如C#、Visual Basic、F#和.NET的數學庫,它包含了.NET平臺上的面向對象數字計算的基礎類。咱們將以連載的形式向你們介紹NMath的實用教程,有任何建議或提示請在下方評論區留言,以便學習交流。函數
本節將介紹如何構造FloatComplex和DoubleComplex的實例:學習
您能夠從表示實部和虛部的一對數值中構造複數對象。若是僅傳遞單個值則假定它是實部,而且虛部設置爲0.0。例如:spa
var c = new FloatComplex( 1.3, 4.5 ); // 1.3 + 4.5i var c2 = new DoubleComplex( 6.5 ); // 6.5 + 0.0i
Dim C As New FloatComplex(1.3, 4.5) ' 1.3 + 4.5i Dim C2 As New DoubleComplex(6.5) ' 6.5 + 0.0i
靜態FromPolar()函數構造具備給定幅度和相位角的複數:對象
var c = DoubleComplex.FromPolar( 2 * Math.Sqrt(2), Math.PI/4 ); // c = 2.0 + 2.0i
Dim C As DoubleComplex = DoubleComplex.FromPolar(2 * Math.Sqrt(2), Math.PI / 4) ' c = 2.0 + 2.0i
您還能夠從表單的字符串表示(real,imag)構造複雜的數字類型。括號是可選的,忽略空格。一樣,若是隻提供一個值,則假定它是真實的部分。例如4.2,-5.1是有效的字符串,而4.2-5.1i不是有效的字符串。blog
string s = "(1.1, -3.23)"; var c = new DoubleComplex( s );
Dim S As String = "(1.1, -3.23)" Dim C As New DoubleComplex(S)
靜態Parse()方法執行相同的功能:教程
string s = "(1.1, -3.23)"; DoubleComplex c = DoubleComplex.Parse( s );
Dim S As String = "(1.1, -3.23)" Dim C As DoubleComplex = DoubleComplex.Parse(s)
注意:在解析複數字符串時,不能像在某些財務格式中那樣使用括號來表示負數。字符串
相反的,重寫的ToString()成員函數返回複數的字符串表示形式:get
var c = new FloatComplex( 7.61, -1.2 ); Console.WriteLine( c.ToString() ); // prints "(7.61,-1.2)"
Dim C As New FloatComplex(7.61, -1.2) Console.WriteLine(c.ToString()) ' prints "(7.61,-1.2)"
ToString() 方法的變體也接受標準的.NET數字格式字符串。例如格式字符串「E」表示指數(科學)概念。數學
複數類的隱式轉換運算符以下圖所示,箭頭表示隱式提高。string
更多NMath實用教程敬請關注!