Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{mpz} --- |
| 2 | GNU MP library for arbitrary precision arithmetic.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{builtin}{mpz} |
| 4 | |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 5 | \modulesynopsis{Interface to the GNU MP library for arbitrary |
| 6 | precision arithmetic.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 3e43d83 | 1995-08-10 14:21:49 +0000 | [diff] [blame] | 9 | This is an optional module. It is only available when Python is |
| 10 | configured to include it, which requires that the GNU MP software is |
| 11 | installed. |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 12 | \index{MP, GNU library} |
| 13 | \index{arbitrary precision integers} |
| 14 | \index{integer!arbitrary precision} |
Guido van Rossum | 3e43d83 | 1995-08-10 14:21:49 +0000 | [diff] [blame] | 15 | |
| 16 | This module implements the interface to part of the GNU MP library, |
| 17 | which defines arbitrary precision integer and rational number |
| 18 | arithmetic routines. Only the interfaces to the \emph{integer} |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 19 | (\function{mpz_*()}) routines are provided. If not stated |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 20 | otherwise, the description in the GNU MP documentation can be applied. |
| 21 | |
| 22 | In general, \dfn{mpz}-numbers can be used just like other standard |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 23 | Python numbers, e.g.\ you can use the built-in operators like \code{+}, |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 24 | \code{*}, etc., as well as the standard built-in functions like |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 25 | \function{abs()}, \function{int()}, \ldots, \function{divmod()}, |
| 26 | \function{pow()}. \strong{Please note:} the \emph{bitwise-xor} |
| 27 | operation has been implemented as a bunch of \emph{and}s, |
| 28 | \emph{invert}s and \emph{or}s, because the library lacks an |
| 29 | \cfunction{mpz_xor()} function, and I didn't need one. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 30 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 31 | You create an mpz-number by calling the function \function{mpz()} (see |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 32 | below for an exact description). An mpz-number is printed like this: |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 33 | \code{mpz(\var{value})}. |
| 34 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 36 | \begin{funcdesc}{mpz}{value} |
| 37 | Create a new mpz-number. \var{value} can be an integer, a long, |
| 38 | another mpz-number, or even a string. If it is a string, it is |
| 39 | interpreted as an array of radix-256 digits, least significant digit |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 40 | first, resulting in a positive number. See also the \method{binary()} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 41 | method, described below. |
| 42 | \end{funcdesc} |
| 43 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 44 | \begin{datadesc}{MPZType} |
| 45 | The type of the objects returned by \function{mpz()} and most other |
| 46 | functions in this module. |
| 47 | \end{datadesc} |
| 48 | |
| 49 | |
Fred Drake | af8a015 | 1998-01-14 14:51:31 +0000 | [diff] [blame] | 50 | A number of \emph{extra} functions are defined in this module. Non |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 51 | mpz-arguments are converted to mpz-values first, and the functions |
| 52 | return mpz-numbers. |
| 53 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 54 | \begin{funcdesc}{powm}{base, exponent, modulus} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 55 | Return \code{pow(\var{base}, \var{exponent}) \%{} \var{modulus}}. If |
| 56 | \code{\var{exponent} == 0}, return \code{mpz(1)}. In contrast to the |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 57 | \C{} library function, this version can handle negative exponents. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 58 | \end{funcdesc} |
| 59 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 60 | \begin{funcdesc}{gcd}{op1, op2} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 61 | Return the greatest common divisor of \var{op1} and \var{op2}. |
| 62 | \end{funcdesc} |
| 63 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 64 | \begin{funcdesc}{gcdext}{a, b} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 65 | Return a tuple \code{(\var{g}, \var{s}, \var{t})}, such that |
| 66 | \code{\var{a}*\var{s} + \var{b}*\var{t} == \var{g} == gcd(\var{a}, \var{b})}. |
| 67 | \end{funcdesc} |
| 68 | |
| 69 | \begin{funcdesc}{sqrt}{op} |
| 70 | Return the square root of \var{op}. The result is rounded towards zero. |
| 71 | \end{funcdesc} |
| 72 | |
| 73 | \begin{funcdesc}{sqrtrem}{op} |
| 74 | Return a tuple \code{(\var{root}, \var{remainder})}, such that |
| 75 | \code{\var{root}*\var{root} + \var{remainder} == \var{op}}. |
| 76 | \end{funcdesc} |
| 77 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 78 | \begin{funcdesc}{divm}{numerator, denominator, modulus} |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 79 | Returns a number \var{q} such that |
| 80 | \code{\var{q} * \var{denominator} \%{} \var{modulus} == |
| 81 | \var{numerator}}. One could also implement this function in Python, |
| 82 | using \function{gcdext()}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 83 | \end{funcdesc} |
| 84 | |
| 85 | An mpz-number has one method: |
| 86 | |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 87 | \begin{methoddesc}[mpz]{binary}{} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 88 | Convert this mpz-number to a binary string, where the number has been |
| 89 | stored as an array of radix-256 digits, least significant digit first. |
| 90 | |
Guido van Rossum | 6bb1adc | 1995-03-13 10:03:32 +0000 | [diff] [blame] | 91 | The mpz-number must have a value greater than or equal to zero, |
Fred Drake | fc57619 | 1998-04-04 07:15:02 +0000 | [diff] [blame] | 92 | otherwise \exception{ValueError} will be raised. |
| 93 | \end{methoddesc} |