Multiple-precision arithmetic library providing fast GMP, MPFR, and MPC interfaces for Python
—
gmpy2 provides comprehensive arithmetic operations that work with all multiple-precision types. Operations automatically promote types and use the current context for precision and rounding control.
Fundamental arithmetic operations with context-aware precision and rounding.
def add(x, y):
"""
Add two numbers using current context.
Args:
x, y: Numeric values (int, float, mpz, mpq, mpfr, mpc, etc.)
Returns:
Result promoted to appropriate gmpy2 type
"""
def sub(x, y):
"""
Subtract y from x using current context.
Args:
x, y: Numeric values
Returns:
Result promoted to appropriate gmpy2 type
"""
def mul(x, y):
"""
Multiply two numbers using current context.
Args:
x, y: Numeric values
Returns:
Result promoted to appropriate gmpy2 type
"""
def div(x, y):
"""
Divide x by y using current context.
Args:
x, y: Numeric values
Returns:
Result promoted to appropriate gmpy2 type
"""
def square(x):
"""
Square a number using current context.
Args:
x: Numeric value
Returns:
x squared, promoted to appropriate gmpy2 type
"""Multiple division modes for precise control over rounding behavior.
def floor_div(x, y):
"""Floor division (towards negative infinity)."""
def divexact(x, y):
"""
Exact division for integers (assumes y divides x exactly).
Args:
x, y: Integer values
Returns:
mpz result of x / y
Note:
Faster than regular division when exact division is guaranteed
"""
# Ceiling division
def c_div(x, y):
"""Ceiling division (towards positive infinity)."""
def c_divmod(x, y):
"""Ceiling division with remainder."""
def c_mod(x, y):
"""Ceiling modulo."""
# Floor division
def f_div(x, y):
"""Floor division (towards negative infinity)."""
def f_divmod(x, y):
"""Floor division with remainder."""
def f_mod(x, y):
"""Floor modulo."""
# Truncating division
def t_div(x, y):
"""Truncating division (towards zero)."""
def t_divmod(x, y):
"""Truncating division with remainder."""
def t_mod(x, y):
"""Truncating modulo."""Optimized division operations for powers of 2.
# Ceiling division by 2^n
def c_div_2exp(x, n):
"""Ceiling division by 2^n."""
def c_divmod_2exp(x, n):
"""Ceiling division by 2^n with remainder."""
def c_mod_2exp(x, n):
"""Ceiling modulo 2^n."""
# Floor division by 2^n
def f_div_2exp(x, n):
"""Floor division by 2^n."""
def f_divmod_2exp(x, n):
"""Floor division by 2^n with remainder."""
def f_mod_2exp(x, n):
"""Floor modulo 2^n."""
# Truncating division by 2^n
def t_div_2exp(x, n):
"""Truncating division by 2^n."""
def t_divmod_2exp(x, n):
"""Truncating division by 2^n with remainder."""
def t_mod_2exp(x, n):
"""Truncating modulo 2^n."""
# Multiplication/division by powers of 2
def mul_2exp(x, n):
"""Multiply by 2^n (left shift)."""
def div_2exp(x, n):
"""Divide by 2^n (right shift)."""Power operations with modular arithmetic for cryptographic applications.
def powmod(x, y, z):
"""
Compute (x^y) mod z efficiently.
Args:
x: Base
y: Exponent
z: Modulus
Returns:
mpz result of (x^y) mod z
Note:
Much faster than pow(x, y) % z for large numbers
"""
def powmod_sec(x, y, z):
"""
Secure modular exponentiation resistant to timing attacks.
Args:
x: Base
y: Exponent
z: Modulus
Returns:
mpz result of (x^y) mod z
Note:
Uses constant-time algorithm suitable for cryptographic use
"""
def powmod_base_list(bases, exp, mod):
"""
Compute product of (base^exp) mod mod for multiple bases.
Args:
bases: List of base values
exp: Common exponent
mod: Modulus
Returns:
mpz result
"""
def powmod_exp_list(base, exps, mod):
"""
Compute product of (base^exp) mod mod for multiple exponents.
Args:
base: Common base
exps: List of exponents
mod: Modulus
Returns:
mpz result
"""Standard modulo operations with different rounding modes.
def mod(x, y):
"""
Compute x mod y using current context.
Args:
x, y: Numeric values
Returns:
Remainder of x / y
"""Specialized operations for rational numbers.
def qdiv(x, y):
"""
Rational division returning exact mpq result.
Args:
x, y: Numeric values
Returns:
mpq representing exact x / y
"""
def numer(x):
"""
Get numerator of rational number.
Args:
x: Rational or integer value
Returns:
mpz numerator
"""
def denom(x):
"""
Get denominator of rational number.
Args:
x: Rational or integer value
Returns:
mpz denominator (1 for integers)
"""Comparison and utility operations.
def cmp(x, y):
"""
Three-way comparison.
Args:
x, y: Numeric values
Returns:
-1 if x < y, 0 if x == y, 1 if x > y
"""
def cmp_abs(x, y):
"""
Compare absolute values.
Args:
x, y: Numeric values
Returns:
-1 if |x| < |y|, 0 if |x| == |y|, 1 if |x| > |y|
"""
def sign(x):
"""
Sign of number.
Args:
x: Numeric value
Returns:
-1 for negative, 0 for zero, 1 for positive
"""
def copy_sign(x, y):
"""
Copy sign from y to magnitude of x.
Args:
x: Value providing magnitude
y: Value providing sign
Returns:
Value with magnitude of x and sign of y
"""import gmpy2
# Default precision arithmetic
a = gmpy2.mpfr("1.23456789")
b = gmpy2.mpfr("9.87654321")
print(gmpy2.add(a, b)) # Uses default precision
# High precision arithmetic
with gmpy2.local_context(precision=100):
x = gmpy2.mpfr("1.23456789012345678901234567890")
y = gmpy2.mpfr("9.87654321098765432109876543210")
result = gmpy2.add(x, y) # 100-bit precision
print(result)import gmpy2
# Large number modular exponentiation
base = gmpy2.mpz("12345678901234567890")
exp = gmpy2.mpz("98765432109876543210")
mod = gmpy2.mpz("1000000007")
# Efficient modular exponentiation
result = gmpy2.powmod(base, exp, mod)
print(result)
# Cryptographic secure version
secure_result = gmpy2.powmod_sec(base, exp, mod)
print(secure_result)import gmpy2
x = gmpy2.mpz(17)
y = gmpy2.mpz(5)
# Different division modes
print(gmpy2.f_div(x, y)) # Floor: 3
print(gmpy2.c_div(x, y)) # Ceiling: 4
print(gmpy2.t_div(x, y)) # Truncate: 3
print(gmpy2.f_mod(x, y)) # Floor mod: 2
print(gmpy2.c_mod(x, y)) # Ceiling mod: -3
print(gmpy2.t_mod(x, y)) # Truncate mod: 2import gmpy2
# Exact rational arithmetic
a = gmpy2.mpq(22, 7) # 22/7
b = gmpy2.mpq(355, 113) # 355/113
# Exact operations
sum_exact = gmpy2.add(a, b) # Returns mpq
print(f"Sum: {sum_exact}")
print(f"Numerator: {gmpy2.numer(sum_exact)}")
print(f"Denominator: {gmpy2.denom(sum_exact)}")
# Rational division
quotient = gmpy2.qdiv(a, b)
print(f"Quotient: {quotient}")Install with Tessl CLI
npx tessl i tessl/pypi-gmpy2