blob: c761ea10228163367b1a5ee777a5ca51b206308b [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{mpz} ---
2 GNU MP library for arbitrary precision arithmetic.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{builtin}{mpz}
4
Fred Drake295da241998-08-10 19:42:37 +00005\modulesynopsis{Interface to the GNU MP library for arbitrary
6precision arithmetic.}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00008
Guido van Rossum3e43d831995-08-10 14:21:49 +00009This is an optional module. It is only available when Python is
10configured to include it, which requires that the GNU MP software is
11installed.
Fred Drakefc576191998-04-04 07:15:02 +000012\index{MP, GNU library}
13\index{arbitrary precision integers}
14\index{integer!arbitrary precision}
Guido van Rossum3e43d831995-08-10 14:21:49 +000015
16This module implements the interface to part of the GNU MP library,
17which defines arbitrary precision integer and rational number
18arithmetic routines. Only the interfaces to the \emph{integer}
Fred Drakefc576191998-04-04 07:15:02 +000019(\function{mpz_*()}) routines are provided. If not stated
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020otherwise, the description in the GNU MP documentation can be applied.
21
22In general, \dfn{mpz}-numbers can be used just like other standard
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000023Python numbers, e.g.\ you can use the built-in operators like \code{+},
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000024\code{*}, etc., as well as the standard built-in functions like
Fred Drakefc576191998-04-04 07:15:02 +000025\function{abs()}, \function{int()}, \ldots, \function{divmod()},
26\function{pow()}. \strong{Please note:} the \emph{bitwise-xor}
27operation 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 Rossum5fdeeea1994-01-02 01:22:07 +000030
Fred Drakefc576191998-04-04 07:15:02 +000031You create an mpz-number by calling the function \function{mpz()} (see
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000032below for an exact description). An mpz-number is printed like this:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000033\code{mpz(\var{value})}.
34
Fred Drakefc576191998-04-04 07:15:02 +000035
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000036\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 Drakefc576191998-04-04 07:15:02 +000040 first, resulting in a positive number. See also the \method{binary()}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000041 method, described below.
42\end{funcdesc}
43
Fred Drakefc576191998-04-04 07:15:02 +000044\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 Drakeaf8a0151998-01-14 14:51:31 +000050A number of \emph{extra} functions are defined in this module. Non
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000051mpz-arguments are converted to mpz-values first, and the functions
52return mpz-numbers.
53
Fred Drakecce10901998-03-17 06:33:25 +000054\begin{funcdesc}{powm}{base, exponent, modulus}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000055 Return \code{pow(\var{base}, \var{exponent}) \%{} \var{modulus}}. If
56 \code{\var{exponent} == 0}, return \code{mpz(1)}. In contrast to the
Fred Drakefc576191998-04-04 07:15:02 +000057 \C{} library function, this version can handle negative exponents.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000058\end{funcdesc}
59
Fred Drakecce10901998-03-17 06:33:25 +000060\begin{funcdesc}{gcd}{op1, op2}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000061 Return the greatest common divisor of \var{op1} and \var{op2}.
62\end{funcdesc}
63
Fred Drakecce10901998-03-17 06:33:25 +000064\begin{funcdesc}{gcdext}{a, b}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000065 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 Drakecce10901998-03-17 06:33:25 +000078\begin{funcdesc}{divm}{numerator, denominator, modulus}
Fred Drakefc576191998-04-04 07:15:02 +000079 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 Rossum5fdeeea1994-01-02 01:22:07 +000083\end{funcdesc}
84
85An mpz-number has one method:
86
Fred Drakefc576191998-04-04 07:15:02 +000087\begin{methoddesc}[mpz]{binary}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000088 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 Rossum6bb1adc1995-03-13 10:03:32 +000091 The mpz-number must have a value greater than or equal to zero,
Fred Drakefc576191998-04-04 07:15:02 +000092 otherwise \exception{ValueError} will be raised.
93\end{methoddesc}