System.Single.Equals Method

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

Syntax

public override bool Equals (object obj)

Parameters

obj
An object to compare with this instance.

Returns

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

Remarks

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

code reference: System.Single.Epsilon#3

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 float values that the previous code example found to be unequal.

code reference: System.Single.Epsilon#4

In this case, the values are equal.

Note:

Because float.Epsilon defines the minimum expression of a positive value whose range is near zero, the margin of difference must be greater than float.Epsilon. Typically, it is many times greater than float.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.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0