Karim Belabas on Mon, 09 Jul 2018 18:32:11 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: Truncation Precision in PARI |
* Peter Pein [2018-07-09 14:17]: > that is not very accurate :( > > I get: > > precision(-1 - Euler + 3/2*log(2*Pi) + 6*zetahurwitz(-1, 1, der = 1), 100) > %1 = > 0.1870730725097797894509591576777666319578148029622159376465535484192711630046534855901322306210633101 Please check ??precision : [...] If n is smaller than the precision of a t_REAL component of x, it is truncated, otherwise it is extended with zeros. [...] What you wrote first evaluates the expression to the default accuracy (38 decimal digits) then pads the result with trailing zeros. What you intended is achieved as follows: 1) Changing the global default accuracy ? \p100 realprecision = 115 significant digits (100 digits displayed) ? -1 - Euler + 3/2*log(2*Pi) + 6*zetahurwitz(-1, 1, 1) time = 11 ms. %2 = 0.1870730725097797894509591576777666319578148029622159376465535484192711630046534855901322306210633101 2) OR [usually better in programs] changing locally the accuracy in the dynamic scope. ? localprec(100); z = -1 - Euler + 3/2*log(2*Pi) + 6*zetahurwitz(-1, 1, 1) %1 = 0.18707307250977978945095915767776663196 This still prints results at the default accuracy, but precision(z) is 115 as requested and formatted printing indeed shows the expected digits: ? printf("%.100f", z) 0.1870730725097797894509591576777666319578148029622159376465535484192711630046534855901322306210633101 Cheers, K.B. P.S. Note that your second solution using sumalt is not rigorous in general (although rigorous error bounds can be given for specific cases), see ??sumalt In this case, it gives an essentially correct answer in this case, but 2 decimal digits are wrong at \p115 : ? \p115 ? sumalt(n=2,(-1)^n*(n-1)/(n+2)*zeta(n)) %3 = 0.1870730725097797894509591576777666319578148029622159376465535484192711630046534855901322306210633101211227714967803 ^^ wrong ? -1 - Euler + 3/2*log(2*Pi) + 6*zetahurwitz(-1, 1, der = 1) %4 = 0.1870730725097797894509591576777666319578148029622159376465535484192711630046534855901322306210633101211227714967866 ^^ OK ! ? \p120 realprecision = 134 significant digits (120 digits displayed) ? sumalt(n=2,(-1)^n*(n-1)/(n+2)*zeta(n)) %4 = 0.187073072509779789450959157677766631957814802962215937646553548419271163004653485590132230621063310121122771496786571185 -- Karim Belabas, IMB (UMR 5251) Tel: (+33) (0)5 40 00 26 17 Universite de Bordeaux Fax: (+33) (0)5 40 00 21 23 351, cours de la Liberation http://www.math.u-bordeaux.fr/~kbelabas/ F-33405 Talence (France) http://pari.math.u-bordeaux.fr/ [PARI/GP] `