1

EDIT: I rebased my bignum class to use std::bitset and I just implemented long division fine. I just didn't know any class to store bits. (like std::bitset)

I'm making a bignum class with std::string to use decimal characters as internal representation. I tried implementing division with a simple algorithm:

while N ≥ D do N := N - D end return N

And of course it was slow. I tried implementing long division but that was too hard to do with decimal characters.

Thanks in advance.

12
  • 2
    Do they still teach "long division" in primary school? Or is everyone dependent on electronics these days? Commented Jan 14, 2015 at 12:50
  • Bitwise operations can work just fine with a bignum class Commented Jan 14, 2015 at 12:51
  • Which Bignum class? Have you read the standard literature? Could you point out what you've researched so far in an edit to your question? Why would bitwise operators not work for your number representation? Why would you be the one to implement division for a numerical type for which almost certainly a competent library exists? Commented Jan 14, 2015 at 12:54
  • 1
    That algorithm looks more like modulo than (integer) division. Also, what language are you using, what "Bignum" class do you use, and where is your code? What is the data type of N and D? Commented Jan 14, 2015 at 13:07
  • 1
    If stuff above is too much for you then you can try to use binary search (no need for bit operations) you need just +,*,<= Commented Jan 15, 2015 at 7:19

1 Answer 1

1

Instead of subtracting D very often you could try to find the highest value of the form D^2n and sub this. Than repeat that steps with the remaining value until the remaining is less than D.

Pseudocode

0 result=0
1 powerOfD = D
2 while (powerOfD*powerOfD<N)
3   powerOfD=powerOfD*powerOfD
4 result = result+powerOfD/D, N=N-powerOfD;
5 if(N>D) 
6   goto 1
7 return result

Example 31/3 (N=31, D=3)

0 result=0
1 powerD = 3;
2 3*3 < 31   TRUE
3   powerOfD= 3*3;
2 9*9 < 31   FALSE
4 result=0+9/3; N=31 - 9
5 22> 3   TRUE
6   goto 1
1 powerD = 3
2 3*3 < 22  TRUE
3   powerOfD= 3*3;
2 9*9 < 31   FALSE
4 result=3+9/3; N=22 - 9
5 13> 3   TRUE
6   goto 1
1 powerD = 3
2 3*3 < 13  TRUE
3   powerOfD= 3*3;
2 9*9 < 31   FALSE
4 result=6+9/3; N=13 - 9
5 4> 3   TRUE
6   goto 1
1 powerD = 3
2 3*3 < 4 ALSE
4 result=9+3/3; N=4-3
5 1> 3   FALSE
7 return 10
Sign up to request clarification or add additional context in comments.

4 Comments

What is the significance of result? Can you please show the steps for 31/3?
I fixed a problem in the algorithm and added the requested example. 'result' is the devision result 31/3=10 remains = 1 (stored in N)
Thanks for taking may comment seriously (: and excruciatingly to the letter :).
@greybeard: Your comment stuck me to an error in the algorithm and so it lead to a simpler and now working version. So your comment was very productive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.