Equals Method (Object)

# Complex.Equals Method (Object)

Silverlight

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

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

## Syntax

```public override bool Equals(
Object obj
)
```

#### Parameters

obj
Type: System.Object
The object to compare.

#### Return Value

Type: System.Boolean
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);

```

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);
outputBlock.Text += c1.Equals(n1) + "\n";               // Returns true.

```

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 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 Demo(System.Windows.Controls.TextBlock outputBlock)
{
float n1 = 1.430718e-12f;
Complex c1 = new Complex(1.430718e-12, 0);
outputBlock.Text += String.Format("{0} = {1}: {2}", c1, n1, c1.Equals(n1)) + "\n";
}
}
// 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 Demo(System.Windows.Controls.TextBlock outputBlock)
{
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;
outputBlock.Text += String.Format("{0} = {1}: {2}", c1, n1, result) + "\n";
}
}
// The example displays the following output:
//       (1.430718E-12, 0) = 1.430718E-12: 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.