Search code examples
c#arraysbigintegercompareto

How to compare big integers stored in int array?


I am trying to implement BigInteger class in C#. Right now I am stuck at a method called isLessThan(HugeInteger b). This is my class and the corresponding methods.

class HugeInteger
{
    public int[] array = new int[40];

    public HugeInteger(String s)
    {
        this.input(s);
    } // end constructor

    /*To read a huge integer having upto 40 digits, I am using an integer array to store 
      these values(input is read as a string for this matter) and later on will override 
      ToString() for output if needed.*/
    private void input(String s)
    {
        char[] charInteger = s.ToCharArray();
        int index = 0;
        for (int i = 0; i <= 39 - charInteger.Length; i++)
        {
            array[i] = 0;
        }
        for (int i = 39 - charInteger.Length + 1; i <= 39; i++)
        {

            array[i] = int.Parse(charInteger[index].ToString());
            index++;
        }
    }

    public bool isLessThan(HugeInteger that)
    {

        for (int i = 0; i <= 39; i++)
        {
            if (this.array[i] < that.array[i])
            {
                return true;
            }
        }
        return false;
    }
}

So basically, I have 40 digits stored into an integer array for every HugeInteger object. But I know for sure that my method isLessThan(HugeInteger b) is wrong and that there is something simple I am overlooking. So how should I go about making a right isLessthan(HugeInteger b) method?

Edit: My isLessThan method doesn't work for some cases like if I try to compare "9873" and "75", I get true, but I need a false. Sorry for not being clear.

Note: I give my inputs(like 9873 or 75) as a string and then parse them to int in my input method and then store them into integer array.


Solution

  • OK, let's implement the comparisons; the typical way uses universal comparison (in some languages it's even has a special operator <=>)

      class HugeInteger {
        ...
    
        // Universal comparator
        //  +1 left > right
        //   0 left == right
        //  -1 left < right
        public static int Compare(HugeInteger left, HugeInteger right) {
          // first, we should deal with null's; let null be the leaset possible value
    
          // left and right are just the references of one inctance?
          if (Object.ReferenceEquals(left, right)) 
            return 0;
          else if (left == null)
            return -1;
          else if (right == null)
            return 1;
    
          // Now we checked for null, let's compare both arrays  
    
          //DONE: no 39, 40 or other magic numbers, but Length 
          for (int i = 0; i < left.array.Length; ++i)
            if (left.array[i] < right.array[i])
              return -1;
            else if (left.array[i] > right.array[i])
              return 1;
    
          return 0; // no differences found, so left equals to right
        }
    

    Having comparator implemented it's very easy to write isLessThan, isGreaterThan:

        public bool isLessThan(HugeInteger other) {
          return Compare(this, other) < 0;
        }
    
        public bool isGreaterThan(HugeInteger other) {
          return Compare(this, other) > 0;
        }
        ....