Complex.Equals メソッド

定義

2 つの複素数が等しいかどうかを示す値を返します。

オーバーロード

名前 説明
Equals(Object)

現在のインスタンスと指定したオブジェクトの値が同じかどうかを示す値を返します。

Equals(Complex)

現在のインスタンスと指定した複素数の値が同じかどうかを示す値を返します。

Equals(Object)

ソース:
Complex.cs
ソース:
Complex.cs
ソース:
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

比較するオブジェクト。

返品

true obj パラメーターがComplex オブジェクトまたはComplex オブジェクトへの暗黙的な変換が可能な型であり、その値が現在のComplex オブジェクトと等しい場合はfalse。それ以外の場合は。

注釈

実数部が等しく、虚数部が等しい場合、2 つの複素数は等しくなります。 Equals(Object) メソッドは、次の式と同じです。

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

obj パラメーターがComplex オブジェクトではなく、暗黙的な変換が定義されているデータ型である場合、Equals(Object) メソッドは、objComplexの値と等しく、虚数部が 0 であるobj オブジェクトに変換してから比較を実行します。 次の例は、複素数と倍精度浮動小数点値が等しいことを見つけることでこれを示しています。

double n1 = 16.33;
System.Numerics.Complex c1 =
       new System.Numerics.Complex(16.33, 0);
Console.WriteLine(c1.Equals(n1));               // Returns true.
let n1 = 16.33;
let c1 = System.Numerics.Complex(16.33, 0)
printfn $"{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.

注意 (呼び出し元)

実数成分と虚数成分の精度が異なるため、明らかに等価な 2 つの値が等しくないと見なすことができるため、 Equals(Complex) メソッドを慎重に使用してください。 比較を実行する前に objDouble に変換する必要がある場合は、問題を強調できます。 次の例では、実数成分が 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
open System.Numerics

let n1 = 1.430718e-12f
let c1 = Complex(1.430718e-12, 0);
printfn $"{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

推奨される手法の 1 つは、値が等しいかどうかを比較するのではなく、2 つの値の差の許容できるマージン (値の実際の成分と虚数成分の 1 つの .01% など) を定義することです。 2 つの値の差の絶対値がその余白以下の場合、その差は精度の差が原因である可能性が高いため、値は等しい可能性があります。 次の例では、この手法を使用して、前のコード例で等しくないと判明した 2 つの値を比較します。 これで、それらが等しいことがわかります。

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
open System.Numerics

let n1 = 1.430718e-12f
let c1 = Complex(1.430718e-12, 0);
let difference = 0.0001f;

// Compare the values
let result = (abs (c1.Real - float n1) <= c1.Real * float difference) && c1.Imaginary = 0;
printfn $"{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
ソース:
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

比較する複素数。

返品

true この複素数と value が同じ値を持つ場合は。それ以外の場合は false

実装

注釈

Equals(Complex) メソッドは、IEquatable<T>構造体のComplex実装を提供します。 パラメーターを複素数に変換する必要がないため、 Equals(Object) メソッドよりも若干優れたパフォーマンスを発揮します。

実数部が等しく、虚数部が等しい場合、2 つの複素数は等しくなります。 Equals(Complex) メソッドは、次の式と同じです。

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

注意 (呼び出し元)

実数成分と虚数成分の精度が異なるため、明らかに等価な 2 つの値が等しくないと見なすことができるため、 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
    let c1 = System.Numerics.Complex(3.33333, 0.142857)
    let c2 = System.Numerics.Complex(10. / 3., 1. / 7.)
    printfn $"{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

推奨される手法の 1 つは、値が等しいかどうかを比較するのではなく、2 つの値の差の許容できるマージン (値の実際の成分と虚数成分の 1 つの .01% など) を定義することです。 2 つの値の差の絶対値がその余白以下の場合、その差は精度の差が原因である可能性が高いため、値は等しい可能性があります。 次の例では、この手法を使用して、前のコード例で等しくないことがわかった 2 つの複合値を比較します。 2 つの複素数が等しいと見なされます。

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
    let c1 = System.Numerics.Complex(3.33333, 0.142857)
    let c2 = System.Numerics.Complex(10. / 3., 1. / 7.)
    let difference = 0.0001

    // Compare the values
    let result =
        (Math.Abs(c1.Real - c2.Real) <= c1.Real * difference)
        && (Math.Abs(c1.Imaginary - c2.Imaginary) <= c1.Imaginary * difference)

    printfn $"{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

こちらもご覧ください

適用対象