Talk:Division algorithm

message: I have seen the algorithm
\frac{1}{1-x}
 = \frac{1+x}{1-x^2}
 = \frac{(1+x)\cdot(1+x^2)}{1-x^4}
 = \frac{(1+x)\cdot(1+x^2)\cdot(1+x^4)}{1-x^8}
etc. When x^{2^n} becomes small enough, then the denominator is set to 1 and the algorithm terminates.
Does someone know its name or better a reference?
I believe it was used in an IBM machine. HenningThielemann (talk) 16:45, 25 March 2010 (UTC)
replies: text: Why is this referred to as being related to the binomial theorem? (I don't see a source for making that connection.) What it relies upon is the difference of two squares, which is quite different! If I was to suggest an English name for the method (the elementary algebraic identity  (a-b)(a+b) = a^2 - b^2  doesn't seem to have much of an established name in English, so it'd be hard to name the method after that), then "squaring away the error" is probably appropriate. Alternatively, the method can be viewed as an application of the formula for the sum of a geometric series, but that is again not the same as the binomial theorem. 130.243.94.123 (talk) 12:07, 4 June 2015 (UTC)
thread_title: Division based on a binomial theorem

message: This page actually discusses implementing division algorithms for digital circuits (i.e. a divider in a microprocessor math unit). Many other types of division also exist for electronics that are not addressed in this page (e.g. voltage divider, current divider, etc)
===Voting===
:Add *Support or *Oppose followed by an optional one sentence explanation, then sign your vote with 
~~~~''
*Support Jpape 07:21, 6 December 2005 (UTC)
thread_title: Requested move

message: :Add *Support or *Oppose followed by an optional one sentence explanation, then sign your vote with 
~~~~''
*Support Jpape 07:21, 6 December 2005 (UTC)
thread_title: Voting

message: *It's consistent with Adder (electronics), for whatever that's worth.  Though I do agree with the proposal to some extent.  --Interiot 07:45, 6 December 2005 (UTC)
thread_title: Discussion

message: The algorithm as described is inefficient. If the only two digits used are 1 and -1, the latter can be represented as 0, then:
Q = P (positive term)
N (the negative term) = ~P = -P-1
Two's complement of N (-N) = P+1
The sum of P and -N = 2*P + 1.
Therefore, the least significant digit for the quotient is always 1, and the overall precision is one less bit than expected.
A better implementation of non-restoring division is to allow digits 0, 1, and -1 (this will require twice the number of state devices for the quotient, though):
 P[0] = N
 i = 0
 while(i<n)
 {
   if(abs(P[i]) < 0.25) q[n-(i+1)] = 0;
   else if(P[i] >= 0) { q[n-(i+1)] =  1; P[i+1] = 2*P[i] - D; }
   else               { q[n-(i+1)] = -1; P[i+1] = 2*P[i] + D; }
   i++;
 }
Comparing with 0.25 is cheap: the absolute value is less than 0.25 if the two most significant bits of mantissa are equal to the sign bit.
((That's essentially SRT now, isn't it? -Ben))
: what about an extra section with SRT explanation and code example? -- 89.247.39.228 (talk) 18:41, 29 July 2008 (UTC)
thread_title:  Non-restoring division 

message: I'm not smart enough to understand any of the formulas on this article and I don't know which this is, but I do know that this is not the best routine - but it is a simple example of integer division in compilable C code. I only post it to talk page because I came here looking for such a thing and couldn't find it, so when I figured it out, I post it here. Just delete it if you don't like. I won't be offended. Maybe one person will be helped.
        int Q=0;
        int N=100000000;   //1.00000000
        int D=3183; //0.3182 (in other words, 1/PI) - the result should be pi.
        int mult=65536;
        while(mult>0)
        {
                if((N-(D*mult))>=0)
                {
                        N=N-(D*mult);
                        Q=Q+mult;
                }
                mult=mult>>1; //Right-shift by 1 bit..
        }
	printf ("Q=%d, R=%d\n",Q,N);
Anyway, after the loop is done, N contains the remainder.
This code is very limited and for illustration only. PS: C code and this page don't get along.
64.146.180.232 (talk) 03:53, 11 July 2008 (UTC)
thread_title:  Simple compilable code sample - for demonstration only 

message: Hi,
I just fixed some issues in the pseudocode. As all pseudocode it is untestable, and thus likely to be buggy. Below you find a C implementation of same complexity for the restoring unsigned integer division, but compile- and verifiable:
 /** save as d u_div.c -- compile & test by typing:
  * gcc -Wall -g -O0 -o u_div u_div.c && ./u_div 100 10
  */
 #include <stdio.h>
 #include <stdlib.h>
 
 long long i_div (long long bit_width, long long P, long long D)
 {
 	long long P_sub_D;
 	long long q = 0;
 	long long i;
 
 	D <<= bit_width;
 	for (i=bit_width-1; i>=0; i--) {
 		P = 2 * P;
 		P_sub_D = P - D;
 		if (P_sub_D >= 0) {
 			P = P_sub_D;
 			q |= (1 << i);
 		}
 	}
 	return q;
 }
 
 int main (int argc, char **argv)
 {
 	long long P = strtoll(argv[1], NULL, 0);
 	long long D = strtoll(argv[2], NULL, 0);
 	long long bit_width = 16;                  // word width
 	printf("%lld / %lld = %lld\n", P, D, i_div(bit_width, P, D));
 	return 0;
 }
Hope this helps to make the situation better,
-- 89.247.39.228 (talk) 18:36, 29 July 2008 (UTC)
thread_title:  pseudo code untestable 

message: The distinctive difference between Non Restoring and SRT division is incorrectly described as the use of a table.
Radix 2 division uses digits +1, 0 and -1, don't need any table and deliver one result bit per iteration. Higher radix SRT division (for example Radix 4, digits +2,+1,0,-1 and -2) need a table as digit selection depends on the divisor and remainder (not dividend, except for the first iteration) values.
The main advantage of SRT division for electronic circuit design is that an approximate value of the remainder is used for digit selection at each iteration, instead of the exact value. Faster, non carry propagating adders can be used.
That in turn enables higher precision, shorter cycle time or the calculation of several bits par cycle either by using a high radix divider or by combining several Radix 2 cells  — Preceding unsigned comment added by 194.98.70.14 (talk) 11:13, 16 June 2011 (UTC)
replies: text: I think we really need a citation for SRT division, if you could suggest a good one that would really help. Dmcq (talk) 11:27, 16 June 2011 (UTC)
thread_title:  SRT Division 

message: This new section stuck at the beginning is basically the same algorithm as the restoring division one. However neither of them is cited and I think the actual code in the restoring division one is a bit clunky. So I think I'll add citation needed to both and then we can keep the one that is cited first and remove the other one. Dmcq (talk) 23:04, 20 February 2012 (UTC)
thread_title:  Integer division (unsigned) with remainder 

message: I recently moved Division (digital) to Division algorithm, replacing a former redirect to Euclidean division. In retrospect this was too bold - I belatedly discovered that the term "division algorithm" is used (idiosyncratically) to refer to the theorem outlined at Euclidean division as well as to algorithms that perform division. I've fixed all the links now, and I think it makes sense for the primary topic to refer to the more obvious subject. I think it also makes sense to have a general article about division algorithms, similar to multiplication algorithm, rather than one solely concerned with division algorithms used in digital circuits. Unlike Adder (electronics), dividers are too complex to describe in detail at the logic or gate level, so most of what's here is already pseudocode suitable for implementation in a variety of hardware, software, and other settings. As such few changes were needed except to add some info on basic slow algorithms not normally used in implementations, which I think help in any case to introduce the notation and serve as a behavioural reference for the more complex ones. Feedback is welcome. Dcoetzee 12:31, 23 September 2012 (UTC)
thread_title:  Recent moves 

message: The article claims:
::X_0 = {48 \over 17} - {32 \over 17} D \,. 
:Using this approximation, the error of the initial value is less than
::\vert \epsilon_0 \vert \leq {1 \over 17} \approx 0.059 \,.
It seems that the error is actually bounded by 2/17, not 1/17. Measuring error at the two endpoints and the single local maximum, we find error is 1/17 at D=1, 2/17 at D=1/2, and 48/17 - 2\sqrt{32/17} or about 1.36/17 at the local maximum. This would imply the estimate for S may be incorrect (should have \log_2 17/2 rather than \log_2 17).
I also attempted a derivation of the optimal linear approximation in terms of max error. The max error for f(x)=a-bx is given by \max\{a-2\sqrt{b}, 2-a+b/2, 1-a+b\}, assuming it crosses 1/x twice in [0.5, 1]. The planes 2-a+b/2 and 1-a+b have a difference of 1-b/2, so they intersect where b = 2; where b < 2, the plane 2 - a + b/2 is higher, and where b > 2, the plane 1-a+b is higher.
When b < 2 the functions a-2\sqrt{b} and 2 - a + b/2 intersect where a = b/4 + \sqrt{b} + 1, along which max error is 1 - \sqrt{b} + b/4, which is minimized at endpoint b=2, giving the approximation f(x) = 3/2 + \sqrt{2} - 2x which has max error of 3/2 - \sqrt{2} or about 0.0857864.
When b > 2 the functions a-2\sqrt{b} and 1-a+b intersect where a = b/2 + \sqrt{b} + 1/2, along which max error is 1/2 (-1 + \sqrt{b})^2, which is again minimized at endpoint b=2, giving the same result as before.
In short the optimal linear approximation appears to be f(D) = 3/2 + \sqrt{2} - 2D with a max error of 3/2 - \sqrt{2} or about 0.0857864 < 1/11, as compared to 2/17 or 0.117647 < 1/8 for the one given in the article. Not only is the approximation better, but it saves a multiply since 2D is just a shift. Obviously I can't add this to the article since it's original research, but it does suggest that the approximation we're suggesting is suboptimal. Even rational approximations of this solution are better than the suggested one (e.g. 50/17 - 2D gives a max error of 0.112749, 32/11 - 2D gives 0.0909091, and 102/35 - 2D gives 0.0858586).
Note that the average error squared given by the integral \int\limits_{1/2}^{1} (f(x)-1/x)^2\, dx \approx 0.00170884 is not quite as good as for the currently supplied function which gives 0.00155871, so there is that tradeoff. However if there is no test for convergence and the number of iterations is determined solely by the desired precision, then average error doesn't help. Dcoetzee 05:26, 24 September 2012 (UTC)
replies: text: Okay, I've now found a source "Optimal initial approximations for the Newton-Raphson division algorithm" (1994) that I may be able to use to update the article appropriately. Its treatment is more sophisticated than mine (my approach seems to have been that of ref [10], Swartzlander's "Overview of computer arithmetic" - it turns out limiting max error in the initial estimate doesn't necessarily best limit relative error in the result). Dcoetzee 06:20, 24 September 2012 (UTC)

text: Update I just noticed the thread farther up the page concerning the choice of initialization, and mentioning the same exact paper... silly me not reading the full talk page first. Regardless the article as it stands is in error, not because the initial estimate is a bad one but because it gives the wrong max error and the analysis is not sophisticated enough to explain why it's good - the current simple analysis based on max error of the initial estimate would favour an estimate with better max error. Clearly a more nuanced discussion will be required to inform readers about tradeoffs in the choice of the initial estimate. I'll try to fill it out once I get access to the paper. Dcoetzee 06:34, 24 September 2012 (UTC)
thread_title:  Problem with Newton-Raphson initial estimate 

message: I have tagged this section  not only because it is a stub, but mainly because some important material is completely lacking in the article, mainly the problem of correct rounding. The section should mention the norm IEEE 754 and describe how the algorithms have to be modified (and have been) to follow this norm. Also, and this is related, this section should mention Intel bug. D.Lazard (talk) 09:52, 24 September 2012 (UTC)
thread_title:  Section Rounding error 

message: This algorithm for signed integers produces quotient bits of 0 and 1. The dividend has double the number of bits as the divisor, quotient, and remainder. In this example, a 32 bit dividend is divided by a 16 bit divisor, producing a 16 bit quotient and remainder. There are some pre and post processing steps used to handle negative divisors, and clean up of the remainder. The overflow logic is not included.
#include <stdio.h>
/* signed integer non restoring divide, no overflow check */
/* quotients round down towards negative infinity */
/* shorts are assumed to be 16 bits, ints are assumed to be 32 bits */
/* unsigned variables are used to prevent sign extension by C */
unsigned int divide(unsigned int dvnd, unsigned short dvsr)
{
int i;
unsigned short qb;                  /* quotient bit */
unsigned short q;                   /* quotient */
    if(dvsr>>15)                    /* if dvsr negative, decrement dvnd */
        dvnd -= 1;
    q = 0;
    qb = (dvnd>>31) == (dvsr>>15);  /* if signs same, qb = 1, else qb = 0 */
                                    /*  and if signs same, subtract, else add */
    i = 0;
    while(1){
        dvnd = qb ? dvnd - ((unsigned int)dvsr<<16) : dvnd + ((unsigned int)dvsr<<16);
        qb = (dvnd>>31) == (dvsr>>15);
        q = (q<<1) | qb;
        if(++i == 17)
            break;
        dvnd <<= 1;
    }
    if((dvnd>>31) != (dvsr>>15))    /* if remainder wrong sign, add dvsr */
        dvnd += (unsigned int)dvsr<<16;
    if(dvsr>>15)                    /* if dvsr negative, increment remainder */
        dvnd += 0x10000;
    return(dvnd | q);
}
int main(){
char buffer[80];
unsigned int m0, m1;
    while(1){
        printf("enter numbers in hex: ");
        fgets(buffer, sizeof(buffer), stdin);
        if(2 != sscanf(buffer, "%x %x", &m0, &m1))
            break;
        m0 = divide(m0, (unsigned short)m1);
        printf("quotient is: %4x  remainder is %4x\n", m0 & 0xffff, m0>>16);
    }
    return 0;
}
Rcgldr (talk) 16:39, 22 November 2013 (UTC)
thread_title:  Alternate version of non-restoring division 

message: Labelling some algorithms as "slow" and others as "fast" can be a bit misleading, particularly from the perspective of 
hardware implementation.
There are additive methods (non restoring, SRT) and multiplicative ones (newton, goldshmidt...).
The multiplicative methods converge in fewer steps, but these steps are far more complex, because a multiplication is more complex than an addition.
The multiplicative methods also require an initial approximation wich incurs some delay and area.
The Goldshmidt method purpose' is also related to multiplication speed.
It has some issues with precision and accumulating errors, but it has the benefit of using two independant 
multiplications per iteration, instead of dependant ones for Newton. It enables to use two parallel multipliers or one
pipelined two-cycles multiplier.  — Preceding unsigned comment added by Temlib (talk • contribs) 23:06, 24 July 2014 (UTC)
thread_title:  Fast and Slow division 

message: In this text :
 Express D as M × 2e where 1 ≤ M < 2 (standard floating point representation)
 D' := D / 2e+1   // scale between 0.5 and 1, can be performed with bit shift / exponent subtraction
 N' := N / 2e+1
 X := 48/17 - 32/17 × D'   // precompute constants with same precision as D
    // can be precomputed based on fixed P
            X := X + X × (1 - D' × X)
 end
 return N' × X
:For example, for a double-precision floating-point division, this method uses 10 multiplies, 9 adds, and 2 shifts.
I'm just curious on how many iterations that are performed in that case? Since the algorithm does two MUL and two ADD each iteration, I assume that only five iterations are needed then? Correct?  — Preceding unsigned comment added by Eleteria (talk • contribs) 09:35, 28 February 2016 (UTC)
replies: text:  I only see 4 iterations; 1 multiply, 1 add, and 2 shifts going in, 4 iters for 8 multiplies and 8 adds, and one multiply coming out. Glrx (talk) 22:45, 3 March 2016 (UTC)
thread_title:  Regarding the Newton-Raphson algorithm  

message: should you not mention the recursive one, for non-negative integers ?
 div(a,b) 
   a < b => 0
   else  => 1 + div(a-b,b)  — Preceding unsigned comment added by 87.240.239.70 (talk) 18:37, 23 June 2016 (UTC)
replies: text: This algorithm is correct, but is exponentially slower than long division (the number of steps is equal to the quotient, while for the usual algorithm the number of steps is the number of digits of the quotient). Therefore, it is never used and does not deserve to be mentioned. D.Lazard (talk) 20:28, 23 June 2016 (UTC)
thread_title: Recursive algorithm?

message: In this section it states "while making use of the second expression, one must compute the product between X_i and (2-DX_i) with double the required precision (2n bits)." It appears to me that the required precision would be n+1 bits. Rcgldr (talk) 18:21, 28 December 2016 (UTC)
thread_title:  Newton–Raphson - number of significant bits 

message: Let divide 123 by 45. Will have 123 / 4 to got quotient 30. Then divide 30 by 15. Will have 30 / 15 to got quotient 2. It is divide the number, answer is quotient 2. The only except divide by numbers 11, 111, 1111 ... If one try divide 22 by 11 he will got exact 2. Now let such calculate do the exact number to select. That sequence of digit 1 should to select on divisor value. See further a sample how to: 123 / 4 (the number 4 is !digit! of number 45). To the end, a digits of divisor should prepared append a digit 1. Then digit 5 should have 15. This easy can binary divide algorithm. I would accept any note on hardware implementation of "chipdie" divide.95.78.44.26 (talk) 08:46, 5 April 2017 (UTC)
thread_title:  Chipdie divide algorithm the most like multiply by design. Each digit of divisor is prepared doing high bit set. There is sample next. 

message: The same improvements which we made to Newton-Raphson division can be made to Goldschmidt division.
:Shift N and D so that D is in [1/2, 1]
: F := \frac{140}{33} + D \cdot (\frac{-64}{11} + D \cdot \frac{256}{99})  /* Puts D into [98/99, 100/99].
:Begin loop
:: N := N \cdot F 
:: D := D \cdot F 
:: E := 1 - D 
::If E is indistinguishable from 0, then output N and stop.
:: F:= 1 + E + E \cdot E  /* Cubes the error E of D.
:End loop.
OK? JRSpriggs (talk) 08:14, 29 May 2018 (UTC)
thread_title:  Same improvements to Goldschmidt division 

message: It would be nice if the article contained a concise comparison of the division methods: are some methods "better" than some others, in some respect? Currently there is very little that would help see how better and better division methods were developed, and what is the state of the art currently. At its present state the article is just a long list of different division algorithms.  — Preceding unsigned comment added by 87.92.32.62 (talk) 11:32, 28 April 2019 (UTC)
replies: text: When working with fixed-length numbers (floating point as well as fixed-point arithmetic), the best algorithm strongly depends of the used hardware and software technology. It is thus difficult to give more details in an encyclopedic article. However, when working with arbitrary-length, the final answer has been given, and I have added it to the lead: division and multiplication have the same computational complexity (up to a constant factor), and a faster division algorithm relies on a faster multiplication algorithm. Some more details would be welcome, but we must wait that a competent editor will be willing to improve the article. D.Lazard (talk) 13:40, 28 April 2019 (UTC)
thread_title:  Summary and Comparison 

message: The article must include details of galley division. —Jencie Nasino (talk) 02:20, 16 August 2019 (UTC)
replies: text: I put a link to that article in the "See also" section. There is no need to describe the method in detail again here. And since it is no longer used by anyone, it is notable only for historical reasons. JRSpriggs (talk) 04:58, 17 August 2019 (UTC)
thread_title:  Galley division 

message: An editor has asked for a discussion to address the redirect Anderson Earle Goldschmidt Powers algorithm. Please participate in the redirect discussion if you wish to do so.  D.Lazard (talk) 14:30, 24 October 2019 (UTC)
thread_title:  "Anderson Earle Goldschmidt Powers algorithm" listed at [[Wikipedia:Redirects for discussion|Redirects for discussion]] 

message: Where is this relation coming from? I have seen R[i+1] = R[i] + q[i] * D * B^i, but not this one.  — Preceding unsigned comment added by Mikecondron (talk • contribs) 19:07, 4 February 2020 (UTC)
thread_title:  Recurrence relation at top of "Slow Division Methods"