Complex.Equals 方法

定义

返回一个值,该值指示两个复数是否相等。

重载

Equals(Object)

返回一个值,该值指示当前实例与指定的对象是否具有相同的值。

Equals(Complex)

返回一个值,该值指示当前实例与指定的复数是否具有相同的值。

Equals(Object)

Source:
Complex.cs
Source:
Complex.cs
Source:
Complex.cs

返回一个值,该值指示当前实例与指定的对象是否具有相同的值。

public:
 override bool Equals(System::Object ^ obj);
public override bool Equals (object obj);
public override bool Equals (object? obj);
override this.Equals : obj -> bool
Public Overrides Function Equals (obj As Object) As Boolean

参数

obj
Object

要比较的对象。

返回

如果 obj 参数是一个 Complex 对象或是一个能够隐式转换为 Complex 对象的类型,并且其值等于当前 Complex 对象,则为 true;否则为 false

注解

如果两个复数的实数相等且虚部分相等,则两个复数相等。 方法 Equals(Object) 等效于以下表达式:

return this.Real.Equals(((Complex) value).Real) &&
       this.Imaginary.Equals(((Complex) value).Imaginary);
Return Me.Real.Equals(CType(value, Complex).Real) AndAlso 
       Me.Imaginary.Equals(CType(value, Complex).Imaginary)

obj如果 参数不是 Complex 对象,但它是为其定义了隐式转换的数据类型,则 Equals(Object) 方法在执行比较之前,将转换为objComplex实际部分等于 的值obj且其虚部分等于零的 对象。 以下示例通过发现复数和双精度浮点值相等来说明这一点。

double n1 = 16.33;
System.Numerics.Complex c1 =
       new System.Numerics.Complex(16.33, 0);
Console.WriteLine(c1.Equals(n1));               // Returns true.
Dim n1 As Double = 16.33
Dim c1 As New System.Numerics.Complex(16.33, 0)
Console.WriteLine(c1.Equals(n1))                ' Returns True.

调用方说明

Equals(Complex)请谨慎使用 方法,因为两个明显等效的值可以被视为不相等,因为它们的实际和虚部分的精度不同。 如果在 obj 执行比较之前必须转换为 , Double 则可以强调问题。 以下示例比较一个复数,该复数的实际分量似乎等于 Single 具有该值 Single 的值。 如输出所示,相等性比较返回 False

using System;
using System.Numerics;

public class Example
{
   public static void Main()
   {
      float n1 = 1.430718e-12f;
      Complex c1 = new Complex(1.430718e-12, 0);
      Console.WriteLine("{0} = {1}: {2}", c1, n1, c1.Equals(n1));
   }
}
// The example displays the following output:
//       (1.430718E-12, 0) = 1.430718E-12: False
Imports System.Numerics

Module Example
   Public Sub Main()
      Dim n1 As Single = 1.430718e-12
      Dim c1 As New Complex(1.430718e-12, 0)
      Console.WriteLine("{0} = {1}: {2}", c1, n1, c1.Equals(n1))
   End Sub
End Module
' The example displays the following output:
'       (1.430718E-12, 0) = 1.430718E-12: False

建议的一种方法是定义两个值之间的可接受的差差 (,例如) 一个值的实际和虚部分的 0.01%,而不是比较值是否相等。 如果两个值之间的差异的绝对值小于或等于该边距,则差异可能是由于精度差异,因此,这些值可能相等。 以下示例使用此方法比较前面的代码示例发现不相等的两个值。 现在发现它们相等。

using System.Numerics;

public class Example
{
   public static void Main()
   {
      float n1 = 1.430718e-12f;
      Complex c1 = new Complex(1.430718e-12, 0);
      double difference = .0001;

      // Compare the values
      bool result = (Math.Abs(c1.Real - n1) <= c1.Real * difference) &
                    c1.Imaginary == 0;
      Console.WriteLine("{0} = {1}: {2}", c1, n1, result);
   }
}
// The example displays the following output:
//       (1.430718E-12, 0) = 1.430718E-12: True
Imports System.Numerics

Module Example
   Public Sub Main()
      Dim n1 As Single = 1.430718e-12
      Dim c1 As New Complex(1.430718e-12, 0)
      Dim difference As Double = .0001
      
      ' Compare the values
      Dim result As Boolean = (Math.Abs(c1.Real - n1) <= c1.Real * difference) And
                              c1.Imaginary = 0
      Console.WriteLine("{0} = {1}: {2}", c1, n1, result)       
   End Sub
End Module
' The example displays the following output:
'       (1.430718E-12, 0) = 1.430718E-12: True

适用于

Equals(Complex)

Source:
Complex.cs
Source:
Complex.cs
Source:
Complex.cs

返回一个值,该值指示当前实例与指定的复数是否具有相同的值。

public:
 virtual bool Equals(System::Numerics::Complex value);
public bool Equals (System.Numerics.Complex value);
override this.Equals : System.Numerics.Complex -> bool
Public Function Equals (value As Complex) As Boolean

参数

value
Complex

要比较的复数。

返回

如果此复数与 value 具有相同的值,则为 true;否则为 false

实现

注解

方法 Equals(Complex) 为 结构提供 IEquatable<T> 实现 Complex 。 它的性能略好于 Equals(Object) 方法,因为它不必将其参数转换为复数。

如果两个复数的实数相等且虚部分相等,则两个复数相等。 方法 Equals(Complex) 等效于以下表达式:

return this.Real.Equals(value) && this.Imaginary.Equals(value);
Return Me.Real.Equals(value.Real) AndAlso Me.Imaginary.Equals(value.Imaginary)

调用方说明

Equals(Complex)请谨慎使用 方法,因为两个明显等效的值可以被视为不相等,因为它们的实际和虚部分的精度不同。 以下示例报告 (3.33333, 0.142857)(10/3, 1/7) 不相等。

System.Numerics.Complex c1 = new System.Numerics.Complex(3.33333, .142857);
System.Numerics.Complex c2 = new System.Numerics.Complex(10/3.0, 1.0/7);
Console.WriteLine("{0} = {1}: {2}", c1, c2, c1.Equals(c2));
// The example displays the following output:
//    (3.33333, 0.142857) = (3.33333333333333, 0.142857142857143): False
Dim c1 As New System.Numerics.Complex(3.33333, .142857)
Dim c2 As New System.Numerics.Complex(10/3, 1/7)
Console.WriteLine("{0} = {1}: {2}", c1, c2, c1.Equals(c2))       
' The example displays the following output:
'    (3.33333, 0.142857) = (3.33333333333333, 0.142857142857143): False

建议的一种方法是定义两个值之间的可接受的差差 (,例如) 一个值的实际和虚部分的 0.01%,而不是比较值是否相等。 如果两个值之间的差异的绝对值小于或等于该边距,则差异可能是由于精度差异,因此,这些值可能相等。 以下示例使用此方法比较前面的代码示例发现不相等的两个复杂值。 它发现两个复数相等。

System.Numerics.Complex c1 = new System.Numerics.Complex(3.33333, .142857);
System.Numerics.Complex c2 = new System.Numerics.Complex(10/3.0, 1.0/7);
double difference = .0001;

// Compare the values
bool result = (Math.Abs(c1.Real - c2.Real) <= c1.Real * difference) &
              (Math.Abs(c1.Imaginary - c2.Imaginary) <= c1.Imaginary * difference);
Console.WriteLine("{0} = {1}: {2}", c1, c2, result);
// The example displays the following output:
//    (3.33333, 0.142857) = (3.33333333333333, 0.142857142857143): True
Dim c1 As New System.Numerics.Complex(3.33333, .142857)
Dim c2 As New System.Numerics.Complex(10/3.0, 1.0/7)
Dim difference As Double = .0001

' Compare the values
Dim result As Boolean = (Math.Abs(c1.Real - c2.Real) <= c1.Real * difference) And
                        (Math.Abs(c1.Imaginary - c2.Imaginary) <= c1.Imaginary * difference)
Console.WriteLine("{0} = {1}: {2}", c1, c2, result)       
' The example displays the following output:
'    (3.33333, 0.142857) = (3.33333333333333, 0.142857142857143): True

另请参阅

适用于