{"id":5237,"library":"gmpy2","title":"gmpy2: GMP/MPFR/MPC for Python","description":"gmpy2 is an optimized, C-coded Python extension module that provides an interface to the GMP (GNU Multiple Precision Arithmetic Library), MPFR (Multiple-Precision Floating-point Reliable Library), and MPC (Multiple Precision Complex Library). It enables fast arbitrary-precision integer, rational, real, and complex arithmetic in Python. The current stable version is 2.3.0, and it is actively maintained with regular updates to support newer Python versions and fix bugs.","status":"active","version":"2.3.0","language":"en","source_language":"en","source_url":"https://github.com/gmpy2/gmpy2","tags":["arbitrary-precision","mathematics","numeric","GMP","MPFR","MPC","high-performance","scientific-computing"],"install":[{"cmd":"pip install gmpy2","lang":"bash","label":"Install stable release"}],"dependencies":[{"reason":"Underlying C library for arbitrary-precision integer and rational arithmetic. Often bundled in wheels.","package":"gmp","optional":true},{"reason":"Underlying C library for correctly rounded multiple-precision floating-point real arithmetic. Required since gmpy2 version 2.1.0a1. Often bundled in wheels.","package":"mpfr","optional":true},{"reason":"Underlying C library for correctly rounded multiple-precision complex floating-point arithmetic. Required since gmpy2 version 2.1.0a1. Often bundled in wheels.","package":"mpc","optional":true}],"imports":[{"symbol":"mpz","correct":"from gmpy2 import mpz"},{"symbol":"mpq","correct":"from gmpy2 import mpq"},{"symbol":"mpfr","correct":"from gmpy2 import mpfr"},{"symbol":"mpc","correct":"from gmpy2 import mpc"},{"symbol":"get_context","correct":"from gmpy2 import get_context"},{"note":"Importing all symbols with '*' is not recommended to avoid potential name conflicts with other modules or variables.","wrong":"from gmpy2 import *","symbol":"*","correct":"from gmpy2 import mpz, mpfr"}],"quickstart":{"code":"import gmpy2\nfrom gmpy2 import mpz, mpfr, get_context\n\n# Arbitrary-precision integer arithmetic\na = mpz(12345678901234567890 ** 2)\nb = mpz(5)\nprint(f\"mpz square: {a}\")\nprint(f\"mpz division: {a / b}\")\n\n# Arbitrary-precision floating-point real arithmetic\n# Use strings for exact representation to avoid Python float precision issues\npi_approx = mpfr('3.14159265358979323846264338327950288')\nprint(f\"Default precision pi: {pi_approx}\")\n\n# Manage precision using a context\ncurrent_context = get_context()\noriginal_precision = current_context.precision\n\nwith gmpy2.localcontext() as ctx:\n    ctx.precision = 100 # Set precision to 100 bits\n    pi_high_prec = gmpy2.const_pi()\n    print(f\"High precision pi (100 bits): {pi_high_prec}\")\n\nprint(f\"Original precision after context block: {current_context.precision == original_precision}\")\n\n# Example with rational numbers\nr = gmpy2.mpq(1, 3) + gmpy2.mpq(1, 4)\nprint(f\"mpq sum: {r}\")\n","lang":"python","description":"This quickstart demonstrates basic arbitrary-precision integer (mpz), real (mpfr), and rational (mpq) arithmetic. It also shows how to manage floating-point precision using `gmpy2.get_context()` and `gmpy2.localcontext()` to control precision settings. Constants should ideally be passed as strings to `mpfr` to ensure full arbitrary precision from the start, avoiding Python's native float limitations."},"warnings":[{"fix":"Upgrade Python to 3.11 or newer before updating gmpy2 past version 2.3.0.","message":"Future versions of gmpy2 (e.g., v2.4.0 and later) will drop support for CPython versions older than 3.11. Users currently on Python 3.9 or 3.10 with gmpy2 2.3.0 will need to upgrade their Python interpreter to at least 3.11 before upgrading to gmpy2 v2.4.0 (stable) or newer.","severity":"breaking","affected_versions":">=2.4.0 (alpha/beta versions already affected)"},{"fix":"Ensure GMP, MPFR, and MPC development libraries are installed on your system if pre-compiled wheels are not available.","message":"Installation via `pip` usually provides pre-compiled binary wheels which include the necessary GMP, MPFR, and MPC C libraries. However, if a wheel is not available for your specific platform or Python version, `pip install gmpy2` will attempt to build from source, requiring these underlying C development libraries (e.g., `libgmp-dev`, `libmpfr-dev`, `libmpc-dev` on Debian/Ubuntu) to be pre-installed on your system.","severity":"gotcha","affected_versions":"All versions (when building from source)"},{"fix":"Carefully manage the precision and scale of numbers. Monitor memory usage and CPU time for performance-critical sections. Consider using `xmpz` for mutable integer operations if appropriate.","message":"Arbitrary-precision arithmetic can be computationally intensive and memory-hungry, especially with extremely large numbers or extensive operations. While `gmpy2` is highly optimized, unmanaged use can still lead to significant performance bottlenecks or memory exhaustion for very demanding computations.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For temporary or localized changes to context settings, use `with gmpy2.localcontext() as ctx:`, which creates a thread-local copy and restores the previous context upon exiting the `with` block. Alternatively, explicitly create and activate contexts with `gmpy2.set_context(new_context)` and `gmpy2.get_context().copy()`.","message":"The `gmpy2.context` object, which controls parameters like floating-point precision, rounding mode, and exception handling, is a global object by default. Directly modifying `gmpy2.get_context()` can cause side effects across different parts of an application.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Replace all instances of `gmpy.mpf` with `gmpy2.mpfr`.","message":"The `mpf` type, which was part of the original `gmpy` library (the predecessor to `gmpy2`), was renamed to `mpfr` in `gmpy2` version 2.0.0b1 to reflect its reliance on the MPFR library. Code migrating from older `gmpy` to `gmpy2` should update `mpf` references to `mpfr`.","severity":"deprecated","affected_versions":"<2.0.0b1 (for `gmpy` library users)"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}