1 \section{Built-in module
\sectcode{mpz
}}
4 This module implements the interface to part of the GNU MP library.
5 This library contains arbitrary precision integer and rational number
6 arithmetic routines. Only the interfaces to the
\emph{integer
}
7 (
\samp{mpz_
{\rm \ldots}}) routines are provided. If not stated
8 otherwise, the description in the GNU MP documentation can be applied.
10 In general,
\dfn{mpz
}-numbers can be used just like other standard
11 Python numbers, e.g.\ you can use the built-in operators like
\code{+
},
12 \code{*
}, etc., as well as the standard built-in functions like
13 \code{abs
},
\code{int
},
\ldots,
\code{divmod
},
\code{pow
}.
14 \strong{Please note:
} the
{\it bitwise-xor
} operation has been implemented as
15 a bunch of
{\it and
}s,
{\it invert
}s and
{\it or
}s, because the library
16 lacks an
\code{mpz_xor
} function, and I didn't need one.
18 You create an mpz-number by calling the function called
\code{mpz
} (see
19 below for an exact description). An mpz-number is printed like this:
20 \code{mpz(
\var{value
})
}.
22 \renewcommand{\indexsubitem}{(in module mpz)
}
23 \begin{funcdesc
}{mpz
}{value
}
24 Create a new mpz-number.
\var{value
} can be an integer, a long,
25 another mpz-number, or even a string. If it is a string, it is
26 interpreted as an array of radix-
256 digits, least significant digit
27 first, resulting in a positive number. See also the
\code{binary
}
28 method, described below.
31 A number of
{\em extra
} functions are defined in this module. Non
32 mpz-arguments are converted to mpz-values first, and the functions
35 \begin{funcdesc
}{powm
}{base\, exponent\, modulus
}
36 Return
\code{pow(
\var{base
},
\var{exponent
}) \%
{} \var{modulus
}}. If
37 \code{\var{exponent
} ==
0}, return
\code{mpz(
1)
}. In contrast to the
38 \C-library function, this version can handle negative exponents.
41 \begin{funcdesc
}{gcd
}{op1\, op2
}
42 Return the greatest common divisor of
\var{op1
} and
\var{op2
}.
45 \begin{funcdesc
}{gcdext
}{a\, b
}
46 Return a tuple
\code{(
\var{g
},
\var{s
},
\var{t
})
}, such that
47 \code{\var{a
}*
\var{s
} +
\var{b
}*
\var{t
} ==
\var{g
} == gcd(
\var{a
},
\var{b
})
}.
50 \begin{funcdesc
}{sqrt
}{op
}
51 Return the square root of
\var{op
}. The result is rounded towards zero.
54 \begin{funcdesc
}{sqrtrem
}{op
}
55 Return a tuple
\code{(
\var{root
},
\var{remainder
})
}, such that
56 \code{\var{root
}*
\var{root
} +
\var{remainder
} ==
\var{op
}}.
59 \begin{funcdesc
}{divm
}{numerator\, denominator\, modulus
}
60 Returns a number
\var{q
}. such that
61 \code{\var{q
} *
\var{denominator
} \%
{} \var{modulus
} ==
\var{numerator
}}.
62 One could also implement this function in Python, using
\code{gcdext
}.
65 An mpz-number has one method:
67 \renewcommand{\indexsubitem}{(mpz method)
}
68 \begin{funcdesc
}{binary
}{}
69 Convert this mpz-number to a binary string, where the number has been
70 stored as an array of radix-
256 digits, least significant digit first.
72 The mpz-number must have a value greater than or equal to zero,
73 otherwise a
\code{ValueError
}-exception will be raised.