System.Single.CompareTo methods

This article provides supplementary remarks to the reference documentation for this API.

Values must be identical to be considered equal. Particularly when floating-point values depend on multiple mathematical operations, it's 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.

Although an object whose value is NaN is not considered equal to another object whose value is NaN (even itself), the IComparable<T> interface requires that A.CompareTo(A) return zero.

CompareTo(System.Object)

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

using System;

public class Example
{
   public static void Main()
   {
       float value1 = 16.5457f;
       float operand = 3.8899982f;
       object value2 = value1 * operand / operand;
       Console.WriteLine("Comparing {0} and {1}: {2}\n",
                         value1, value2, value1.CompareTo(value2));
       Console.WriteLine("Comparing {0:R} and {1:R}: {2}",
                         value1, value2, value1.CompareTo(value2));
   }
}
// The example displays the following output:
//       Comparing 16.5457 and 16.5457: -1
//       
//       Comparing 16.5457 and 16.545702: -1
let value1 = 16.5457f
let operand = 3.8899982f
let value2 = box (value1 * operand / operand)
printfn $"Comparing {value1} and {value2}: {value1.CompareTo value2}\n"
printfn $"Comparing {value1:R} and {value2:R}: {value1.CompareTo value2}"
// The example displays the following output:
//       Comparing 16.5457 and 16.5457: -1
//       
//       Comparing 16.5457 and 16.545702: -1
Module Example2
    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.

CompareTo(System.Single)

This method implements the System.IComparable<T> interface and performs slightly better than the Single.CompareTo(Object) overload because it doesn't have to convert the value parameter to an object.

using System;

public class Example2
{
   public static void Main()
   {
       float value1 = 16.5457f;
       float operand = 3.8899982f;
       float value2 = value1 * operand / operand;
       Console.WriteLine("Comparing {0} and {1}: {2}\n",
                         value1, value2, value1.CompareTo(value2));
       Console.WriteLine("Comparing {0:R} and {1:R}: {2}",
                         value1, value2, value1.CompareTo(value2));
   }
}
// The example displays the following output:
//       Comparing 16.5457 and 16.5457: -1
//       
//       Comparing 16.5457 and 16.545702: -1
let value1 = 16.5457f
let operand = 3.8899982f
let value2 = value1 * operand / operand
printfn $"Comparing {value1} and {value2}: {value1.CompareTo value2}\n"
printfn $"Comparing {value1:R} and {value2:R}: {value1.CompareTo value2}"
// The example displays the following output:
//       Comparing 16.5457 and 16.5457: -1
//       
//       Comparing 16.5457 and 16.545702: -1
Module Example
   Public Sub Main()
       Dim value1 As Single = 16.5457
       Dim value2 As Single = 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

Widening conversions

Depending on your programming language, it might be possible to code a 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 Single and the parameter type is Int32. The Microsoft C# compiler generates instructions to represent the value of the parameter as a Single object, then generates a Single.CompareTo(Single) 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 .NET. Consequently, a comparison of two particular numbers might change between versions of .NET because the precision of the numbers' internal representation might change.