# Single.Equals 方法

## 重载

 Equals(Object) 返回一个值，该值指示此实例是否等于指定的对象。Returns a value indicating whether this instance is equal to a specified object. Equals(Single) 返回一个值，该值指示此实例和指定的 Single 对象是否表示相同的值。Returns a value indicating whether this instance and a specified Single object represent the same value.

## 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

Boolean

### 示例

``````obj1 = (Single)500;

if ( a.Equals( obj1 ) )
{
Console::WriteLine( "The value type and reference type values are equal." );
}
``````
``````obj1 = (Single)500;
if (a.Equals(obj1)) {
Console.WriteLine("The value type and reference type values are equal.");
}
``````
``````Obj1 = CType(500, Single)

If A.Equals(Obj1) Then
Console.WriteLine("The value type and reference type values are equal.")
End If
``````

### 注解

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. 下面的示例报告 Single 值3333和 Single 除以3相除后返回的值不相等。The following example reports that the Single value .3333 and the Single returned by dividing 1 by 3 are unequal.

``````// Initialize two floats with apparently identical values
float float1 = .33333f;
object float2 = 1/3;
// Compare them for equality
Console.WriteLine(float1.Equals(float2));    // displays false
``````
``````' Initialize two singles with apparently identical values
Dim single1 As Single = .33333
Dim single2 As Object = 1/3
' Compare them for equality
Console.WriteLine(single1.Equals(single2))    ' displays False
``````

``````// Initialize two floats with apparently identical values
float float1 = .33333f;
object float2 = (float) 1/3;
// Define the tolerance for variation in their values
float difference = Math.Abs(float1 * .0001f);

// Compare the values
// The output to the console indicates that the two values are equal
if (Math.Abs(float1 - (float) float2) <= difference)
Console.WriteLine("float1 and float2 are equal.");
else
Console.WriteLine("float1 and float2 are unequal.");
``````
``````' Initialize two singles with apparently identical values
Dim single1 As Single = .33333
Dim single2 As Object = 1/3
' Define the tolerance for variation in their values
Dim difference As Single = Math.Abs(single1 * .0001f)

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

### 调用方说明

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

## Equals(Single)

``````public:
virtual bool Equals(float obj);``````
``public bool Equals (float obj);``
``override this.Equals : single -> bool``
``Public Function Equals (obj As Single) As Boolean``

obj
Single

Boolean

### 比较中的精度Precision in Comparisons

Equals方法应谨慎使用，因为两个值的精度不同，因此两个明显等效的值可能不相等。The Equals method should be used with caution, because two apparently equivalent values can be unequal because of the differing precision of the two values. 下面的示例报告 Single 值3333和 Single 除以3相除后返回的值不相等。The following example reports that the Single value .3333 and the Single returned by dividing 1 by 3 are unequal.

``````// Initialize two floats with apparently identical values
float float1 = .33333f;
float float2 = 1/3;
// Compare them for equality
Console.WriteLine(float1.Equals(float2));    // displays false
``````
``````' Initialize two singles with apparently identical values
Dim single1 As Single = .33333
Dim single2 As Single = 1/3
' Compare them for equality
Console.WriteLine(single1.Equals(single2))    ' displays False
``````

``````// Initialize two floats with apparently identical values
float float1 = .33333f;
float float2 = (float) 1/3;
// Define the tolerance for variation in their values
float difference = Math.Abs(float1 * .0001f);

// Compare the values
// The output to the console indicates that the two values are equal
if (Math.Abs(float1 - float2) <= difference)
Console.WriteLine("float1 and float2 are equal.");
else
Console.WriteLine("float1 and float2 are unequal.");
``````
``````' Initialize two singles with apparently identical values
Dim single1 As Single = .33333
Dim single2 As Single = 1/3
' Define the tolerance for variation in their values
Dim difference As Single = Math.Abs(single1 * .0001f)

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

``````using System;

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

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

public static bool HasMinimalDifference(float value1, float value2, int units)
{
byte[] bytes = BitConverter.GetBytes(value1);
int iValue1 = BitConverter.ToInt32(bytes, 0);

bytes = BitConverter.GetBytes(value2);
int iValue2 = BitConverter.ToInt32(bytes, 0);

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

return false;
}

int diff = Math.Abs(iValue1 - iValue2);

if (diff <= units)
return true;

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

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

Public Function HasMinimalDifference(value1 As Single, value2 As Single, units As Integer) As Boolean
Dim bytes() As Byte = BitConverter.GetBytes(value1)
Dim iValue1 As Integer =  BitConverter.ToInt32(bytes, 0)

bytes = BitConverter.GetBytes(value2)
Dim iValue2 As Integer =  BitConverter.ToInt32(bytes, 0)

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

Dim diff As Integer =  Math.Abs(iValue1 - iValue2)

If diff <= units Then
Return True
End If

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

### 调用方说明

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