views:

219

answers:

2

Guys I'm working on class called LINT (large int) for learning purposes, and everything went ok till know. I'm stuck on implementing operator/(const LINT&). The problem here is that in when I want to divide LINT by LINT I'm getting into recursive fnc invocation i.e:

//unfinished
LINT_rep LINT_rep::divide_(const LINT_rep& bottom)const
{
typedef LINT_rep::Iterator iter;
 iter topBeg = begin();
 iter topEnd = end();
 iter bottomBeg = bottom.begin();
 iter bottomEnd = bottom.end();
LINT_rep topTmp;//for storing smallest number (dividend) which can be used to divide by divisor
 while (topBeg != topEnd)
 {
  topTmp.insert_(*topBeg);//Number not large enough add another digit
 if (topTmp >= bottom)
 {//ok number >= we can divide 
     LINT_rep topShelf = topTmp / bottom;//HERE I'M RUNNING INTO TROUBLE
 }
 else
 {


 }
 ++topBeg;
 }
 return LINT_rep("-1");//DUMMY
}

What I'm trying to do is to implement this as if I would divide those numbers by hand, so for example having for a dividend 1589 and for divisor 27 I would go like so:

  1. check if first digit is >= divisor and if so divide
  2. if not add to the first digit another digit and check if a > b

At some point it will be bigger (in simplified scenario) and if so I have to divide but in this case I'm running into recursive call and I have no idea how to break it.
One note: as a tmp I have to use LINT instead of int for example because those numbers my not fit into int.
So generally what I'm asking for is there any other way to do division? Or maybe there is false logic in my thinking (quite possible). Thank you.

+1  A: 

First, please don't work on such a class. Use CGAL's big int, and there was some boost bigint submission I think, also, there're about three or four other popular implementations.

Second, the division algorithm is described here: http://en.wikipedia.org/wiki/Long_division

[EDIT] Correct way to do it: Digit k of the result (C): if first digit (from left) of A, call it A[nA-1] is smaller than B[nB-1], write zero into C[k]. k-- (move to next digit). Otherwise, you seek maximum digit C[k] so that C[k]*B*10^k <= A. That is done in a loop. Actually, the previous sentence is a private case of this one. But it is not yet finished. You do A-=C[k]*B*10^k (the substracted part was zero otherwise). Only then,

k-- (next digit). Loop until k==0.

No need for recursion. Just two nested loops.

One loop for k (digit of the result), one loop for finding every digit, one loop (near it) for substracting (the -= operator).

Pavel Radzivilovsky
Oh, sorry, then it should be properly tagged. I thought you are asking an 'industrial' question.
Pavel Radzivilovsky
@Pavel but thanks for your edit
There is nothing we can do
@Pavel could you please explain it using example (1589 / 27)?
There is nothing we can do
@Pavel I've asked you to explain your answer using an example because I feel that you didn't give in your original answer all the steps necessary.
There is nothing we can do
@Pavel continue or you've made mistake in one of your steps.
There is nothing we can do
@Pavel correct me if I'm wrong but first time k should be incremented not decremented.
There is nothing we can do
1. No, i meant k-- as I wrote.2. There's a difference between practice and real development. The wikipedia article contains an in-depth description of Omar Khayam's algorithm. It is what I used to write my code. BTW, this code here is not tested, but I too implemented it once and it worked. 3. Guys, please remove off-topic comments. People might actually google for this information later.
Pavel Radzivilovsky
@Pavel as I've said in one of my previous response, thank you for your answer (edited part), didn't mean to be unplesant but I've got this unpleasant vibe out of your answer and I didn't like that.
There is nothing we can do
@Pavel: you're right. I removed my comments. I've made my point that there's no call for insults when commenting on those who *try* to answer your questions.
jalf
Thanks jalf. @A-ha, I am certainly not polish, and my (proud) race is totally irrelevant, please, remove all off-topic comments and I will be ready to discuss the algorithm further. I had certainly no indention to insult anybody, that's not why I am here.
Pavel Radzivilovsky
@I have no problem with removing any comments that are off topic which I'm going to do now. BTW I partially implemented the algorithm you've showed in your answer but as I've said I'm using in case A[0] < B ++k; What I do not understand from your explanation (but I hope you will be willing to share it) that you are decrementing k all the time but at what value this k starts? If you could do (step by step) this: 1589 / 27 I would be more than grateful
There is nothing we can do
@Pavel edit to last comment what I meant by step by step is not actual code (this I want to do myself), I would like to see what happens in each step as you going through your algorithm.
There is nothing we can do
@Pavel glad you showed me what's your word really means. But no bother, I did it (and there is a mistake in your answer).
There is nothing we can do
LOL, I actually wanted to post a walkthru, but okay, I won't.
Pavel Radzivilovsky
LOL I tell you something why don't you go and fuck yourself? And do not mistake proud for arogance as you did with race instead of nation you arogant buffon.
There is nothing we can do
And as I've already said I did it without you so what for would I want your "walkthrough"? Better go and fuck yourself you arogant prick. And if you don't understand why I call you with these names ask me and I would be more than happy to explain it to you.
There is nothing we can do
@Pavel Ty prostaku z Podwiśniewa Wielkiego.
There is nothing we can do
So far it's rather amusing, please continue. Also, I don't understand polish, I am not polish, I do not leave in poland, I have no polish family members, and even my last name is not polish.
Pavel Radzivilovsky
@Pavel first if you do not understand polish how do you know that's polish (sure you have a polish friend and he told you), second and most important yes, your name is polish even the first part - Paweł Radziwiłowski in its original form, but I feel sorry for you that you didn't know it and that you don't know anything about your name.
There is nothing we can do
@Pavel and I wouldn't use bad language but you by your pig arrogance provoked me to do so.
There is nothing we can do
Jew? Even worse.
There is nothing we can do
sure. we are very bad, arrogant and rude people ;)
Pavel Radzivilovsky
@Pavel not all, but you definitely are in very subtle way.
There is nothing we can do
Thanks, A-ha, for #1 anecdote of all my stackoverflow membership.
Pavel Radzivilovsky
+1  A: 

When doing your part (1) you can't divide; you have to repeatedly subtract, or guess to subtract a multiple, just like when you do it by hand. You can 'guess' more effectively by setting upper and lower bounds for the multiple required and doing a binary-chop through the range.

I've done a similar thing myself; it's a handy exercise to practice operator overloading. I can supply a snippet of code if you like, although it uses arrays and half-baked exceptions so I hesitate to offer it up before the expert readers of this site.

Brian Hooper
@Brian +1 thank you for your answer, just chewing it. Probably will need your help with this.
There is nothing we can do