BigInteger.Compare(BigInteger, BigInteger) Método

Definição

Compara dois BigInteger valores e devolve um inteiro que indica se o primeiro valor é menor, igual ou maior que o segundo.

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

Parâmetros

left
BigInteger

O primeiro valor a comparar.

right
BigInteger

O segundo valor a comparar.

Devoluções

Um inteiro com sinal que indica os valores relativos de left e right, conforme mostrado na tabela seguinte.

Valor Condição
Menos que zero left é inferior a right.
Zero left igual a right.
Maior que zero left é maior que right.

Observações

Embora o BigInteger tipo não tenha um alcance fixo, as comparações de BigInteger valores não se caracterizam pela falta de precisão que caracteriza a comparação de números em ponto flutuante. O exemplo seguinte compara dois BigInteger valores que diferem por um e que cada um tem 1.896 dígitos. O Compare método indica corretamente que os dois valores não são iguais.

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

Aplica-se a

Ver também