Single.Equals Method (Single)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Returns a value indicating whether this instance and a specified Single object represent the same value.
Assembly: mscorlib (in mscorlib.dll)
Public Function Equals ( _ obj As Single _ ) As Boolean
public bool Equals( float obj )
true if obj is equal to this instance; otherwise, false.
Depending on your programming language, it might be possible to code an Equals 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 Single and the parameter type is Int32. The Microsoft C# compiler generates instructions to represent the value of the parameter as a Single object, then generates a Single..::.Equals(Single) 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.
Precision in Comparisons
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 Single value .3333 and the Single returned by dividing 1 by 3 are unequal.
' Initialize two singles with apparently identical values Dim single1 As Single = 0.33333 Dim single2 As Single = 1 / 3 ' Compare them for equality outputBlock.Text &= single1.Equals(single2) & vbCrLf ' displays False
// Initialize two floats with apparently identical values float float1 = .33333f; float float2 = 1 / 3; // Compare them for equality outputBlock.Text += float1.Equals(float2) + "\n"; // displays false
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 Single values that the previous code example found to be unequal.
' Initialize two singles with apparently identical values Dim single1 As Single = 0.33333 Dim single2 As Single = 1 / 3 ' Define the tolerance for variation in their values Dim difference As Single = Math.Abs(single1 * 0.0001F) ' Compare the values ' The output indicates that the two values are equal If Math.Abs(single1 - single2) <= difference Then outputBlock.Text &= "single1 and single2 are equal." & vbCrLf Else outputBlock.Text &= "single1 and single2 are unequal." & vbCrLf End If
// 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 indicates that the two values are equal if (Math.Abs(float1 - float2) <= difference) outputBlock.Text += "float1 and float2 are equal." + "\n"; else outputBlock.Text += "float1 and float2 are unequal." + "\n";
In this case, the values are equal.
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.
Windows Phone OS
Supported in: 8.1, 8.0, 7.1, 7.0