public value class Complex : IEquatable<System::Numerics::Complex>, IFormattable
public value class Complex : IEquatable<System::Numerics::Complex>, IParsable<System::Numerics::Complex>, ISpanParsable<System::Numerics::Complex>, System::Numerics::IAdditionOperators<System::Numerics::Complex, System::Numerics::Complex, System::Numerics::Complex>, System::Numerics::IAdditiveIdentity<System::Numerics::Complex, System::Numerics::Complex>, System::Numerics::IDecrementOperators<System::Numerics::Complex>, System::Numerics::IDivisionOperators<System::Numerics::Complex, System::Numerics::Complex, System::Numerics::Complex>, System::Numerics::IEqualityOperators<System::Numerics::Complex, System::Numerics::Complex>, System::Numerics::IIncrementOperators<System::Numerics::Complex>, System::Numerics::IMultiplicativeIdentity<System::Numerics::Complex, System::Numerics::Complex>, System::Numerics::IMultiplyOperators<System::Numerics::Complex, System::Numerics::Complex, System::Numerics::Complex>, System::Numerics::INumberBase<System::Numerics::Complex>, System::Numerics::ISignedNumber<System::Numerics::Complex>, System::Numerics::ISubtractionOperators<System::Numerics::Complex, System::Numerics::Complex, System::Numerics::Complex>, System::Numerics::IUnaryNegationOperators<System::Numerics::Complex, System::Numerics::Complex>, System::Numerics::IUnaryPlusOperators<System::Numerics::Complex, System::Numerics::Complex>
public struct Complex : IEquatable<System.Numerics.Complex>, IFormattable
public readonly struct Complex : IEquatable<System.Numerics.Complex>, IFormattable
public readonly struct Complex : IEquatable<System.Numerics.Complex>, IParsable<System.Numerics.Complex>, ISpanParsable<System.Numerics.Complex>, System.Numerics.IAdditionOperators<System.Numerics.Complex,System.Numerics.Complex,System.Numerics.Complex>, System.Numerics.IAdditiveIdentity<System.Numerics.Complex,System.Numerics.Complex>, System.Numerics.IDecrementOperators<System.Numerics.Complex>, System.Numerics.IDivisionOperators<System.Numerics.Complex,System.Numerics.Complex,System.Numerics.Complex>, System.Numerics.IEqualityOperators<System.Numerics.Complex,System.Numerics.Complex>, System.Numerics.IIncrementOperators<System.Numerics.Complex>, System.Numerics.IMultiplicativeIdentity<System.Numerics.Complex,System.Numerics.Complex>, System.Numerics.IMultiplyOperators<System.Numerics.Complex,System.Numerics.Complex,System.Numerics.Complex>, System.Numerics.INumberBase<System.Numerics.Complex>, System.Numerics.ISignedNumber<System.Numerics.Complex>, System.Numerics.ISubtractionOperators<System.Numerics.Complex,System.Numerics.Complex,System.Numerics.Complex>, System.Numerics.IUnaryNegationOperators<System.Numerics.Complex,System.Numerics.Complex>, System.Numerics.IUnaryPlusOperators<System.Numerics.Complex,System.Numerics.Complex>
[System.Serializable]
public struct Complex : IEquatable<System.Numerics.Complex>, IFormattable
type Complex = struct
interface IFormattable
type Complex = struct
interface IFormattable
interface IParsable<Complex>
interface ISpanFormattable
interface ISpanParsable<Complex>
interface IAdditionOperators<Complex, Complex, Complex>
interface IAdditiveIdentity<Complex, Complex>
interface IDecrementOperators<Complex>
interface IDivisionOperators<Complex, Complex, Complex>
interface IEqualityOperators<Complex, Complex>
interface IIncrementOperators<Complex>
interface IMultiplicativeIdentity<Complex, Complex>
interface IMultiplyOperators<Complex, Complex, Complex>
interface INumberBase<Complex>
interface ISubtractionOperators<Complex, Complex, Complex>
interface IUnaryNegationOperators<Complex, Complex>
interface IUnaryPlusOperators<Complex, Complex>
interface ISignedNumber<Complex>
[<System.Serializable>]
type Complex = struct
interface IFormattable
Public Structure Complex
Implements IEquatable(Of Complex), IFormattable
Public Structure Complex
Implements IAdditionOperators(Of Complex, Complex, Complex), IAdditiveIdentity(Of Complex, Complex), IDecrementOperators(Of Complex), IDivisionOperators(Of Complex, Complex, Complex), IEqualityOperators(Of Complex, Complex), IEquatable(Of Complex), IIncrementOperators(Of Complex), IMultiplicativeIdentity(Of Complex, Complex), IMultiplyOperators(Of Complex, Complex, Complex), INumberBase(Of Complex), IParsable(Of Complex), ISignedNumber(Of Complex), ISpanParsable(Of Complex), ISubtractionOperators(Of Complex, Complex, Complex), IUnaryNegationOperators(Of Complex, Complex), IUnaryPlusOperators(Of Complex, Complex)
Complex
复数是包含实数部分和虚数部分的数字。 复数 z 通常以 z = x + yi 的形式编写,其中
x
和
y
是实数,
i
是具有属性
i
2
= -1 的虚数单位。 复数的实际部分由
x
表示,复数的虚部由
y
表示。
当实例化和操作复数时,该
Complex
类型使用笛卡尔坐标系 (真实虚构) 。 复数可以表示为二维坐标系中的点,称为复平面。 复数的实际部分位于水平轴) (x 轴上,虚部位于 y 轴上, (垂直轴) 。
使用极坐标系统,也可以根据复杂平面中的任何点的绝对值来表示。 在极坐标中,一个点的特点是两个数字:
其数量级,即从原点 (的距离,即 0,0,0,或 x 轴和 y 轴相交的点) 。
其阶段,即实轴与从原点到点绘制的线条之间的角度。
实例化复数
可以通过以下方法之一将值分配给复数:
通过将两
Double
个值传递给其构造函数。 第一个值表示复数的实际部分,第二个值表示其虚部。 这些值表示二维笛卡尔坐标系中复数的位置。
通过在 Visual Basic)
Complex.FromPolarCoordinates
方法中调用静态 (
Shared
,从其极坐标创建复数。
通过将 、
SByte
、、、
UInt16
Int16
、
UInt32
Int32
、
Int64
、
UInt64
或
Single
值
Double
分配给
Byte
Complex
对象。 该值将成为复数的实际部分,其虚部等于 0。
通过在 C#) 中转换 (或将 Visual Basic 中的 (转换为对象,)
Decimal
对象
Complex
或
BigInteger
值。 该值将成为复数的实际部分,其虚部等于 0。
通过将方法或运算符
Complex
返回的复数分配给对象。 例如,是一个静态方法,
Complex.Add
它返回一个复数,该复数是两个复数的总和
Complex.Addition
,运算符添加两个复数并返回结果。
以下示例演示了将值分配给复数的这五种方法中的每一种方法。
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);
// The example displays the following output:
// (12, 6)
// (3.14, 0)
// (12.3, 0)
// (1, 0)
// (2, 0)
// (8.65824721882145, 5.00347430269914)
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
' The example displays the following output:
' (12, 6)
' (3.14, 0)
' (12.3000001907349, 0)
' (1, 0)
' (2, 0)
' (8.65824721882145, 5.00347430269914)
包含复数的操作
Complex
.NET Framework中的结构包括提供以下功能的成员:
比较两个复数的方法,以确定它们是否相等。
用于对复数执行算术运算的运算符。
Complex
运算符使你能够使用复数执行加法、减法、乘法、除法和一元负运算。
对复数执行其他数值运算的方法。 除了四个基本算术运算之外,还可以将复数提高到指定幂,查找复数的平方根,并获取复数的绝对值。
对复数执行三角运算的方法。 例如,可以计算由复数表示的角度的正切值。
请注意,由于属性
Imaginary
是只读的
Real
,因此不能修改现有
Complex
对象的值。 对数字执行运算
Complex
的所有方法(如果其返回值的类型)
Complex
都返回一个新
Complex
数字。
精度和复数
复数的实数和虚部由两个双精度浮点值表示。 这意味着值
Complex
(如双精度浮点值)可能会因数值运算而失去精度。 这意味着,即使两
Complex
个值之间的差异是由于精度损失,两个值的相等性比较也可能失败。 有关详细信息,请参阅
Double
。
例如,对数字的对数执行指数应返回原始数字。 但是,在某些情况下,浮点值的精度损失可能会导致两个值之间的细微差异,如以下示例所示。
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);
// The example displays the following output:
// (-8.98846567431158E+307, -8.98846567431158E+307)
// (-8.98846567431161E+307, -8.98846567431161E+307)
// Equal: False
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)
' The example displays the following output:
' (-8.98846567431158E+307, -8.98846567431158E+307)
' (-8.98846567431161E+307, -8.98846567431161E+307)
' Equal: False
同样,以下示例计算数字
Complex
的平方根,在 32 位和 IA64 版本的.NET Framework上生成略有不同的结果。
Complex minusOne = new Complex(-1, 0);
Console.WriteLine(Complex.Sqrt(minusOne));
// The example displays the following output:
// (6.12303176911189E-17, 1) on 32-bit systems.
// (6.12323399573677E-17,1) on IA64 systems.
Dim minusOne As New Complex(-1, 0)
Console.WriteLine(Complex.Sqrt(minusOne))
' The example displays the following output:
' (6.12303176911189E-17, 1) on 32-bit systems.
' (6.12323399573677E-17,1) on IA64 systems.
复数、无穷大和 NaN
复数的实数和虚部由
Double
值表示。 除了从
Double.MinValue
中
Double.MaxValue
到处,复数的实数或虚部还可以有一个值
Double.PositiveInfinity
,
Double.NegativeInfinity
或者
Double.NaN
。
Double.PositiveInfinity
、
Double.NegativeInfinity
和
Double.NaN
所有在算术或三角运算中传播。
在以下示例中,除法
Zero
产生一个复数,其实数和虚部均为
Double.NaN
复数。 因此,使用此值执行乘法也会生成一个复数,其实数和虚部为
Double.NaN
复数。 同样,执行溢出类型范围的
Double
乘法会生成一个复数,其实部是
Double.NaN
其虚部
Double.PositiveInfinity
。 随后,使用此复数执行除法返回一个复数,其实部是
Double.NaN
其虚部。
Double.PositiveInfinity
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);
// The example displays the following output:
// (NaN, NaN)
// (NaN, NaN)
// (NaN, Infinity)
// (NaN, Infinity)
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
' The example displays the following output:
' (NaN, NaN)
' (NaN, NaN)
' (NaN, Infinity)
' (NaN, Infinity)
具有无效或溢出数据类型范围的
Double
数学运算不会引发异常。 相反,它们返回一个
Double.PositiveInfinity
或
Double.NegativeInfinity
Double.NaN
满足以下条件:
正数除以零返回
Double.PositiveInfinity
。
溢出数据类型上限
Double
的任何操作将
Double.PositiveInfinity
返回。
负数除以零返回
Double.NegativeInfinity
。
溢出数据类型下限
Double
的任何操作将
Double.NegativeInfinity
返回。
零除以零返回
Double.NaN
。
对值为
Double.PositiveInfinity
、
Double.NegativeInfinity
返回或返回
Double.NegativeInfinity
Double.PositiveInfinity
的操作
Double.NaN
数执行的任何操作,或
Double.NaN
取决于特定操作。
请注意,这适用于方法执行的任何中间计算。 例如,使用公式的乘
new Complex(9e308, 9e308) and new Complex(2.5, 3.5)
法 (ac - bd) + (ad + bc) i。 乘法产生的实际分量计算表达式 9e308 * 2.5 - 9e308 * 3.5。 此表达式中的每个中间乘法返回
Double.PositiveInfinity
,并尝试从
Double.PositiveInfinity
返回
Double.NaN
中减去
Double.PositiveInfinity
。
默认情况下,复数的字符串表示形式采用实际
虚构
)
形式
(
,其中
实
,
数和
虚
数是构成复数实数和虚数分量的值的字符串表示形式
Double
。 该方法的某些
ToString
重载允许自定义这些
Double
值的字符串表示形式,以反映特定区域性的格式约定,或以标准或自定义数字格式字符串定义的特定格式显示。 (有关详细信息,请参阅
标准数字格式字符串
和
自定义数字格式字符串
。)
表示复数的字符串表示形式之一是 +bi,其中一种是复数的实际分量,b 是复数的虚数分量。 在电气工程中,复数通常表示为 +bj。 可以在这两种形式之一中返回复数的字符串表示形式。 为此,请通过实现
ICustomFormatter
和
IFormatProvider
接口来定义自定义格式提供程序,然后调用该方法
String.Format(IFormatProvider, String, Object[])
。
以下示例定义一个
ComplexFormatter
类,该类以 +bi 或 + bj 的形式将复数表示为字符串。
using System;
using System.Numerics;
public class ComplexFormatter :IFormatProvider, ICustomFormatter
public object GetFormat(Type formatType)
if (formatType == typeof(ICustomFormatter))
return this;
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";
return c1.ToString(format, provider);
if (arg is IFormattable)
return ((IFormattable) arg).ToString(format, provider);
else if (arg != null)
return arg.ToString();
return String.Empty;
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
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
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"
Return c1.ToString(fmt, provider)
End If
If Typeof arg Is IFormattable Then
Return DirectCast(arg, IFormattable).ToString(fmt, provider)
ElseIf arg IsNot Nothing Then
Return arg.ToString()
Return String.Empty
End If
End If
End Function
End Class
下面的示例随后使用此自定义格式化程序显示复数的字符串表示形式。
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));
// The example displays the following output:
// 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
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
' The example displays the following output:
' 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