System.Double.Equals Method

Returns a value indicating whether this instance and a specified double object represent the same value.

Syntax

public bool Equals (double obj)

Parameters

obj
A double object to compare to this instance.

Returns

true if obj is equal to this instance; otherwise, false.

Remarks

This method implements the IEquatable`1 interface, and performs slightly better than double.Equals(object) because it does not have to convert the obj parameter to an object.

Widening Conversions

Depending on your programming language, it might be possible to code a double.Equals(double) method where the parameter type has fewer bits (is narrower) than the instance type. This is possible because some programming languages perform an implicit widening conversion that represents the parameter as a type with as many bits as the instance.

For example, suppose the instance type is double and the parameter type is int. The Microsoft C# compiler generates instructions to represent the value of the parameter as a double object, then generates a double.Equals(double) method that compares the values of the instance and the widened representation of the parameter.

Consult your programming language's documentation to determine if its compiler performs implicit widening conversions of numeric types. For more information, see the Type Conversion Tables topic.

Precision in Comparisons

The double.Equals(double) method should be used with caution, because two apparently equivalent values can be unequal due to the differing precision of the two values. The following example reports that the double value .333333 and the double value returned by dividing 1 by 3 are unequal.

code reference: System.Double.Epsilon#1

Rather than comparing for equality, one technique involves defining an acceptable relative margin of difference between two values (such as .001% of one of the values). 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 differences in precision and, therefore, the values are likely to be equal. The following example uses this technique to compare .33333 and 1/3, the two double values that the previous code example found to be unequal. In this case, the values are equal.

code reference: System.Double.Epsilon#2

Note:

Because double.Epsilon defines the minimum expression of a positive value whose range is near zero, the margin of difference between two similar values must be greater than double.Epsilon. Typically, it is many times greater than double.Epsilon. Because of this, we recommend that you do not use double.Epsilon when comparing double values for equality.

A second technique involves comparing the difference between two floating-point numbers with some absolute value. If the difference is less than or equal to that absolute value, the numbers are equal. If it is greater, the numbers are not equal. One alternative is to arbitrarily select an absolute value. This is problematic, however, because an acceptable margin of difference depends on the magnitude of the double values. A second alternative takes advantage of a design feature of the floating-point format: The difference between the integer representation of two floating-point values indicates the number of possible floating-point values that separates them. For example, the difference between 0.0 and double.Epsilon is 1, because double.Epsilon is the smallest representable value when working with a double whose value is zero. The following example uses this technique to compare .33333 and 1/3, which are the two double values that the previous code example with the double.Equals(double) method found to be unequal. Note that the example uses the BitConverter.DoubleToInt64Bits(double) method to convert a double-precision floating-point value to its integer representation.

code reference: System.Double.Equals#1

The precision of floating-point numbers beyond the documented precision is specific to the implementation and version of the .NET Framework. Consequently, a comparison of two particular numbers might change between versions of the .NET Framework because the precision of the numbers' internal representation might change.

If two double.NaN values are tested for equality by calling the double.Equals(double) method, the method returns true. However, if two double.NaN values are tested for equality by using the equality operator, the operator returns false. When you want to determine whether the value of a double is not a number (NaN), an alternative is to call the double.IsNaN(double) method.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0