C# BigInteger Compare

Description

BigInteger Compare 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.

Syntax

BigInteger.Compare has the following syntax.


public static int Compare(
  BigInteger left,
  BigInteger right
)

Parameters

BigInteger.Compare has the following parameters.

  • left - The first value to compare.
  • right - The second value to compare.

Returns

BigInteger.Compare method returns 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.

Example


using System;// ww w.  j  a v  a2 s.  c  om
using System.IO;
using System.Numerics;

public class Example
{
   public static void Main()
   {
        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 code above generates the following result.





















Home »
  C# Tutorial »
    System.Numerics »




BigInteger
Complex