blob: 1e586dcafd63a9e6eb75bdf7d5a66867039fbb7d [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{mpz} ---
Fred Drake76991ec1999-02-20 05:20:49 +00002 GNU arbitrary magnitude integers}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake76991ec1999-02-20 05:20:49 +00004\declaremodule{builtin}{mpz}
Fred Drake295da241998-08-10 19:42:37 +00005\modulesynopsis{Interface to the GNU MP library for arbitrary
6precision arithmetic.}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00008
Guido van Rossum3e43d831995-08-10 14:21:49 +00009This is an optional module. It is only available when Python is
10configured to include it, which requires that the GNU MP software is
11installed.
Fred Drakefc576191998-04-04 07:15:02 +000012\index{MP, GNU library}
13\index{arbitrary precision integers}
14\index{integer!arbitrary precision}
Guido van Rossum3e43d831995-08-10 14:21:49 +000015
16This module implements the interface to part of the GNU MP library,
17which defines arbitrary precision integer and rational number
18arithmetic routines. Only the interfaces to the \emph{integer}
Fred Drakefc576191998-04-04 07:15:02 +000019(\function{mpz_*()}) routines are provided. If not stated
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020otherwise, the description in the GNU MP documentation can be applied.
21
Fred Drake9b134bf1998-09-10 18:42:55 +000022Support for rational numbers\index{rational numbers} can be
Fred Drakefcf94d41999-04-22 20:55:59 +000023implemented in Python. For an example, see the
24\module{Rat}\withsubitem{(demo module)}{\ttindex{Rat}} module, provided as
Fred Drake9b134bf1998-09-10 18:42:55 +000025\file{Demos/classes/Rat.py} in the Python source distribution.
26
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000027In general, \dfn{mpz}-numbers can be used just like other standard
Fred Drake9b134bf1998-09-10 18:42:55 +000028Python numbers, e.g., you can use the built-in operators like \code{+},
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000029\code{*}, etc., as well as the standard built-in functions like
Fred Drakefc576191998-04-04 07:15:02 +000030\function{abs()}, \function{int()}, \ldots, \function{divmod()},
31\function{pow()}. \strong{Please note:} the \emph{bitwise-xor}
32operation has been implemented as a bunch of \emph{and}s,
33\emph{invert}s and \emph{or}s, because the library lacks an
34\cfunction{mpz_xor()} function, and I didn't need one.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000035
Fred Drakefc576191998-04-04 07:15:02 +000036You create an mpz-number by calling the function \function{mpz()} (see
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000037below for an exact description). An mpz-number is printed like this:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000038\code{mpz(\var{value})}.
39
Fred Drakefc576191998-04-04 07:15:02 +000040
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000041\begin{funcdesc}{mpz}{value}
42 Create a new mpz-number. \var{value} can be an integer, a long,
43 another mpz-number, or even a string. If it is a string, it is
44 interpreted as an array of radix-256 digits, least significant digit
Fred Drakefc576191998-04-04 07:15:02 +000045 first, resulting in a positive number. See also the \method{binary()}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000046 method, described below.
47\end{funcdesc}
48
Fred Drakefc576191998-04-04 07:15:02 +000049\begin{datadesc}{MPZType}
50 The type of the objects returned by \function{mpz()} and most other
51 functions in this module.
52\end{datadesc}
53
54
Fred Drakeaf8a0151998-01-14 14:51:31 +000055A number of \emph{extra} functions are defined in this module. Non
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000056mpz-arguments are converted to mpz-values first, and the functions
57return mpz-numbers.
58
Fred Drakecce10901998-03-17 06:33:25 +000059\begin{funcdesc}{powm}{base, exponent, modulus}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000060 Return \code{pow(\var{base}, \var{exponent}) \%{} \var{modulus}}. If
61 \code{\var{exponent} == 0}, return \code{mpz(1)}. In contrast to the
Fred Drakefc576191998-04-04 07:15:02 +000062 \C{} library function, this version can handle negative exponents.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000063\end{funcdesc}
64
Fred Drakecce10901998-03-17 06:33:25 +000065\begin{funcdesc}{gcd}{op1, op2}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000066 Return the greatest common divisor of \var{op1} and \var{op2}.
67\end{funcdesc}
68
Fred Drakecce10901998-03-17 06:33:25 +000069\begin{funcdesc}{gcdext}{a, b}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000070 Return a tuple \code{(\var{g}, \var{s}, \var{t})}, such that
71 \code{\var{a}*\var{s} + \var{b}*\var{t} == \var{g} == gcd(\var{a}, \var{b})}.
72\end{funcdesc}
73
74\begin{funcdesc}{sqrt}{op}
75 Return the square root of \var{op}. The result is rounded towards zero.
76\end{funcdesc}
77
78\begin{funcdesc}{sqrtrem}{op}
79 Return a tuple \code{(\var{root}, \var{remainder})}, such that
80 \code{\var{root}*\var{root} + \var{remainder} == \var{op}}.
81\end{funcdesc}
82
Fred Drakecce10901998-03-17 06:33:25 +000083\begin{funcdesc}{divm}{numerator, denominator, modulus}
Fred Drakefc576191998-04-04 07:15:02 +000084 Returns a number \var{q} such that
85 \code{\var{q} * \var{denominator} \%{} \var{modulus} ==
86 \var{numerator}}. One could also implement this function in Python,
87 using \function{gcdext()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000088\end{funcdesc}
89
90An mpz-number has one method:
91
Fred Drakefc576191998-04-04 07:15:02 +000092\begin{methoddesc}[mpz]{binary}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000093 Convert this mpz-number to a binary string, where the number has been
94 stored as an array of radix-256 digits, least significant digit first.
95
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000096 The mpz-number must have a value greater than or equal to zero,
Fred Drakefc576191998-04-04 07:15:02 +000097 otherwise \exception{ValueError} will be raised.
98\end{methoddesc}
Fred Drakeb40501b2001-12-15 18:37:24 +000099
100
101\begin{seealso}
102 \seetitle[http://gmpy.sourceforge.net/]{General Multiprecision Python}{
103 This project is building new numeric types to allow
104 arbitrary-precision arithmetic in Python. Their first
105 efforts are also based on the GNU MP library.}
106\end{seealso}