Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Single.CompareTo Method (Object)

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.

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

'Declaration
Public Function CompareTo ( _
	value As Object _
) As Integer

Parameters

value
Type: System.Object

An object to compare, or Nothing.

Return Value

Type: System.Int32
A signed number indicating the relative values of this instance and value.

Return Value

Description

Less than zero

This instance is less than value.

-or-

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

Zero

This instance is equal to value.

-or-

This instance and value are both not a number (NaN), PositiveInfinity, or NegativeInfinity.

Greater than zero

This instance is greater than value.

-or-

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

-or-

value is Nothing.

Implements

IComparable.CompareTo(Object)

ExceptionCondition
ArgumentException

value is not a Single.

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

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 CompareTo method may seem surprising at times. For example, multiplication by a particular value followed by division by the same value should produce the original value, but 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 Equals(Single) method.

Module Example
   Public Sub Main()
       Dim value1 As Single = 16.5457
       Dim value2 As Object = value1 * CSng(3.8899982) / CSng(3.8899982)
       Console.WriteLine("Comparing {0} and {1}: {2}",
                         value1, value2, value1.CompareTo(value2))
       Console.WriteLine()
       Console.WriteLine("Comparing {0:R} and {1:R}: {2}",
                         value1, value2, value1.CompareTo(value2))
   End Sub 
End Module 
' The example displays the following output: 
'       Comparing 16.5457 and 16.5457: -1 
'        
'       Comparing 16.5457 and 16.545702: -1

This method is implemented to support the IComparable interface. Note that, although a NaN is not considered to be equal to another 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.

The following code example demonstrates the CompareTo method.

Obj1 = CType(450, Single)

If A.CompareTo(Obj1) < 0 Then
    Console.WriteLine(A.ToString() + " is less than " + Obj1.ToString() + ".")
End If 

If (A.CompareTo(Obj1) > 0) Then
    Console.WriteLine(A.ToString() + " is greater than " + Obj1.ToString() + ".")
End If 

If (A.CompareTo(Obj1) = 0) Then
    Console.WriteLine(A.ToString() + " equals " + Obj1.ToString() + ".")
End If

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft