Complex.Equals 方法

定義

傳回值,這個值表示兩個複數是否相等。

多載

Equals(Object)

傳回值,這個值表示目前執行個體與指定的物件是否有相同的值。

Equals(Complex)

傳回值,這個值指出目前執行個體和指定複數是否有相同的值。

Equals(Object)

來源:
Complex.cs
來源:
Complex.cs
來源:
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

其中一個建議的技術是定義兩個值之間可接受的差異邊界 (,例如其中一個值的實數和虛陣列件) ,而不是比較相等的值。 如果兩個值之間的差異絕對值小於或等於該邊界,則差異可能是因為有效位數的差異而造成,因此,這些值可能相等。 下列範例會使用這項技術來比較先前程式碼範例發現不相等的兩個值。 現在會發現它們相等。

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)

來源:
Complex.cs
來源:
Complex.cs
來源:
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

其中一個建議的技術是定義兩個值之間可接受的差異邊界 (,例如其中一個值的實數和虛陣列件) ,而不是比較相等的值。 如果兩個值之間的差異絕對值小於或等於該邊界,則差異可能是因為有效位數的差異,因此值可能相等。 下列範例會使用這項技術來比較先前程式碼範例發現不相等的兩個複雜值。 它會發現兩個複數相等。

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

另請參閱

適用於