I'm trying to use Bigfloat library in python 2.7.
from bigfloat import *
f1 = Context(precision=2000)
with precision(2000): f1 = 1e-19*1e-19*9e9/((1-1e-18)*(1-1e-18))-1e-19*1e-19*9e9
with precision(100): f2 = 1.6e-27*1.6e-27*6.6e-11/(1e-18*1e-18)
print BigFloat(f1) print f2
Python gives me f1=0, but it is not true. I tested it with g++ and the result is 1.75e-46.
Is it an error in my program? Is it not possible to calculate this precision with Bigfloat ? Is it an error in the lib?
As an example, here's how you might compute
f1to a precision of 256 bits using thebigfloatlibrary.Note the use of
BigFloat('1e-19'), which is creating the closest binary float to10**-19at the current precision (256 bits). This is different fromBigFloat(1e-19)(without the single quotes), since there1e-19is a Python float, so has already been rounded to 53-bit precision.Take a look at the documentation for more details.
However, with a bit of creativity and algebra, you don't need a high-precision library at all here. You can rewrite the expression for
f1as:and by putting everything over a common denominator, the quantity in parentheses can be rewritten as
(2 - z) * z / ((1 - z) * (1 - z)). So you could equally well computef1as:and in this form, you don't lose accuracy when
zis very small. So now regular Python floats are good enough:If you do decide you want to use a high-precision floating-point library, I'd also recommend looking at the gmpy2 library. It's based on the same underlying MPFR library as bigfloat, but it's better maintained.