BigInteger.Compare(BigInteger, BigInteger) 方法

定义

比较两个 BigInteger 值,并返回一个整数,该整数指示第一个值是小于、等于还是大于第二个值。

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

要比较的第一个值。

right
BigInteger

要比较的第二个值。

返回

一个有符号整数,指示 leftright 的相对值,如下表所示。

条件
小于零 left 小于 right
left 等于 right
大于零 left 大于 right

注解

虽然 类型 BigInteger 没有固定范围,但值的比较 BigInteger 并不以浮点数比较特征的精度不足为特征。 以下示例比较两 BigInteger 个值,它们相差 1,并且每个值都有 1,896 位数字。 方法 Compare 正确报告这两个值不相等。

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
let number1 = BigInteger.Pow(int64 System.Int64.MaxValue, 100)
let number2 = number1 + 1I
let relation = 
    match BigInteger.Compare(number1, number2) with
    | -1 -> "<"
    | 0 -> "="
    | 1 | _ -> ">"

printfn $"{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

适用于

另请参阅