Re: digits of a float

"Ruud H.G. van Tol" <[email protected]>
Newsgroups gmane.comp.mathematics.pari.user
Message-ID <[email protected]>
On 2024-12-26 19:21, Loïc Grenié wrote:
> On Thu 26 Dec, 2024, at 17:21, Ruud H.G. van Tol wrote:
>
>     [...] For Pi-digits, I am now using:
>
>     pi_digits(n, p=20)= {
>        my(u=10^(n+p-1), f(x, u)=my(n=1, q=u\x, r=q, s=1, t);
>     while(t=(q\=(x*x))\(n+=2), r+=(s=-s)*t); r*4);
>        digits((4*f(5, u) - f(239, u))\10^p);
>     } \\ Machin-like, with p > the maximal number of consecutive
>     9-digits to
>     be expected (A048940)
>
>
> Wouldn't floor be more adapted than round?

The code has an off-by-1 error. Corrected:

pi_digits(n, p=20)= {
   my(u=10^(n+p), f(x, u)=my(n=1, q=u\x, r=q, s=1, t); 
while(t=(q\=(x*x))\(n+=2), r+=(s=-s)*t); r*4);
   digits((4*f(5, u) - f(239, u))\10^(p+1));
}


With a low value of p, the rounding shows:

? pi_digits( 769, 10 )[-10..-1]
% [1, 3, 4, 9, 9, 9, 9, 9, 9, 8]  :OK

? pi_digits( 763, 6 )[-4..-1]
% [1, 3, 5, 0]  :BAD

? pi_digits( 762, 7 )[-3..-1]
% [1, 3, 5]  :BAD (also invalidates the comment about what p is needed)

-- Ruud
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.