cpython/Doc/lib/libmpz.tex

99 lines
3.7 KiB
TeX
Raw Normal View History

\section{\module{mpz} ---
GNU MP library for arbitrary precision arithmetic.}
\declaremodule{builtin}{mpz}
\modulesynopsis{Interface to the GNU MP library for arbitrary
precision arithmetic.}
1994-01-01 21:22:07 -04:00
1995-08-10 11:21:49 -03:00
This is an optional module. It is only available when Python is
configured to include it, which requires that the GNU MP software is
installed.
\index{MP, GNU library}
\index{arbitrary precision integers}
\index{integer!arbitrary precision}
1995-08-10 11:21:49 -03:00
This module implements the interface to part of the GNU MP library,
which defines arbitrary precision integer and rational number
arithmetic routines. Only the interfaces to the \emph{integer}
(\function{mpz_*()}) routines are provided. If not stated
1994-01-01 21:22:07 -04:00
otherwise, the description in the GNU MP documentation can be applied.
Support for rational numbers\index{rational numbers} can be
implemented in Python. For an example, see the \module{Rat}%
\withsubitem{(demo module)}{\ttindex{Rat}} module, provided as
\file{Demos/classes/Rat.py} in the Python source distribution.
1994-01-01 21:22:07 -04:00
In general, \dfn{mpz}-numbers can be used just like other standard
Python numbers, e.g., you can use the built-in operators like \code{+},
1994-01-01 21:22:07 -04:00
\code{*}, etc., as well as the standard built-in functions like
\function{abs()}, \function{int()}, \ldots, \function{divmod()},
\function{pow()}. \strong{Please note:} the \emph{bitwise-xor}
operation has been implemented as a bunch of \emph{and}s,
\emph{invert}s and \emph{or}s, because the library lacks an
\cfunction{mpz_xor()} function, and I didn't need one.
1994-01-01 21:22:07 -04:00
You create an mpz-number by calling the function \function{mpz()} (see
1995-03-13 06:03:32 -04:00
below for an exact description). An mpz-number is printed like this:
1994-01-01 21:22:07 -04:00
\code{mpz(\var{value})}.
1994-01-01 21:22:07 -04:00
\begin{funcdesc}{mpz}{value}
Create a new mpz-number. \var{value} can be an integer, a long,
another mpz-number, or even a string. If it is a string, it is
interpreted as an array of radix-256 digits, least significant digit
first, resulting in a positive number. See also the \method{binary()}
1994-01-01 21:22:07 -04:00
method, described below.
\end{funcdesc}
\begin{datadesc}{MPZType}
The type of the objects returned by \function{mpz()} and most other
functions in this module.
\end{datadesc}
A number of \emph{extra} functions are defined in this module. Non
1994-01-01 21:22:07 -04:00
mpz-arguments are converted to mpz-values first, and the functions
return mpz-numbers.
\begin{funcdesc}{powm}{base, exponent, modulus}
1994-01-01 21:22:07 -04:00
Return \code{pow(\var{base}, \var{exponent}) \%{} \var{modulus}}. If
\code{\var{exponent} == 0}, return \code{mpz(1)}. In contrast to the
\C{} library function, this version can handle negative exponents.
1994-01-01 21:22:07 -04:00
\end{funcdesc}
\begin{funcdesc}{gcd}{op1, op2}
1994-01-01 21:22:07 -04:00
Return the greatest common divisor of \var{op1} and \var{op2}.
\end{funcdesc}
\begin{funcdesc}{gcdext}{a, b}
1994-01-01 21:22:07 -04:00
Return a tuple \code{(\var{g}, \var{s}, \var{t})}, such that
\code{\var{a}*\var{s} + \var{b}*\var{t} == \var{g} == gcd(\var{a}, \var{b})}.
\end{funcdesc}
\begin{funcdesc}{sqrt}{op}
Return the square root of \var{op}. The result is rounded towards zero.
\end{funcdesc}
\begin{funcdesc}{sqrtrem}{op}
Return a tuple \code{(\var{root}, \var{remainder})}, such that
\code{\var{root}*\var{root} + \var{remainder} == \var{op}}.
\end{funcdesc}
\begin{funcdesc}{divm}{numerator, denominator, modulus}
Returns a number \var{q} such that
\code{\var{q} * \var{denominator} \%{} \var{modulus} ==
\var{numerator}}. One could also implement this function in Python,
using \function{gcdext()}.
1994-01-01 21:22:07 -04:00
\end{funcdesc}
An mpz-number has one method:
\begin{methoddesc}[mpz]{binary}{}
1994-01-01 21:22:07 -04:00
Convert this mpz-number to a binary string, where the number has been
stored as an array of radix-256 digits, least significant digit first.
1995-03-13 06:03:32 -04:00
The mpz-number must have a value greater than or equal to zero,
otherwise \exception{ValueError} will be raised.
\end{methoddesc}