Re: Public domain algorithm for arbitrary-precision fast division?
- From: "David N. Williams" <williams@xxxxxxxxx>
- Date: Sun, 08 Mar 2009 10:36:58 -0400
Helmut Jarausch wrote:
> Greg Lovern wrote:
>> I'm looking for an algorithm for doing arbitrary-precision (bignum)
>> division more quickly than the "long division" method taught to school
>> children.
>
> Depending on the number of digits there are methods which are
> dramatically faster.
> AFAIK, the fastest method uses fast multiplication by two steps.
> To compute a/b do
>
> 1.) compute 1/b without any division. That can be done by Newton's method
> applied to the function f(x) = 1/x - b giving the quadratic
> convergent
> iteration x<- x*(2-b*x) (Use a standard precision (64-bit
> hardware floating point)
> approximation to 1/b as initial value of x).
> Quadratic convergence means: the number of exact digits doubles in
> each iteration!
>
> 2) compute a*(1/b)
>
> Now, you have to use a fast multiplication. There are many of them
> e.g. by using the Fast Fourier Transform
> or
> http://mathworld.wolfram.com/KaratsubaMultiplication.html
> or
> http://en.wikipedia.org/wiki/Schönhage-Strassen_algorithm
>
> and probably some more.
>
> Google for multiple precision arithmetic.
David N. Williams wrote:
> [...]
> I'm not expert on the literature, but my impression from my own
> searches is that Knuth's division algorithm is likely to be
> about as fast as the current state of the art, for general
> purposes. I'd certainly like to know if that's not true!
Dave Rusin sent a private response to my post, mentioning
Newton's method and kindly explaining a bit about FFT
multiplication, which made me realize that my impression was, to
say the least, fuzzy.
So I tried to get a more precise feel for when Knuth's
multiplication and division algorithms are better by looking at
the documentation for GMP 4.2.4 (GNU Multiple Precision
Arithmetic Library):
http://gmplib.org/
http://gmplib.org/manual/Multiplication-Algorithms.html#Multiplication-Algorithms
http://gmplib.org/manual/Division-Algorithms.html#Division-Algorithms
If I read that correctly, Knuth is thought to be roughly better
for multiplication when the number of limbs* is below
MUL_KARATSUBA_THRESHOLD, and below DIV_DC_THRESHOLD for
division. The latter is said to be about twice the former. And
according to the source file gmp-impl.h:
/* If MUL_KARATSUBA_THRESHOLD is not already defined, define it to a
value which is good on most machines. */
#ifndef MUL_KARATSUBA_THRESHOLD
#define MUL_KARATSUBA_THRESHOLD 32
#endif
It appears that MUL_FFT_THRESHOLD is roughly 128 * 3 * 10 =
3,840, with other non-Knuth methods preferred between 32 and
3,840.
For 2N by N limb division, Newton's method sets in for "large to
very large N". On the other hand, on some machines it is faster
than hardware division, and is then used for the machine-size
division steps in the algorithms.
(Hope I've got all of this right!)
I've never used it, but I must say that from the outside, GMP
seems pretty impressive. It appears to have undergone a lot of
theoretical and empirical algorithm analysis, tuned for various
cpus.
The OP says he doesn't want "... any GNU or similar
restrictions." But I wonder if GMP wouldn't be okay -- since
GMP is under the LGPL, not the GPL, his software wouldn't suffer
unusual restrictions by linking to the GMP library.
I'd appreciate corrections to any of the above info. And what
are the disadvantages of GMP?
-- David
* A "limb" stores a base 2^[n-1] digit, where n is the number of
bits in a machine word.
.
- Follow-Ups:
- Re: Public domain algorithm for arbitrary-precision fast division?
- From: Helmut Jarausch
- Re: Public domain algorithm for arbitrary-precision fast division?
- References:
- Public domain algorithm for arbitrary-precision fast division?
- From: Greg Lovern
- Re: Public domain algorithm for arbitrary-precision fast division?
- From: Helmut Jarausch
- Public domain algorithm for arbitrary-precision fast division?
- Prev by Date: if you could send solutions to me
- Next by Date: Draft paper submission deadline extended (will not be extended further): TMFCS-09
- Previous by thread: Re: Public domain algorithm for arbitrary-precision fast division?
- Next by thread: Re: Public domain algorithm for arbitrary-precision fast division?
- Index(es):
Relevant Pages
|