home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!uwm.edu!csd4.csd.uwm.edu!markh
- From: markh@csd4.csd.uwm.edu (Mark)
- Newsgroups: comp.programming
- Subject: Re: Arbitrary precision with large bases
- Date: 8 Jan 1993 01:00:14 GMT
- Organization: Computing Services Division, University of Wisconsin - Milwaukee
- Lines: 52
- Message-ID: <1iijmuINNmh4@uwm.edu>
- References: <1icne6INNpjb@uwm.edu> <martin.726311424@bert>
- NNTP-Posting-Host: 129.89.7.4
-
- In article <martin.726311424@bert> martin@math.rwth-aachen.de ( Martin Schoenert) writes:
- >Base 2^32 has the following advantages.
- >
- >a) the larger the base the fewer digits any given integer has in this
- > base. Since (for example) the performance of the multiplication
- > is proportional to the product of the number of digits of the
- > operands it is always a win to use larger bases (though the win
- > with base 2^32 over base 10^9 is only about 12.5%).
- >
- >b) many modern processors have a 32bit x 32bit -> 64bit instruction.
- > This instruction cannot directly be used from within C, so one needs
- > a little bit of assembler. However if one does that one gets
- > AB div BASE and AB mod BASE with one instruction.
-
- These are disadvantages too. If you want extra room for work space in
- intermediate calculations (such as adding two digits in base 2^N) in a portable
- manner, you should really use 2^31 or 2^30 as the base instead.
-
- The advantages of 2^30 (or 10^9) are that (1) it's (almost) equal to a perfect
- power of 2, it (almost) factors evenly into two equal factors (both (almost)
- equal to 2^15) which comes in handy for multiplication, and it's a perfect
- cube, which comes in handy for division.
-
- >With respect to aspect (2). Knuth shows that if you normalize the
- >divisor such that its leading digit is larger than or equal to BASE/2 and
- >compute an appoximation of a digit of the quotient by dividing the
- >leading three digits of the remainder by the leading two digits of the
- >divisor the approximation is at most too large by 1 *independent* of the
- >base. It is easy to detect if the digit is too large and make the
- >appropriate corrections.
-
- Another way to normalize, with a base that is a perfect cube (N^3) is to
- (1) normalize the divisor so that its leading digit lies between N and N^2 - 1,
- and (2) always arrange so that the dividend's leading digit never exceed N
- times the divisor's leading digit (divide by N if need be).
-
- This will likewise guarantee that the approximation never be off by more than
- 1.
-
- Also, it's easier to underestimate the quotient's digit, rather than
- overestimate it. Then division just consists of shifts and subtracts -- no
- corrections and no backtracking. The basic algorithm becomes:
-
- remainder = dividend; quotient = 0;
- while (1) {
- if (divisor > remainder) {
- multiply quotient and remainder by N
- } else {
- digit = leading_digit(remainder) / (leading_digit(divisor) + 1)
- remainder -= digit*divisor; quotient += digit
- }
- }
-