本文整理匯總了VB.NET中System.Numerics.Complex結構體的典型用法代碼示例。如果您正苦於以下問題:VB.NET Complex結構體的具體用法?VB.NET Complex怎麽用?VB.NET Complex使用的例子?那麽, 這裏精選的結構體代碼示例或許可以為您提供幫助。
在下文中一共展示了Complex結構體的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Example
' 導入命名空間
Imports System.Numerics
Module Example
Public Sub Main()
' Create a complex number by calling its class constructor.
Dim c1 As New Complex(12, 6)
Console.WriteLine(c1)
' Assign a Double to a complex number.
Dim c2 As Complex = 3.14
Console.WriteLine(c2)
' Cast a Decimal to a complex number.
Dim c3 As Complex = CType(12.3d, Complex)
Console.WriteLine(c3)
' Assign the return value of a method to a Complex variable.
Dim c4 As Complex = Complex.Pow(Complex.One, -1)
Console.WriteLine(c4)
' Assign the value returned by an operator to a Complex variable.
Dim c5 As Complex = Complex.One + Complex.One
Console.WriteLine(c5)
' Instantiate a complex number from its polar coordinates.
Dim c6 As Complex = Complex.FromPolarCoordinates(10, .524)
Console.WriteLine(c6)
End Sub
End Module
輸出:
(12, 6) (3.14, 0) (12.3000001907349, 0) (1, 0) (2, 0) (8.65824721882145, 5.00347430269914)
示例2: Complex
Dim value As New Complex(Double.MinValue/2, Double.MinValue/2)
Dim value2 As Complex = Complex.Exp(Complex.Log(value))
Console.WriteLine("{0} {3}{1} {3}Equal: {2}", value, value2,
value = value2,
vbCrLf)
輸出:
(-8.98846567431158E+307, -8.98846567431158E+307) (-8.98846567431161E+307, -8.98846567431161E+307) Equal: False
示例3: Example
' 導入命名空間
Imports System.Numerics
Module Example
Public Sub Main()
Dim c1 As Complex = New Complex(Double.MaxValue / 2, Double.MaxValue /2)
Dim c2 As Complex = c1 / Complex.Zero
Console.WriteLine(c2.ToString())
c2 = c2 * New Complex(1.5, 1.5)
Console.WriteLine(c2.ToString())
Console.WriteLine()
Dim c3 As Complex = c1 * New Complex(2.5, 3.5)
Console.WriteLine(c3.ToString())
c3 = c3 + New Complex(Double.MinValue / 2, Double.MaxValue / 2)
Console.WriteLine(c3)
End Sub
End Module
輸出:
(NaN, NaN) (NaN, NaN) (NaN, Infinity) (NaN, Infinity)
示例4: GetFormat
' 導入命名空間
Imports System.Numerics
Public Class ComplexFormatter
Implements IFormatProvider, ICustomFormatter
Public Function GetFormat(formatType As Type) As Object _
Implements IFormatProvider.GetFormat
If formatType Is GetType(ICustomFormatter) Then
Return Me
Else
Return Nothing
End If
End Function
Public Function Format(fmt As String, arg As Object,
provider As IFormatProvider) As String _
Implements ICustomFormatter.Format
If TypeOf arg Is Complex Then
Dim c1 As Complex = DirectCast(arg, Complex)
' Check if the format string has a precision specifier.
Dim precision As Integer
Dim fmtString As String = String.Empty
If fmt.Length > 1 Then
Try
precision = Int32.Parse(fmt.Substring(1))
Catch e As FormatException
precision = 0
End Try
fmtString = "N" + precision.ToString()
End If
If fmt.Substring(0, 1).Equals("I", StringComparison.OrdinalIgnoreCase) Then
Return c1.Real.ToString(fmtString) + " + " + c1.Imaginary.ToString(fmtString) + "i"
ElseIf fmt.Substring(0, 1).Equals("J", StringComparison.OrdinalIgnoreCase) Then
Return c1.Real.ToString(fmtString) + " + " + c1.Imaginary.ToString(fmtString) + "j"
Else
Return c1.ToString(fmt, provider)
End If
Else
If Typeof arg Is IFormattable Then
Return DirectCast(arg, IFormattable).ToString(fmt, provider)
ElseIf arg IsNot Nothing Then
Return arg.ToString()
Else
Return String.Empty
End If
End If
End Function
End Class
示例5: Example
Module Example
Public Sub Main()
Dim c1 As Complex = 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))
End Sub
End Module
輸出:
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