BigInteger.Compare(BigInteger, BigInteger) 方法

定义

比较两个 BigInteger 值,并返回一个整数,该整数指示第一个值是小于、等于还是大于第二个值。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.

public:
 static int Compare(System::Numerics::BigInteger left, System::Numerics::BigInteger right);
public static int Compare (System.Numerics.BigInteger left, System.Numerics.BigInteger right);
static member Compare : System.Numerics.BigInteger * System.Numerics.BigInteger -> int
Public Shared Function Compare (left As BigInteger, right As BigInteger) As Integer

参数

left
BigInteger

要比较的第一个值。The first value to compare.

right
BigInteger

要比较的第二个值。The second value to compare.

返回

一个有符号整数,指示 leftright 的相对值,如下表所示。A signed integer that indicates the relative values of left and right, as shown in the following table.

“值”Value 条件Condition
小于零Less than zero left 小于 rightleft is less than right.
Zero left 等于 rightleft equals right.
大于零Greater than zero left 大于 rightleft is greater than right.

注解

尽管 BigInteger 类型没有固定的范围,但 BigInteger 值的比较不是以反映浮点数比较的精度所引起的。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. 下面的示例比较两个不同的 BigInteger 值,每个值都有1896位数。The following example compares two BigInteger values that differ by one and that each have 1,896 digits. Compare 方法正确报告了两个值是否不相等。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
Dim number1 As BigInteger = BigInteger.Pow(Int64.MaxValue, 100)
Dim number2 As BigInteger = number1 + 1
Dim relation As String = ""
Select Case BigInteger.Compare(number1, number2)
   Case -1
      relation = "<"
   Case 0
      relation = "="
   Case 1
      relation = ">"
End Select            
Console.WriteLine("{0} {1} {2}", number1, relation, number2)
' The example displays the following output:
'    3.0829940252776347122742186219E+1896 < 3.0829940252776347122742186219E+1896

适用于

另请参阅