Complex.Equals Method

Definition

Returns a value that indicates whether two complex numbers are equal.

Overloads

Equals(Object)

Returns a value that indicates whether the current instance and a specified object have the same value.

Equals(Complex)

Returns a value that indicates whether the current instance and a specified complex number have the same value.

Equals(Object)

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

Returns a value that indicates whether the current instance and a specified object have the same value.

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

Parameters

obj
Object

The object to compare.

Returns

true if the obj parameter is a Complex object or a type capable of implicit conversion to a Complex object, and its value is equal to the current Complex object; otherwise, false.

Remarks

Two complex numbers are equal if their real parts are equal and their imaginary parts are equal. The Equals(Object) method is equivalent to the following expression:

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)

If the obj parameter is not a Complex object, but it is a data type for which an implicit conversion is defined, the Equals(Object) method converts obj to a Complex object whose real part is equal to the value of obj and whose imaginary part is equal to zero before it performs the comparison. The following example illustrates this by finding that a complex number and a double-precision floating-point value are equal.

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.

Notes to Callers

Use the Equals(Complex) method with caution, because two values that are apparently equivalent can be considered unequal due to the differing precision of their real and imaginary components. The problem can be accentuated if obj must be converted to a Double before performing the comparison. The following example compares a complex number whose real component appears to be equal to a Single value with that Single value. As the output shows, the comparison for equality returns 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

One recommended technique is to define an acceptable margin of difference between the two values (such as .01% of one of the values' real and imaginary components) instead of comparing the values for equality. If the absolute value of the difference between the two values is less than or equal to that margin, the difference is likely to be due to a difference in precision and, therefore, the values are likely to be equal. The following example uses this technique to compare the two values that the previous code example found to be unequal. It now finds them to be equal.

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

Applies to

Equals(Complex)

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

Returns a value that indicates whether the current instance and a specified complex number have the same value.

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

Parameters

value
Complex

The complex number to compare.

Returns

true if this complex number and value have the same value; otherwise, false.

Implements

Remarks

The Equals(Complex) method provides the IEquatable<T> implementation for the Complex structure. It performs slightly better than Equals(Object) method because it does not have to convert its parameter to a complex number.

Two complex numbers are equal if their real parts are equal and their imaginary parts are equal. The Equals(Complex) method is equivalent to the following expression:

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)

Notes to Callers

Use the Equals(Complex) method with caution, because two values that are apparently equivalent can be considered unequal due to the differing precision of their real and imaginary components. The following example reports that (3.33333, 0.142857) and (10/3, 1/7) are not equal.

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

One recommended technique is to define an acceptable margin of difference between the two values (such as .01% of one of the values' real and imaginary components) instead of comparing the values for equality. If the absolute value of the difference between the two values is less than or equal to that margin, the difference is likely to be due to a difference in precision, and, therefore, the values are likely to be equal. The following example uses this technique to compare the two complex values that the previous code example found to be unequal. It finds the two complex numbers to be equal.

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

See also

Applies to