BigInteger.Compare(BigInteger, BigInteger) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
比較兩個 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
要比較的第二個值。
傳回
帶正負號的整數,表示 left
和 right
的相對值,如下表所示。
值 | 條件 |
---|---|
小於零 |
left 小於 right 。
|
零 |
left 等於 right 。
|
大於零 |
left 大於 right 。
|
備註
雖然此 BigInteger 類型沒有固定範圍,但值的比較 BigInteger 不是以浮點數比較為特徵的精確度所描述。 下列範例會比較兩 BigInteger 個不同值,每個值都有 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