Built-in module mpz

mpz

This module implements the interface to part of the GNU MP library. This library contains arbitrary precision integer and rational number arithmetic routines. Only the interfaces to the integer (mpz_...) routines are provided. If not stated otherwise, the description in the GNU MP documentation can be applied.

In general, mpz-numbers can be used just like other standard Python numbers, e.g. you can use the built-in operators like +, *, etc., as well as the standard built-in functions like abs, int, ..., divmod, pow. Please note: the bitwise-xor operation has been implemented as a bunch of ands, inverts and ors, because the library lacks an mpz_xor function, and I didn't need one.

You create an mpz-number, by calling the function called mpz (see below for an excact description). An mpz-number is printed like this: mpz(value).


\begin{funcdesc}{mpz}{value}
Create a new mpz-number. \var{value} can be an int...
...tive number. See also the \code{binary}
method, described below.
\end{funcdesc}

A number of extra functions are defined in this module. Non mpz-arguments are converted to mpz-values first, and the functions return mpz-numbers.


\begin{funcdesc}{powm}{base\, exponent\, modulus}
Return \code{pow(\var{base}, ...
... \C-library function, this version can handle negative exponents.
\end{funcdesc}


\begin{funcdesc}{gcd}{op1\, op2}
Return the greatest common divisor of \var{op1} and \var{op2}.
\end{funcdesc}


\begin{funcdesc}{gcdext}{a\, b}
Return a tuple \code{(\var{g}, \var{s}, \var{t}...
...}*\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}...
...that
\code{\var{root}*\var{root} + \var{remainder} == \var{op}}.
\end{funcdesc}


\begin{funcdesc}{divm}{numerator\, denominator\, modulus}
Returns a number \var...
...ould also implement this function in python, using \code{gcdext}.
\end{funcdesc}

An mpz-number has one method:


\begin{funcdesc}{binary}{}
Convert this mpz-number to a binary string, where th...
...to zero,
otherwise a \code{ValueError}-exception will be raised.
\end{funcdesc}