python - Is there an error of precision in that program? -
i'm trying verify sum , seems in program don't find 0, there error ?
from mpmath import * mp.dps = 1500 a=mpf(0.1) p=mpf(2.) def f1(x): return (a-(mpf(1)-mp.sqrt(1-x**2)))**p*x def f2(x): return x*(x-a)**p def f3(x): return x**p i=mp.quad(f1,[0,mp.sqrt(mpf(1)-mpf(0.9)**mpf(2))]) j=mp.quad(f2,[0,a]) k=mp.quad(f3,[0,a]) print i,j,k print mp.fabs(i)+mp.fabs(j)-mp.fabs(k)
even 1500 digits don't find 0.
when creating mpf
instances python float, value enter in code first converted 53-bit value , converted high-precision value. working values not expecting.
if working high-precision code, should either initialize string or integer. try following instead:
from mpmath import * mp.dps = 1500 a=mpf("0.1") p=mpf(2) def f1(x): return (a-(mpf(1)-mp.sqrt(1-x**2)))**p*x def f2(x): return x*(x-a)**p def f3(x): return x**p i=mp.quad(f1,[0,mp.sqrt(mpf(1)-mpf("0.9")**mpf(2))]) j=mp.quad(f2,[0,a]) k=mp.quad(f3,[0,a]) print i,j,k print mp.fabs(i)+mp.fabs(j)-mp.fabs(k)
Comments
Post a Comment