Multiple-precision arithmetic library providing fast GMP, MPFR, and MPC interfaces for Python
npx @tessl/cli install tessl/pypi-gmpy2@2.2.0gmpy2 is an optimized, C-coded Python extension module that provides fast multiple-precision arithmetic operations. It interfaces with the GNU Multiple Precision Arithmetic Library (GMP), the Multiple Precision Floating-Point Reliable Library (MPFR), and the Multiple Precision Complex Library (MPC) to deliver high-performance arbitrary-precision arithmetic for integers, rational numbers, real numbers, and complex numbers.
pip install gmpy2import gmpy2Import specific data types and functions:
from gmpy2 import mpz, mpq, mpfr, mpc, get_context, set_contextImport everything:
from gmpy2 import *import gmpy2
from gmpy2 import mpz, mpq, mpfr, mpc
# Multiple-precision integers
big_int = mpz("123456789012345678901234567890")
print(big_int * 2) # 246913578024691357802469135780
# Rational numbers with exact representation
rational = mpq(22, 7) # Approximation of pi
print(rational) # 22/7
print(float(rational)) # 3.142857142857143
# High-precision floating-point numbers
with gmpy2.local_context(precision=100): # 100-bit precision
x = mpfr("1.23456789012345678901234567890")
y = gmpy2.sqrt(x)
print(y) # High-precision square root
# Complex numbers with arbitrary precision
z = mpc("3.14159+2.71828j")
print(gmpy2.phase(z)) # Complex phase/argumentgmpy2 provides a comprehensive multi-precision arithmetic system built on three main components:
This design enables gmpy2 to serve as the foundation for computational mathematics, scientific computing, and cryptographic applications requiring precision beyond Python's native numeric types.
Core data types providing arbitrary-precision arithmetic: integers (mpz), mutable integers (xmpz), rationals (mpq), floating-point numbers (mpfr), and complex numbers (mpc).
class mpz:
def __init__(self, x=0, base=10): ...
def bit_length(self) -> int: ...
def is_prime(self, n=25) -> bool: ...
class mpq:
def __init__(self, x=0, y=1): ...
@property
def numerator(self) -> mpz: ...
@property
def denominator(self) -> mpz: ...
class mpfr:
def __init__(self, x=0.0, precision=None): ...
@property
def precision(self) -> int: ...
def is_finite(self) -> bool: ...
class mpc:
def __init__(self, real=0, imag=0, precision=None): ...
@property
def real(self) -> mpfr: ...
@property
def imag(self) -> mpfr: ...Basic and advanced arithmetic operations with context-aware precision and rounding control for all numeric types.
def add(x, y): ...
def sub(x, y): ...
def mul(x, y): ...
def div(x, y): ...
def powmod(x, y, z): ...
def square(x): ...Comprehensive number theory functions including GCD/LCM, modular arithmetic, primality testing, factorization helpers, and integer sequences.
def gcd(*args): ...
def lcm(*args): ...
def is_prime(x, n=25) -> bool: ...
def next_prime(x): ...
def invert(x, y): ...
def bincoef(n, k): ...
def fac(n): ...
def fib(n): ...Extensive collection of mathematical functions including trigonometry, logarithms, exponentials, special functions, and constants with configurable precision.
def sqrt(x): ...
def sin(x): ...
def cos(x): ...
def exp(x): ...
def log(x): ...
def gamma(x): ...
def const_pi(precision=None): ...
def const_euler(precision=None): ...Efficient bit manipulation operations for integer types including setting, clearing, testing bits, and bitwise logical operations.
def bit_set(x, n): ...
def bit_clear(x, n): ...
def bit_test(x, n) -> bool: ...
def bit_count(x) -> int: ...
def bit_length(x) -> int: ...
def hamdist(x, y) -> int: ...Precision and rounding control system providing thread-safe computation contexts with configurable precision, rounding modes, and exception handling.
class context:
def __init__(self, precision=53, round=RoundToNearest, **kwargs): ...
def __enter__(self): ...
def __exit__(self, *args): ...
def get_context() -> context: ...
def set_context(ctx: context): ...
def local_context(**kwargs): ...Random number generation for all gmpy2 types with multiple algorithms and configurable random state management.
class random_state:
def __init__(self, seed=0): ...
def mpz_random(state, n): ...
def mpz_urandomb(state, n): ...
def mpfr_random(state): ...
def mpc_random(state): ...Version information, binary serialization, floating-point utilities, and system information functions.
def version() -> str: ...
def mp_version() -> str: ...
def mpfr_version() -> str: ...
def mpc_version() -> str: ...
def mp_limbsize() -> int: ...
def license() -> str: ...
def to_binary(x): ...
def from_binary(s): ...
def pack(x, precision, emin, emax): ...
def unpack(s, precision, emin, emax): ...
def get_max_precision() -> int: ...
def get_emax_max() -> int: ...
def get_emin_min() -> int: ...
def free_cache(): ...# Rounding modes
RoundToNearest: int
RoundToZero: int
RoundUp: int
RoundDown: int
RoundAwayZero: int
Default: int
# Version information
__version__: strclass DivisionByZeroError(ArithmeticError): ...
class InexactResultError(ArithmeticError): ...
class InvalidOperationError(ArithmeticError): ...
class OverflowResultError(ArithmeticError): ...
class UnderflowResultError(ArithmeticError): ...
class RangeError(ArithmeticError): ...