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