Complex.Equals Method (Complex)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Namespace:  System.Numerics
Assembly:  System.Numerics (in System.Numerics.dll)

Syntax

'Declaration
Public Function Equals ( _
    value As Complex _
) As Boolean
public bool Equals(
    Complex value
)

Parameters

Return Value

Type: System.Boolean
true if this complex number and value have the same value; otherwise, false.

Implements

IEquatable<T>.Equals(T)

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 Me.real.Equals(value.real) AndAlso Me.imaginary.Equals(value.imaginary)
return this.Real.Equals(value) && this.Imaginary.Equals(value);

Notes to Callers

Use the Equals 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.

Dim c1 As New System.Numerics.Complex(3.33333, 0.142857)
Dim c2 As New System.Numerics.Complex(10 / 3, 1 / 7)
outputBlock.Text += String.Format("{0} = {1}: {2}", c1, c2, c1.Equals(c2)) & vbCrLf
' 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);
outputBlock.Text += String.Format("{0} = {1}: {2}", c1, c2, c1.Equals(c2)) + "\n";
// 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.

Dim c1 As New System.Numerics.Complex(3.33333, 0.142857)
Dim c2 As New System.Numerics.Complex(10 / 3.0, 1.0 / 7)
Dim difference As Double = 0.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)
outputBlock.Text += String.Format("{0} = {1}: {2}", c1, c2, result) & vbCrLf
' The example displays the following output:
'    (3.33333, 0.142857) = (3.33333333333333, 0.142857142857143): True
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);
outputBlock.Text += String.Format("{0} = {1}: {2}", c1, c2, result) + "\n";
// The example displays the following output:
//    (3.33333, 0.142857) = (3.33333333333333, 0.142857142857143): True

Version Information

Silverlight

Supported in: 5, 4

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.