Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 572
Jim Jeffries Avatar asked Dec 11 '25 10:12

Jim Jeffries


1 Answers

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.
like image 77
Rosh Oxymoron Avatar answered Dec 14 '25 01:12

Rosh Oxymoron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!