System.Single.CompareTo Method

Compares this instance to a specified single-precision floating-point number and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified single-precision floating-point number.


public int CompareTo (float value)


A single-precision floating-point number to compare.


A signed number indicating the relative values of this instance and value.

Less than zero

This instance is less than value.


This instance is not a number (float.NaN) and value is a number.


This instance is equal to value.


Both this instance and value are not a number (float.NaN), float.PositiveInfinity, or float.NegativeInfinity.

Greater than zero

This instance is greater than value.


This instance is a number and value is not a number (float.NaN).


Values must be identical to be considered equal. It is common for floating-point values to lose precision and to become nearly identical except for their least significant digits, especially when the values depend on multiple mathematical operations. Because of this, the return value of the float.CompareTo(float) method at times may seem surprising. For example, multiplication by any value followed by division by the same value should produce the original value. However, in the following example, the computed value turns out to be greater than the original value. Showing all significant digits of the two values by using the "R" standard numeric format string indicates that the computed value differs from the original value in its least significant digits. For information about handling such comparisons, see the Remarks section of the float.Equals(float) method.

code reference: System.Single.CompareTo#1

This method implements the IComparable`1 interface and performs slightly better than the float.CompareTo(object) method because it does not have to convert the value parameter to an object.

Note that, although an object whose value is float.NaN is not considered equal to another object whose value is float.NaN (even itself), the IComparable`1 interface requires that A.CompareTo(A) return zero.

Widening Conversions

Depending on your programming language, it might be possible to code a erload:System.Single.CompareTo 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 float and the parameter type is int. The Microsoft C# compiler generates instructions to represent the value of the parameter as a float object, then generates a float.CompareTo(float) 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. For more information, see the Type Conversion Tables topic.

Precision in Comparisons

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.


Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions:,
Since: .NET 2.0