0

I am currently using a bigint class that was given to me. I have managed to create the adding subtracting and multiplication operations successfully, however I cannot seem to crack the division operator.

I am not fussed on getting the remainder of a quotient I am only interested in the number prior to the decimal. I also have added some checks. The first being if the second number is zero then it will return zero. The second check is if is if the second number is greater than the first I return zero as I am not interested in numbers below zero.

Below is my code and what I have done so far for this big int operator.

Bigint operator/ (const Bigint& n1, const Bigint& n2) {

    Bigint final;
    Bigint quotient;
    int count = 0;
    Bigint result = n1;
    Bigint check;


    for(int i = 0; i < DIGITS; ++i) {
        if(n2.digits[i] == 0){

            quotient = 0;
        }

        else if (n2.digits[i] > n1.digits[i]){

            quotient = 0;
        }

        else {

            while (result.digits[0] > 0){
                for(int i = 0; i < DIGITS; ++i){
                    result.digits[i] -= n2.digits[i];
                if(result.digits[i] < 0){
                    result.digits[i] += 10;
                    result.digits[i+1] = -1;
                }
                }
                count++;
            }


            for(int j = 1; j < DIGITS; j++){
                final.digits[j] = count % 10;
                count = count / 10;
            }
            return final;
                }

            }
            return final;


        }

No matter what I enter into my program it always returns a zero, I've been at this for several hours and can't for the life of me crack it. Any help is greatly appreciated.

Some examples of desired results are:

987654321 / 123456789 = 8
123425 / 545 = 226

Cheers

  • 1
    If you step through the code in a debugger, line by line and watching all variables and their values, nothing seems to be off? All steps in the calculation are correct? – Some programmer dude Apr 20 '17 at 10:59
  • OK, so what part of **// placeholder code: only correct if n2 is 1** do you think we should ignore? – n. m. could be an AI Apr 20 '17 at 11:05
  • @n.m. sorry ignore that comment, I thought I had removed it. – websafepalletone Apr 20 '17 at 11:10
  • "if the second number is greater than the first I return zero as I am not interested in numbers below zero" casts some doubt on whether you know how division works at all. – molbdnilo Apr 20 '17 at 11:12
  • Perhaps check the rest of the code too for things you should have removed. There are quite a few things that could only have got into the code from outer space. – n. m. could be an AI Apr 20 '17 at 11:22
  • @molbdnilo please get off your high horse and muster all your concentration skills, limited it may be and understand that for the application of this program I don't need the program to work for quotients that are below zero e.g. 6 / 8 = 0.75. – websafepalletone Apr 20 '17 at 11:23
  • 1
    @websafepalletone Do you seriously believe that 0.75 is less than zero? – molbdnilo Apr 20 '17 at 11:27
  • @websafepalletone: I don't think you should talk about "limited skills" of others. You obviously don't understand how multiple precision division works. So google for it. – Rudy Velthuis Apr 20 '17 at 12:40
  • @molbdnilo: Yes, well, that is true, but 100000000000/2 will indeed take a long time (as the first answer says), if you do it that way, There are far better ways. – Rudy Velthuis Apr 20 '17 at 15:04

1 Answers1

1

I am not fussed on getting the remainder of a quotient I am only interested in the number prior to the decimal.

You need to decide what kind of numbers you are trying to work with and what kind of division you implement. Integers have no "decimal" (whatever it is). Real number division has no remainder.

The second check is if is if the second number is greater than the first

You are not checking that, you are checking that each digit of the second number is greater than the corresponding digit of the first number. 222 and 999 would pass, but 222 and 990 would fail.

Bigint quotient;

Only assigned, never used.

int count = 0;

This is your final result (you convert it to Bigint later). It can overflow if the result of the division doesn't fit into an int. What's the point of having Bigint then?

while (result.digits[0] > 0){

This doesn't seem correct at all. Why stop the loop when you see 0 in the least significant digit of the result? It looks like you want to stop when the result as a whole is zero or below.

result.digits[i+1] = -1;

A negative digit? Perhaps you wanted result.digits[i+1] -= 1;

count++;

You are implementing the repeated subtraction method of division. While mathematically correct, it is rather slow. 1000000000000000000000000000 / 2 would take a loooooooooong time to compute.

for(int j = 1; j < DIGITS; j++){

Probably should start from 0, like any good little for loop.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243