# Double.Equals 方法

## 多載

 傳回數值，指示這個執行個體和指定的 Double 物件是否表示相同的值。Returns a value indicating whether this instance and a specified Double object represent the same value. 傳回值，指出這個執行個體 (Instance) 是否和指定的物件相等。Returns a value indicating whether this instance is equal to a specified object.

## Equals(Double)

``````public:
virtual bool Equals(double obj);``````
``public bool Equals (double obj);``
``override this.Equals : double -> bool``
``Public Function Equals (obj As Double) As Boolean``

obj
Double

### 比較的有效位數Precision in Comparisons

Equals 方法應該謹慎使用，因為兩個明顯的相等值可能會因為兩個值的不同精確度而不相等。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. 下列範例會報告 Double 值333333和除以 1 x 3 所傳回的 Double 值不相等。The following example reports that the Double value .333333 and the Double value returned by dividing 1 by 3 are unequal.

``````// Initialize two doubles with apparently identical values
double double1 = .33333;
double double2 = 1/3;
// Compare them for equality
Console.WriteLine(double1.Equals(double2));    // displays false
``````
``````' Initialize two doubles with apparently identical values
Dim double1 As Double = .33333
Dim double2 As Double = 1/3
' Compare them for equality
Console.WriteLine(double1.Equals(double2))    ' displays False
``````

``````// Initialize two doubles with apparently identical values
double double1 = .333333;
double double2 = (double) 1/3;
// Define the tolerance for variation in their values
double difference = Math.Abs(double1 * .00001);

// Compare the values
// The output to the console indicates that the two values are equal
if (Math.Abs(double1 - double2) <= difference)
Console.WriteLine("double1 and double2 are equal.");
else
Console.WriteLine("double1 and double2 are unequal.");
``````
``````' Initialize two doubles with apparently identical values
Dim double1 As Double = .33333
Dim double2 As Double = 1/3
' Define the tolerance for variation in their values
Dim difference As Double = Math.Abs(double1 * .00001)

' Compare the values
' The output to the console indicates that the two values are equal
If Math.Abs(double1 - double2) <= difference Then
Console.WriteLine("double1 and double2 are equal.")
Else
Console.WriteLine("double1 and double2 are unequal.")
End If
``````

``````using System;

public class Example
{
public static void Main()
{
double value1 = .1 * 10;
double value2 = 0;
for (int ctr = 0; ctr < 10; ctr++)
value2 += .1;

Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2,
HasMinimalDifference(value1, value2, 1));
}

public static bool HasMinimalDifference(double value1, double value2, int units)
{
long lValue1 = BitConverter.DoubleToInt64Bits(value1);
long lValue2 = BitConverter.DoubleToInt64Bits(value2);

// If the signs are different, return false except for +0 and -0.
if ((lValue1 >> 63) != (lValue2 >> 63))
{
if (value1 == value2)
return true;

return false;
}

long diff = Math.Abs(lValue1 - lValue2);

if (diff <= (long) units)
return true;

return false;
}
}
// The example displays the following output:
//        01 = 0.99999999999999989: True
``````
``````Module Example
Public Sub Main()
Dim value1 As Double = .1 * 10
Dim value2 As Double = 0
For ctr As Integer =  0 To 9
value2 += .1
Next

Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2,
HasMinimalDifference(value1, value2, 1))
End Sub

Public Function HasMinimalDifference(value1 As Double, value2 As Double, units As Integer) As Boolean
Dim lValue1 As long =  BitConverter.DoubleToInt64Bits(value1)
Dim lValue2 As long =  BitConverter.DoubleToInt64Bits(value2)

' If the signs are different, Return False except for +0 and -0.
If ((lValue1 >> 63) <> (lValue2 >> 63)) Then
If value1 = value2 Then
Return True
End If
Return False
End If

Dim diff As Long =  Math.Abs(lValue1 - lValue2)

If diff <= units Then
Return True
End If

Return False
End Function
End Module
' The example displays the following output:
'       1 = 0.99999999999999989: True
``````

### 給呼叫者的注意事項

[！ code-csharpsystem.web. Equals # 2][！ code-vbsystem.web. Equals # 2][!code-csharpSystem.Double.Equals#2] [!code-vbSystem.Double.Equals#2]

## Equals(Object)

``````public:
override bool Equals(System::Object ^ obj);``````
``public override bool Equals (object obj);``
``override this.Equals : obj -> bool``
``Public Overrides Function Equals (obj As Object) As Boolean``

obj
Object

### 備註

Equals 方法應該謹慎使用，因為兩個明顯的相等值可能會因為兩個值的不同精確度而不相等。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. 下列範例會報告 Double 值3333和除以 1 x 3 所傳回的 Double 不相等。The following example reports that the Double value .3333 and the Double returned by dividing 1 by 3 are unequal.

``````// Initialize two doubles with apparently identical values
double double1 = .33333;
object double2 = 1/3;
// Compare them for equality
Console.WriteLine(double1.Equals(double2));    // displays false
``````
``````' Initialize two doubles with apparently identical values
Dim double1 As Double = .33333
Dim double2 As Object = 1/3
' Compare them for equality
Console.WriteLine(double1.Equals(double2))    ' displays False
``````

### 給呼叫者的注意事項

[！ code-csharpsystem.web. Equals # 2][！ code-vbsystem.web. Equals # 2][!code-csharpSystem.Double.Equals#2] [!code-vbSystem.Double.Equals#2]