blob: 98f09a876bb559864c52ff6358cc9dab6f0f8437 [file] [log] [blame]
Fred Drakefc576191998-04-04 07:15:02 +00001\section{Built-in Module \module{mpz}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{builtin}{mpz}
3
4\modulesynopsis{Interface to the GNU MP library for arbitrary precision arithmetic.}
5
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00006
Guido van Rossum3e43d831995-08-10 14:21:49 +00007This is an optional module. It is only available when Python is
8configured to include it, which requires that the GNU MP software is
9installed.
Fred Drakefc576191998-04-04 07:15:02 +000010\index{MP, GNU library}
11\index{arbitrary precision integers}
12\index{integer!arbitrary precision}
Guido van Rossum3e43d831995-08-10 14:21:49 +000013
14This module implements the interface to part of the GNU MP library,
15which defines arbitrary precision integer and rational number
16arithmetic routines. Only the interfaces to the \emph{integer}
Fred Drakefc576191998-04-04 07:15:02 +000017(\function{mpz_*()}) routines are provided. If not stated
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000018otherwise, the description in the GNU MP documentation can be applied.
19
20In general, \dfn{mpz}-numbers can be used just like other standard
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000021Python numbers, e.g.\ you can use the built-in operators like \code{+},
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000022\code{*}, etc., as well as the standard built-in functions like
Fred Drakefc576191998-04-04 07:15:02 +000023\function{abs()}, \function{int()}, \ldots, \function{divmod()},
24\function{pow()}. \strong{Please note:} the \emph{bitwise-xor}
25operation has been implemented as a bunch of \emph{and}s,
26\emph{invert}s and \emph{or}s, because the library lacks an
27\cfunction{mpz_xor()} function, and I didn't need one.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000028
Fred Drakefc576191998-04-04 07:15:02 +000029You create an mpz-number by calling the function \function{mpz()} (see
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000030below for an exact description). An mpz-number is printed like this:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000031\code{mpz(\var{value})}.
32
Fred Drakefc576191998-04-04 07:15:02 +000033
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000034\begin{funcdesc}{mpz}{value}
35 Create a new mpz-number. \var{value} can be an integer, a long,
36 another mpz-number, or even a string. If it is a string, it is
37 interpreted as an array of radix-256 digits, least significant digit
Fred Drakefc576191998-04-04 07:15:02 +000038 first, resulting in a positive number. See also the \method{binary()}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000039 method, described below.
40\end{funcdesc}
41
Fred Drakefc576191998-04-04 07:15:02 +000042\begin{datadesc}{MPZType}
43 The type of the objects returned by \function{mpz()} and most other
44 functions in this module.
45\end{datadesc}
46
47
Fred Drakeaf8a0151998-01-14 14:51:31 +000048A number of \emph{extra} functions are defined in this module. Non
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000049mpz-arguments are converted to mpz-values first, and the functions
50return mpz-numbers.
51
Fred Drakecce10901998-03-17 06:33:25 +000052\begin{funcdesc}{powm}{base, exponent, modulus}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000053 Return \code{pow(\var{base}, \var{exponent}) \%{} \var{modulus}}. If
54 \code{\var{exponent} == 0}, return \code{mpz(1)}. In contrast to the
Fred Drakefc576191998-04-04 07:15:02 +000055 \C{} library function, this version can handle negative exponents.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000056\end{funcdesc}
57
Fred Drakecce10901998-03-17 06:33:25 +000058\begin{funcdesc}{gcd}{op1, op2}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000059 Return the greatest common divisor of \var{op1} and \var{op2}.
60\end{funcdesc}
61
Fred Drakecce10901998-03-17 06:33:25 +000062\begin{funcdesc}{gcdext}{a, b}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000063 Return a tuple \code{(\var{g}, \var{s}, \var{t})}, such that
64 \code{\var{a}*\var{s} + \var{b}*\var{t} == \var{g} == gcd(\var{a}, \var{b})}.
65\end{funcdesc}
66
67\begin{funcdesc}{sqrt}{op}
68 Return the square root of \var{op}. The result is rounded towards zero.
69\end{funcdesc}
70
71\begin{funcdesc}{sqrtrem}{op}
72 Return a tuple \code{(\var{root}, \var{remainder})}, such that
73 \code{\var{root}*\var{root} + \var{remainder} == \var{op}}.
74\end{funcdesc}
75
Fred Drakecce10901998-03-17 06:33:25 +000076\begin{funcdesc}{divm}{numerator, denominator, modulus}
Fred Drakefc576191998-04-04 07:15:02 +000077 Returns a number \var{q} such that
78 \code{\var{q} * \var{denominator} \%{} \var{modulus} ==
79 \var{numerator}}. One could also implement this function in Python,
80 using \function{gcdext()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000081\end{funcdesc}
82
83An mpz-number has one method:
84
Fred Drakefc576191998-04-04 07:15:02 +000085\begin{methoddesc}[mpz]{binary}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000086 Convert this mpz-number to a binary string, where the number has been
87 stored as an array of radix-256 digits, least significant digit first.
88
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000089 The mpz-number must have a value greater than or equal to zero,
Fred Drakefc576191998-04-04 07:15:02 +000090 otherwise \exception{ValueError} will be raised.
91\end{methoddesc}