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