Jim Jeffries
Jim Jeffries

Reputation: 10081

Python Native Arbitrary Precision

Is there any way of compiling Python's interpretter in such a way that it could use a native (c) arbitrary precision library so that Python could use arbitrary precision as if it was a normal number instead of having to use the decimal class?

EG.

>>0.00000001 + 1
1.00000001

Upvotes: 3

Views: 746

Answers (1)

Rosh Oxymoron
Rosh Oxymoron

Reputation: 21055

If you're unsatisfied with the builtin decimal, you can try several libraries that implement higher precision floating point numbers. Most of them, however, are not exactly "native" - you might clarify what you mean by that and why you need it.

  1. mpmath - it provides a pure-Python implementation of high-precision floating point numbers, but it can automatically switch to GNU MP if support for it is available on the system, it has an extensive set of features
  2. gmpy - it provides a wrapper for the GNU MP library
  3. bigfloat - it provides a wrapper for the GNU MPFR library (which is based on GNU MP; wrapper doesn't look very promising since it uses ctypes)
  4. You can use Cython and use any of the above-mentioned libraries (e.g. GNU MP) directly, if it is required for a given performance-critical section. You can also make an ad-hoc Cython wrapper for your use case.

Upvotes: 3

Related Questions