Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Double.Equals Method (Object)

Returns a value indicating whether this instance is equal to a specified object.

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

public override bool Equals (
	Object obj
)
public boolean Equals (
	Object obj
)
public override function Equals (
	obj : Object
) : boolean
Not applicable.

Parameters

obj

An object to compare with this instance.

Return Value

true if obj is an instance of Double and equals the value of this instance; otherwise, false.

The Equals 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 .3333 and the Double returned by dividing 1 by 3 are unequal.

Rather than comparing for equality, one recommended technique involves defining an acceptable margin of difference between two values (such as .01% 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.

NoteNote:

Because 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 Epsilon. Typically, it is many times greater than Epsilon.

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.

The following code example demonstrates the Equals method.

obj1 = (Double)500;
if (a.Equals(obj1)) {
    Console.WriteLine("The value type and reference type values are equal.");
}

obj1 = new Double(500);
if (obj1.Equals(a)) {
    Console.WriteLine(
        "The value type and reference type values are equal.");

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft