blob: 707a3a812a1f87c184057b9be191eadcb4db3908 [file] [log] [blame]
Fred Drakefc576191998-04-04 07:15:02 +00001\section{Built-in Module \module{mpz}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-mpz}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003\bimodindex{mpz}
4
Guido van Rossum3e43d831995-08-10 14:21:49 +00005This is an optional module. It is only available when Python is
6configured to include it, which requires that the GNU MP software is
7installed.
Fred Drakefc576191998-04-04 07:15:02 +00008\index{MP, GNU library}
9\index{arbitrary precision integers}
10\index{integer!arbitrary precision}
Guido van Rossum3e43d831995-08-10 14:21:49 +000011
12This module implements the interface to part of the GNU MP library,
13which defines arbitrary precision integer and rational number
14arithmetic routines. Only the interfaces to the \emph{integer}
Fred Drakefc576191998-04-04 07:15:02 +000015(\function{mpz_*()}) routines are provided. If not stated
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000016otherwise, the description in the GNU MP documentation can be applied.
17
18In general, \dfn{mpz}-numbers can be used just like other standard
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000019Python numbers, e.g.\ you can use the built-in operators like \code{+},
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020\code{*}, etc., as well as the standard built-in functions like
Fred Drakefc576191998-04-04 07:15:02 +000021\function{abs()}, \function{int()}, \ldots, \function{divmod()},
22\function{pow()}. \strong{Please note:} the \emph{bitwise-xor}
23operation has been implemented as a bunch of \emph{and}s,
24\emph{invert}s and \emph{or}s, because the library lacks an
25\cfunction{mpz_xor()} function, and I didn't need one.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000026
Fred Drakefc576191998-04-04 07:15:02 +000027You create an mpz-number by calling the function \function{mpz()} (see
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000028below for an exact description). An mpz-number is printed like this:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000029\code{mpz(\var{value})}.
30
Fred Drakefc576191998-04-04 07:15:02 +000031
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000032\begin{funcdesc}{mpz}{value}
33 Create a new mpz-number. \var{value} can be an integer, a long,
34 another mpz-number, or even a string. If it is a string, it is
35 interpreted as an array of radix-256 digits, least significant digit
Fred Drakefc576191998-04-04 07:15:02 +000036 first, resulting in a positive number. See also the \method{binary()}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000037 method, described below.
38\end{funcdesc}
39
Fred Drakefc576191998-04-04 07:15:02 +000040\begin{datadesc}{MPZType}
41 The type of the objects returned by \function{mpz()} and most other
42 functions in this module.
43\end{datadesc}
44
45
Fred Drakeaf8a0151998-01-14 14:51:31 +000046A number of \emph{extra} functions are defined in this module. Non
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000047mpz-arguments are converted to mpz-values first, and the functions
48return mpz-numbers.
49
Fred Drakecce10901998-03-17 06:33:25 +000050\begin{funcdesc}{powm}{base, exponent, modulus}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000051 Return \code{pow(\var{base}, \var{exponent}) \%{} \var{modulus}}. If
52 \code{\var{exponent} == 0}, return \code{mpz(1)}. In contrast to the
Fred Drakefc576191998-04-04 07:15:02 +000053 \C{} library function, this version can handle negative exponents.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000054\end{funcdesc}
55
Fred Drakecce10901998-03-17 06:33:25 +000056\begin{funcdesc}{gcd}{op1, op2}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000057 Return the greatest common divisor of \var{op1} and \var{op2}.
58\end{funcdesc}
59
Fred Drakecce10901998-03-17 06:33:25 +000060\begin{funcdesc}{gcdext}{a, b}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000061 Return a tuple \code{(\var{g}, \var{s}, \var{t})}, such that
62 \code{\var{a}*\var{s} + \var{b}*\var{t} == \var{g} == gcd(\var{a}, \var{b})}.
63\end{funcdesc}
64
65\begin{funcdesc}{sqrt}{op}
66 Return the square root of \var{op}. The result is rounded towards zero.
67\end{funcdesc}
68
69\begin{funcdesc}{sqrtrem}{op}
70 Return a tuple \code{(\var{root}, \var{remainder})}, such that
71 \code{\var{root}*\var{root} + \var{remainder} == \var{op}}.
72\end{funcdesc}
73
Fred Drakecce10901998-03-17 06:33:25 +000074\begin{funcdesc}{divm}{numerator, denominator, modulus}
Fred Drakefc576191998-04-04 07:15:02 +000075 Returns a number \var{q} such that
76 \code{\var{q} * \var{denominator} \%{} \var{modulus} ==
77 \var{numerator}}. One could also implement this function in Python,
78 using \function{gcdext()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000079\end{funcdesc}
80
81An mpz-number has one method:
82
Fred Drakefc576191998-04-04 07:15:02 +000083\begin{methoddesc}[mpz]{binary}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000084 Convert this mpz-number to a binary string, where the number has been
85 stored as an array of radix-256 digits, least significant digit first.
86
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000087 The mpz-number must have a value greater than or equal to zero,
Fred Drakefc576191998-04-04 07:15:02 +000088 otherwise \exception{ValueError} will be raised.
89\end{methoddesc}