user3165933
user3165933

Reputation: 17

Adding two big numbers pt.2

So in my last question's code was an error. I tried to modify the code which should add two big numbers made as two arrays (I can't use BigIntiger for this, I have to made the method by myself). But it still gives me wrong results of addition.

For example (I already have the constructors for this):

BigNumber dl1 = new BigNumber(1500);
BigNumber dl2 = new BigNumber("987349837937497938943242");

dl3 = dl1.add(dl2);
    System.out.println("Result: " + dl3);

It gives me 6575 which is wrong result.

public BigNumber add(BigNumber num2){

    char[] m = null;
    long y = 0;
    long x = 0;
    boolean tmpBool = false;
    boolean leftIsBigger = false;
    String tmpString = "";
    int ending = 0;

    if (this.n.length >= num2.n.length){
        m = new char[this.n.length + 1];
        y = num2.n.length;
        x = this.n.length;
        leftIsBigger = true;
    }
    else{
        m = new char[this.n.length + 1];
        y = this.n.length;
        x = num2.n.length;
    }

    for(int i = 0; i < y; i++){
        int left = 0;
        if(leftIsBigger) left = Character.getNumericValue(this.n[i]);
        else left = Character.getNumericValue(num2.n[i]);

        for(int j = 0; j < y; j++){
            int right = 0;
            if(!leftIsBigger) right = Character.getNumericValue(num2.n[j]);
            else righta = Character.getNumericValue(this.n[j]);

            int z = left + right;

            if(tmpBool){
                z++;
                tmpBool = false;
            }
            if(z > 9){
                tmpBool = true;
                z = z%10;
            }
            m[i] = Character.forDigit(z, 10);
        }

        ending++;
    }

    for(int k = ending; k < m.length - 1; k++){
        if (leftIsBigger){
            if (tmpBool){
                int c = Character.getNumericValue(this.n[k]);
                if (c > 9){
                    tmpBool = true;
                    c = c%10;
                    m[k] = Character.forDigit(c, 10);
                }
                else{
                    tmpBool = false;
                    m[k] = Character.forDigit((c+1), 10);
                }
            }
            else
                m[k] = this.n[k];
        }else{
            if (tmpBool){
                int c = Character.getNumericValue(liczba2.n[k]);
                if (c > 9){
                    tmpBool = true;
                    c = c%10;
                    m[k] = Character.forDigit(c, 10);
                }
                else{
                    tmpBool = false;
                    m[k] = Character.forDigit((c+1), 10);
                }
            }
            else
                m[k] = this.n[k];
        }
    }
    for (int it = m.length - 1; it >= 0; it--){
        tmpString += m[it];
    }

    BigNumber dl = new BigNumber(tmpString);
    return dl;      
}

Upvotes: 0

Views: 104

Answers (2)

Ingo
Ingo

Reputation: 36339

Your code is too complicated for me to search for the error. The whole "left is longer" logic is flawed, IMHO.

I'd do it thus, assuming we are working on char-Arrays with decimal digits in them:

char [] x, y;    // the operands we want to add
char [] result = new char[max (x.length, y.length) + 1]; 
int xi = x.length-1;        // index in 1st operand
int yi = y.length-1;        // index in 2nd operand
int ri = result.length-1;   // index in result
boolean carry = false;
while (xi >= 0 || yi >= 0) {
   char xc  = xi >= 0 ? x[xi--] : '0';
   char yc  = yi >= 0 ? y[yi--] : '0';
   char res = xc + yc - '0';
   if (carry) res++;
   carry = res > '9';
   if (carry) res -= 10;
   result[ri--] = res;
}
assert (ri == 0);
result[0] = carry ? '1' : '0';

Note that the result array is always 1 char longer than the longest argument. This is not good, as repeated additions will result in longer and longer arrays that carry a lot of 0 in front. Hence, either copy the result to another array if the last addition did not have a carry bit, or - even better - change the algorithm so that it ignores leading zeroes.

This is left as an exercise.

Upvotes: 1

Bart Enkelaar
Bart Enkelaar

Reputation: 694

Isn't the problem that in your initial if statement (The one that checks the lengths of the inner arrays) in the else you initialize your m char array to the length of this.n instead of num2.n?

EDIT: Also, the way you've setup your iterations, I assume your inner arrays go from left to right? as in Index 0 is 10^0, index 1 is 10^1, index 2 is 10^2 etc? Otherwise that would be a problem as well. Be mindful that this means you have to revert the inner String char array in the String type constructor.

Upvotes: 1

Related Questions