blob: 236666822c5edc59226ea02d13105152f9ac3776 [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
Fred Drake0c0b5302001-12-16 01:54:55 +00009\deprecated{2.2}{See the references at the end of this section for
10 information about packages which provide similar
11 functionality. This module will be removed in Python
12 2.3.}
13
14
Guido van Rossum3e43d831995-08-10 14:21:49 +000015This is an optional module. It is only available when Python is
16configured to include it, which requires that the GNU MP software is
17installed.
Fred Drakefc576191998-04-04 07:15:02 +000018\index{MP, GNU library}
19\index{arbitrary precision integers}
20\index{integer!arbitrary precision}
Guido van Rossum3e43d831995-08-10 14:21:49 +000021
22This module implements the interface to part of the GNU MP library,
23which defines arbitrary precision integer and rational number
24arithmetic routines. Only the interfaces to the \emph{integer}
Fred Drakefc576191998-04-04 07:15:02 +000025(\function{mpz_*()}) routines are provided. If not stated
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000026otherwise, the description in the GNU MP documentation can be applied.
27
Fred Drake9b134bf1998-09-10 18:42:55 +000028Support for rational numbers\index{rational numbers} can be
Fred Drakefcf94d41999-04-22 20:55:59 +000029implemented in Python. For an example, see the
30\module{Rat}\withsubitem{(demo module)}{\ttindex{Rat}} module, provided as
Fred Drake9b134bf1998-09-10 18:42:55 +000031\file{Demos/classes/Rat.py} in the Python source distribution.
32
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000033In general, \dfn{mpz}-numbers can be used just like other standard
Fred Drake9b134bf1998-09-10 18:42:55 +000034Python numbers, e.g., you can use the built-in operators like \code{+},
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000035\code{*}, etc., as well as the standard built-in functions like
Fred Drakefc576191998-04-04 07:15:02 +000036\function{abs()}, \function{int()}, \ldots, \function{divmod()},
37\function{pow()}. \strong{Please note:} the \emph{bitwise-xor}
38operation has been implemented as a bunch of \emph{and}s,
39\emph{invert}s and \emph{or}s, because the library lacks an
40\cfunction{mpz_xor()} function, and I didn't need one.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000041
Fred Drakefc576191998-04-04 07:15:02 +000042You create an mpz-number by calling the function \function{mpz()} (see
Guido van Rossum6bb1adc1995-03-13 10:03:32 +000043below for an exact description). An mpz-number is printed like this:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000044\code{mpz(\var{value})}.
45
Fred Drakefc576191998-04-04 07:15:02 +000046
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000047\begin{funcdesc}{mpz}{value}
48 Create a new mpz-number. \var{value} can be an integer, a long,
49 another mpz-number, or even a string. If it is a string, it is
50 interpreted as an array of radix-256 digits, least significant digit
Fred Drakefc576191998-04-04 07:15:02 +000051 first, resulting in a positive number. See also the \method{binary()}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000052 method, described below.
53\end{funcdesc}
54
Fred Drakefc576191998-04-04 07:15:02 +000055\begin{datadesc}{MPZType}
56 The type of the objects returned by \function{mpz()} and most other
57 functions in this module.
58\end{datadesc}
59
60
Fred Drakeaf8a0151998-01-14 14:51:31 +000061A number of \emph{extra} functions are defined in this module. Non
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000062mpz-arguments are converted to mpz-values first, and the functions
63return mpz-numbers.
64
Fred Drakecce10901998-03-17 06:33:25 +000065\begin{funcdesc}{powm}{base, exponent, modulus}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000066 Return \code{pow(\var{base}, \var{exponent}) \%{} \var{modulus}}. If
67 \code{\var{exponent} == 0}, return \code{mpz(1)}. In contrast to the
Fred Drakefc576191998-04-04 07:15:02 +000068 \C{} library function, this version can handle negative exponents.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000069\end{funcdesc}
70
Fred Drakecce10901998-03-17 06:33:25 +000071\begin{funcdesc}{gcd}{op1, op2}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000072 Return the greatest common divisor of \var{op1} and \var{op2}.
73\end{funcdesc}
74
Fred Drakecce10901998-03-17 06:33:25 +000075\begin{funcdesc}{gcdext}{a, b}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000076 Return a tuple \code{(\var{g}, \var{s}, \var{t})}, such that
77 \code{\var{a}*\var{s} + \var{b}*\var{t} == \var{g} == gcd(\var{a}, \var{b})}.
78\end{funcdesc}
79
80\begin{funcdesc}{sqrt}{op}
81 Return the square root of \var{op}. The result is rounded towards zero.
82\end{funcdesc}
83
84\begin{funcdesc}{sqrtrem}{op}
85 Return a tuple \code{(\var{root}, \var{remainder})}, such that
86 \code{\var{root}*\var{root} + \var{remainder} == \var{op}}.
87\end{funcdesc}
88
Fred Drakecce10901998-03-17 06:33:25 +000089\begin{funcdesc}{divm}{numerator, denominator, modulus}
Fred Drakefc576191998-04-04 07:15:02 +000090 Returns a number \var{q} such that
91 \code{\var{q} * \var{denominator} \%{} \var{modulus} ==
92 \var{numerator}}. One could also implement this function in Python,
93 using \function{gcdext()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000094\end{funcdesc}
95
96An mpz-number has one method:
97
Fred Drakefc576191998-04-04 07:15:02 +000098\begin{methoddesc}[mpz]{binary}{}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000099 Convert this mpz-number to a binary string, where the number has been
100 stored as an array of radix-256 digits, least significant digit first.
101
Guido van Rossum6bb1adc1995-03-13 10:03:32 +0000102 The mpz-number must have a value greater than or equal to zero,
Fred Drakefc576191998-04-04 07:15:02 +0000103 otherwise \exception{ValueError} will be raised.
104\end{methoddesc}
Fred Drakeb40501b2001-12-15 18:37:24 +0000105
106
107\begin{seealso}
108 \seetitle[http://gmpy.sourceforge.net/]{General Multiprecision Python}{
109 This project is building new numeric types to allow
110 arbitrary-precision arithmetic in Python. Their first
111 efforts are also based on the GNU MP library.}
Fred Drakec5777282001-12-15 20:37:40 +0000112
113 \seetitle[http://www.egenix.com/files/python/mxNumber.html]{mxNumber
114 --- Extended Numeric Types for Python}{Another wrapper
115 around the GNU MP library, including a port of that
116 library to Windows.}
Fred Drakeb40501b2001-12-15 18:37:24 +0000117\end{seealso}