blob: ea0d1b77e9d294f2c1fd409c49ff6086f8965162 [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in module \sectcode{mpz}}
2\bimodindex{mpz}
3
4This module implements the interface to part of the GNU MP library.
5This library contains arbitrary precision integer and rational number
6arithmetic routines. Only the interfaces to the \emph{integer}
7(\samp{mpz_{\rm \ldots}}) routines are provided. If not stated
8otherwise, the description in the GNU MP documentation can be applied.
9
10In general, \dfn{mpz}-numbers can be used just like other standard
11Python numbers, e.g. you can use the built-in operators like \code{+},
12\code{*}, etc., as well as the standard built-in functions like
13\code{abs}, \code{int}, \ldots, \code{divmod}, \code{pow}.
14\strong{Please note:} the {\it bitwise-xor} operation has been implemented as
15a bunch of {\it and}s, {\it invert}s and {\it or}s, because the library
16lacks an \code{mpz_xor} function, and I didn't need one.
17
18You create an mpz-number, by calling the function called \code{mpz} (see
19below for an excact description). An mpz-number is printed like this:
20\code{mpz(\var{value})}.
21
22\renewcommand{\indexsubitem}{(in module mpz)}
23\begin{funcdesc}{mpz}{value}
24 Create a new mpz-number. \var{value} can be an integer, a long,
25 another mpz-number, or even a string. If it is a string, it is
26 interpreted as an array of radix-256 digits, least significant digit
27 first, resulting in a positive number. See also the \code{binary}
28 method, described below.
29\end{funcdesc}
30
31A number of {\em extra} functions are defined in this module. Non
32mpz-arguments are converted to mpz-values first, and the functions
33return mpz-numbers.
34
35\begin{funcdesc}{powm}{base\, exponent\, modulus}
36 Return \code{pow(\var{base}, \var{exponent}) \%{} \var{modulus}}. If
37 \code{\var{exponent} == 0}, return \code{mpz(1)}. In contrast to the
38 \C-library function, this version can handle negative exponents.
39\end{funcdesc}
40
41\begin{funcdesc}{gcd}{op1\, op2}
42 Return the greatest common divisor of \var{op1} and \var{op2}.
43\end{funcdesc}
44
45\begin{funcdesc}{gcdext}{a\, b}
46 Return a tuple \code{(\var{g}, \var{s}, \var{t})}, such that
47 \code{\var{a}*\var{s} + \var{b}*\var{t} == \var{g} == gcd(\var{a}, \var{b})}.
48\end{funcdesc}
49
50\begin{funcdesc}{sqrt}{op}
51 Return the square root of \var{op}. The result is rounded towards zero.
52\end{funcdesc}
53
54\begin{funcdesc}{sqrtrem}{op}
55 Return a tuple \code{(\var{root}, \var{remainder})}, such that
56 \code{\var{root}*\var{root} + \var{remainder} == \var{op}}.
57\end{funcdesc}
58
59\begin{funcdesc}{divm}{numerator\, denominator\, modulus}
60 Returns a number \var{q}. such that
61 \code{\var{q} * \var{denominator} \%{} \var{modulus} == \var{numerator}}.
62 One could also implement this function in python, using \code{gcdext}.
63\end{funcdesc}
64
65An mpz-number has one method:
66
67\renewcommand{\indexsubitem}{(mpz method)}
68\begin{funcdesc}{binary}{}
69 Convert this mpz-number to a binary string, where the number has been
70 stored as an array of radix-256 digits, least significant digit first.
71
72 The mpz-number must have a value greater than- or equal to zero,
73 otherwise a \code{ValueError}-exception will be raised.
74\end{funcdesc}