# HP Forums

Full Version: BCD Division
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

One time 'quiche eater' seeks a (clear) description of the algorithm to perform the division of two (unpacked) BCD numbers (10 digit mantessa + 2 digit exponent - now where might you have seen that format before).

I know it is shift and subtract but I can't remember exactly how it was supposed to work - I do remember it wasn't obvious!

Mike T.

PS I don't want to use an existing library like BIGINT or similar.

I'd start at http://www2.hursley.ibm.com/decimal. The web page's author is the expert on decimal arithmetic.

Many thanks, I'd had found the site earlier but not that page - must have missed it.

I'll have to read it slowly to make sense of it properly (and I guess I'll have to create my own flowchart or structure diagram as there wasn't one) but I think that given a few days, and no too many late nights I should be able to cut some useful code...

Mike T.

to perform U/V where these are bcd floats. first unpack the mantissas, exponents and signs so that you have two positive mantissas u/v. start by settings the exponent of the result, w, to be exp(U) - exp(V). now perform the u/v division as follows:

(1)	ensure that u >= v. if not, shift u left 1 packed bcd digit and decrement w, if quotient = 0, otherwise shift quotient left 1 digit.. goto (1)
(2) while u >= v, perform u = u - v. count the number of subtractions, q. this is the next digit of the answer. goto (1)

there’s a bit more to it than this but essentially you are shifting until you’ve got something to subtract then you subtract all you can at each digit and put the number of subtractions into the answer and continue until you’ve got a full house of quotient mantissa.

lastly fix up the signs and exponent of the quotient.

i wrote some `C’ code one for this here:
http://www.voidware.com/bcdfloats.htm

Now I can make a connection - I did find your web site a while ago, and I should have guessed you would be reading these posts! Unfortunately I haven't yet got the hang of unravelling somebody else's code and have been having more than a little difficulty sorting out what was going on as your routines use packed BCD which makes life a little more interesting. Thank you for the precis.

Mike T.

those routines are a bit old. i have a new 20 digit bcd library now. i keep meaning to put this up for download.