BigInteger.Compare Method

Compares two BigInteger values and returns an integer that indicates whether the first value is less than, equal to, or greater than the second value.

Namespace:  System.Numerics
Assemblies:   System.Runtime.Numerics (in System.Runtime.Numerics.dll)
  System.Numerics (in System.Numerics.dll)

public static int Compare(
	BigInteger left,
	BigInteger right
)

Parameters

left
Type: System.Numerics.BigInteger

The first value to compare.

right
Type: System.Numerics.BigInteger

The second value to compare.

Return Value

Type: System.Int32
A signed integer that indicates the relative values of left and right, as shown in the following table.

Value

Condition

Less than zero

left is less than right.

Zero

left equals right.

Greater than zero

left is greater than right.

Although the BigInteger type has no fixed range, comparisons of BigInteger values are not characterized by the lack of precision that characterizes the comparison of floating-point numbers. The following example compares two BigInteger values that differ by one and that each have 1,896 digits. The Compare method correctly reports that the two values are not equal.

BigInteger number1 = BigInteger.Pow(Int64.MaxValue, 100);
BigInteger number2 = number1 + 1;
string relation = "";
switch (BigInteger.Compare(number1, number2))
{
   case -1:
      relation = "<";
      break;
   case 0:
      relation = "=";
      break;
   case 1:
      relation = ">";
      break;
}
Console.WriteLine("{0} {1} {2}", number1, relation, number2);
// The example displays the following output: 
//    3.0829940252776347122742186219E+1896 < 3.0829940252776347122742186219E+1896

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

.NET for Windows Phone apps

Supported in: Windows Phone 8.1

Portable Class Library

Supported in: Portable Class Library
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft