Single::Equals Method (Object^)

 

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

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

public:
virtual bool Equals(
	Object^ obj
) override

Parameters

obj
Type: System::Object^

An object to compare with this instance.

Return Value

Type: System::Boolean

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

TheEquals 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.

No code example is currently available or this language may not be supported.

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.

No code example is currently available or this language may not be supported.

In this case, the values are equal.

System_CAPS_noteNote

Because Epsilon defines the minimum expression of a positive value whose range is near zero, the margin of difference 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.

Notes to Callers:

Compiler overload resolution may account for an apparent difference in the behavior of the two Equals method overloads. If an implicit conversion between the obj argument and a Single is defined and the argument is not typed as an Object, compilers may perform an implicit conversion and call the Equals(Single) method. Otherwise, they call the Equals(Object^) method, which always returns false if its obj argument is not a Single value. The following example illustrates the difference in behavior between the two method overloads. In the case of all primitive numeric types except for Double in Visual Basic and except for Decimal and Double in C#, the first comparison returns true because the compiler automatically performs a widening conversion and calls the Equals(Single) method, whereas the second comparison returns false because the compiler calls the Equals(Object^) method.

No code example is currently available or this language may not be supported.

The following code example demonstrates the Equals method.

obj1 = (Single)500;

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: