System.Double.CompareTo Method

Compares this instance to a specified double-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 double-precision floating-point number.

Syntax

public int CompareTo (double value)

Parameters

value
A double-precision floating-point number to compare.

Returns

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

Less than zero

This instance is less than value.

-or-

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

Zero

This instance is equal to value.

-or-

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

Greater than zero

This instance is greater than value.

-or-

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

Remarks

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(double) 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#1

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

Note that, although an object whose value is double.NaN is not considered equal to another object whose value is double.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 double.CompareTo(double) 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 double and the parameter type is int. The Microsoft C# compiler generates instructions to represent the value of the parameter as a double object, then generates a double.CompareTo(double) 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.

Requirements

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