System.Double.CompareTo Method

Compares this instance to a specified object 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 object.


public int CompareTo (object value)


An object to compare, or null.


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

A negative integer

This instance is less than value.


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


This instance is equal to value.


This instance and value are both Double.NaN, double.PositiveInfinity, or double.NegativeInfinity

A positive integer

This instance is greater than value.


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


value is null.


ArgumentException value is not a null reference and is not of type double.


The value parameter must be null or an instance of Double; otherwise, an exception is thrown. Any instance of double, regardless of its value, is considered greater than null.

Values must be identical to be considered equal. Particularly when floating-point values depend on multiple mathematical operations, it is common for them to lose precision and for their values to be nearly identical except for their least significant digits. Because of this, the return value of the double.CompareTo(object) method at times may seem surprising. For example, multiplication by a particular value followed by division by the same value should produce the original value. In the following example, however, 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 on handling such comparisons, see the Remarks section of the double.Equals(double) method.

code reference: System.Double.CompareTo#2

This method is implemented to support the IComparable interface. Note that, although a double.NaN is not considered to be equal to another double.NaN (even itself), the IComparable interface requires that A.CompareTo(A) return zero.

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: 1.0.5000.0,,